Search in sources :

Example 46 with BasedSequence

use of com.vladsch.flexmark.util.sequence.BasedSequence in project flexmark-java by vsch.

the class TableBlockParser method split.

private static List<BasedSequence> split(BasedSequence input) {
    BasedSequence line = input.trim();
    int lineLength = line.length();
    List<BasedSequence> segments = new ArrayList<BasedSequence>();
    if (line.startsWith("|")) {
        // segments.add(line.subSequence(0, 1));
        line = line.subSequence(1, lineLength);
        lineLength--;
    }
    boolean escape = false;
    int lastPos = 0;
    int cellChars = 0;
    for (int i = 0; i < lineLength; i++) {
        char c = line.charAt(i);
        if (escape) {
            escape = false;
            cellChars++;
        } else {
            switch(c) {
                case '\\':
                    escape = true;
                    // Removing the escaping '\' is handled by the inline parser later, so add it to cell
                    cellChars++;
                    break;
                case '|':
                    segments.add(line.subSequence(lastPos, i));
                    // segments.add(line.subSequence(i, i + 1));
                    lastPos = i + 1;
                    cellChars = 0;
                    break;
                default:
                    cellChars++;
            }
        }
    }
    if (cellChars > 0) {
        segments.add(line.subSequence(lastPos, lineLength));
    }
    return segments;
}
Also used : BasedSequence(com.vladsch.flexmark.util.sequence.BasedSequence) ArrayList(java.util.ArrayList)

Example 47 with BasedSequence

use of com.vladsch.flexmark.util.sequence.BasedSequence in project flexmark-java by vsch.

the class TableBlockParser method parseInlines.

@Override
public void parseInlines(InlineParser inlineParser) {
    Node section = new TableHead();
    block.appendChild(section);
    List<TableCell.Alignment> alignments = parseAlignment(separatorLine);
    int rowNumber = 0;
    int separatorColumns = alignments.size();
    for (BasedSequence rowLine : block.getContentLines()) {
        if (rowNumber == separatorLineNumber) {
            section.setCharsFromContent();
            section = new TableSeparator();
            block.appendChild(section);
        } else if (rowNumber == separatorLineNumber + 1) {
            section.setCharsFromContent();
            section = new TableBody();
            block.appendChild(section);
        }
        List<BasedSequence> cells = split(rowLine);
        TableRow tableRow = new TableRow(rowLine.trimEOL());
        int rowCells = countCells(cells);
        int maxColumns = rowCells;
        if (bodyColumnsTruncatedToHead && maxColumns > separatorColumns)
            maxColumns = separatorColumns;
        if (rowNumber >= separatorLineNumber) {
            if (!bodyColumnsFilledToHead && rowCells < maxColumns)
                maxColumns = rowCells;
            else if (bodyColumnsFilledToHead && maxColumns < separatorColumns)
                maxColumns = separatorColumns;
        }
        int segmentOffset = 0;
        BasedSequence openingMarker = BasedSequence.NULL;
        for (int i = 0; i < maxColumns; i++) {
            BasedSequence cell = i < rowCells ? cells.get(i + segmentOffset) : BasedSequence.NULL;
            if (!isCell(cell)) {
                openingMarker = cell;
                segmentOffset++;
                cell = i < rowCells ? cells.get(i + segmentOffset) : BasedSequence.NULL;
            }
            TableCell.Alignment alignment = i < alignments.size() ? alignments.get(i) : null;
            TableCell tableCell = new TableCell();
            tableCell.setHeader(rowNumber < separatorLineNumber);
            tableCell.setAlignment(alignment);
            tableCell.setOpeningMarker(openingMarker);
            openingMarker = BasedSequence.NULL;
            // if the next one is not a cell then it is our closing marker
            if (i + segmentOffset + 1 < cells.size()) {
                BasedSequence closingMarker = cells.get(i + segmentOffset + 1);
                if (!isCell(closingMarker)) {
                    segmentOffset++;
                    tableCell.setClosingMarker(closingMarker);
                }
            }
            BasedSequence trimmed = cell.trim();
            tableCell.setText(trimmed);
            inlineParser.parse(trimmed, tableCell);
            tableCell.setCharsFromContent();
            tableRow.appendChild(tableCell);
        }
        tableRow.setCharsFromContent();
        section.appendChild(tableRow);
        rowNumber++;
    }
    if (section instanceof TableSeparator) {
        block.appendChild(new TableBody());
    }
    section.setCharsFromContent();
}
Also used : Node(com.vladsch.flexmark.ast.Node) BasedSequence(com.vladsch.flexmark.util.sequence.BasedSequence)

Example 48 with BasedSequence

use of com.vladsch.flexmark.util.sequence.BasedSequence in project flexmark-java by vsch.

the class TaskListNodeFormatter method render.

private void render(final TaskListItem node, final NodeFormatterContext context, final MarkdownWriter markdown) {
    BasedSequence markerSuffix = node.getMarkerSuffix();
    switch(myOptions.taskListItemCase) {
        case AS_IS:
            break;
        case LOWERCASE:
            markerSuffix = markerSuffix.toLowerCase();
            break;
        case UPPERCASE:
            markerSuffix = markerSuffix.toUpperCase();
            break;
        default:
            throw new IllegalStateException("Missing case for TaskListItemCase " + myOptions.taskListItemCase.name());
    }
    if (node.isItemDoneMarker()) {
        switch(myOptions.taskListItemPlacement) {
            case AS_IS:
            case INCOMPLETE_FIRST:
            case INCOMPLETE_NESTED_FIRST:
                break;
            case COMPLETE_TO_NON_TASK:
            case COMPLETE_NESTED_TO_NON_TASK:
                markerSuffix = BasedSequence.NULL;
                break;
            default:
                throw new IllegalStateException("Missing case for ListItemPlacement " + myOptions.taskListItemPlacement.name());
        }
    }
    CoreNodeFormatter.renderListItem(node, context, markdown, listOptions, markerSuffix.isEmpty() ? markerSuffix : markerSuffix.append(" "));
}
Also used : BasedSequence(com.vladsch.flexmark.util.sequence.BasedSequence)

Example 49 with BasedSequence

use of com.vladsch.flexmark.util.sequence.BasedSequence in project flexmark-java by vsch.

the class TaskListNodeRenderer method render.

private void render(final TaskListItem node, final NodeRendererContext context, final HtmlWriter html) {
    final BasedSequence sourceText = context.getHtmlOptions().sourcePositionParagraphLines || node.getFirstChild() == null ? node.getChars() : node.getFirstChild().getChars();
    if (listOptions.isTightListItem(node)) {
        if (!itemClass.isEmpty())
            html.attr("class", itemClass);
        html.srcPos(sourceText.getStartOffset(), sourceText.getEndOffset()).withAttr(CoreNodeRenderer.TIGHT_LIST_ITEM).withCondIndent().tagLine("li", new Runnable() {

            @Override
            public void run() {
                html.raw(node.isItemDoneMarker() ? TaskListNodeRenderer.this.doneMarker : TaskListNodeRenderer.this.notDoneMarker);
                context.renderChildren(node);
            }
        });
    } else {
        if (!looseItemClass.isEmpty())
            html.attr("class", looseItemClass);
        html.withAttr(CoreNodeRenderer.LOOSE_LIST_ITEM).tagIndent("li", new Runnable() {

            @Override
            public void run() {
                if (!TaskListNodeRenderer.this.paragraphClass.isEmpty())
                    html.attr("class", TaskListNodeRenderer.this.paragraphClass);
                html.srcPos(sourceText.getStartOffset(), sourceText.getEndOffset()).withAttr(TASK_ITEM_PARAGRAPH).tagLine("p", new Runnable() {

                    @Override
                    public void run() {
                        html.raw(node.isItemDoneMarker() ? TaskListNodeRenderer.this.doneMarker : TaskListNodeRenderer.this.notDoneMarker);
                        context.renderChildren(node);
                    }
                });
            }
        });
    }
}
Also used : BasedSequence(com.vladsch.flexmark.util.sequence.BasedSequence)

Example 50 with BasedSequence

use of com.vladsch.flexmark.util.sequence.BasedSequence in project flexmark-java by vsch.

the class GfmIssuesInlineParserExtension method parse.

@Override
public boolean parse(final InlineParser inlineParser) {
    BasedSequence[] matches = inlineParser.matchWithGroups(GITHUB_ISSUE);
    if (matches != null) {
        BasedSequence input = inlineParser.getInput();
        inlineParser.flushTextNode();
        BasedSequence openMarker = matches[1];
        BasedSequence text = matches[2];
        GfmIssue gfmIssue = new GfmIssue(openMarker, text);
        inlineParser.getBlock().appendChild(gfmIssue);
        return true;
    }
    return false;
}
Also used : GfmIssue(com.vladsch.flexmark.ext.gfm.issues.GfmIssue) BasedSequence(com.vladsch.flexmark.util.sequence.BasedSequence)

Aggregations

BasedSequence (com.vladsch.flexmark.util.sequence.BasedSequence)91 Matcher (java.util.regex.Matcher)13 Node (com.vladsch.flexmark.ast.Node)6 ArrayList (java.util.ArrayList)5 MacroClose (com.vladsch.flexmark.ext.xwiki.macros.MacroClose)3 ReplacedTextMapper (com.vladsch.flexmark.util.sequence.ReplacedTextMapper)3 Text (com.vladsch.flexmark.ast.Text)2 AttributesNode (com.vladsch.flexmark.ext.attributes.AttributesNode)2 FootnoteBlock (com.vladsch.flexmark.ext.footnotes.FootnoteBlock)2 Macro (com.vladsch.flexmark.ext.xwiki.macros.Macro)2 Pair (com.vladsch.flexmark.util.Pair)2 RepeatedCharSequence (com.vladsch.flexmark.util.sequence.RepeatedCharSequence)2 Block (com.vladsch.flexmark.ast.Block)1 BulletListItem (com.vladsch.flexmark.ast.BulletListItem)1 Link (com.vladsch.flexmark.ast.Link)1 ListItem (com.vladsch.flexmark.ast.ListItem)1 NodeIterator (com.vladsch.flexmark.ast.NodeIterator)1 OrderedListItem (com.vladsch.flexmark.ast.OrderedListItem)1 Parsing (com.vladsch.flexmark.ast.util.Parsing)1 TextCollectingVisitor (com.vladsch.flexmark.ast.util.TextCollectingVisitor)1