Search in sources :

Example 31 with BasedSequence

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

the class AbbreviationNodeRenderer method render.

private void render(Abbreviation node, NodeRendererContext context, HtmlWriter html) {
    String text = node.getChars().unescape();
    BasedSequence abbreviation = node.getAbbreviation();
    String tag;
    if (options.useLinks) {
        html.attr("href", "#");
        tag = "a";
    } else {
        tag = "abbr";
    }
    html.attr("title", abbreviation);
    html.srcPos(node.getChars()).withAttr().tag(tag);
    html.text(text);
    html.closeTag(tag);
}
Also used : BasedSequence(com.vladsch.flexmark.util.sequence.BasedSequence)

Example 32 with BasedSequence

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

the class AbbreviationParagraphPreProcessor method preProcessBlock.

@Override
public int preProcessBlock(Paragraph block, ParserState state) {
    BasedSequence trySequence = block.getChars();
    Matcher matcher = ABBREVIATION_BLOCK.matcher(trySequence);
    int lastFound = 0;
    while (matcher.find()) {
        // abbreviation definition
        if (matcher.start() != lastFound)
            break;
        lastFound = matcher.end();
        int openingStart = matcher.start(1);
        int openingEnd = matcher.end(1);
        int textEnd = lastFound;
        BasedSequence openingMarker = trySequence.subSequence(openingStart, openingStart + 2);
        BasedSequence text = trySequence.subSequence(openingStart + 2, openingEnd - 2).trim();
        BasedSequence closingMarker = trySequence.subSequence(openingEnd - 2, openingEnd);
        AbbreviationBlock abbreviationBlock = new AbbreviationBlock();
        abbreviationBlock.setOpeningMarker(openingMarker);
        abbreviationBlock.setText(text);
        abbreviationBlock.setClosingMarker(closingMarker);
        abbreviationBlock.setAbbreviation(trySequence.subSequence(openingEnd, textEnd).trim());
        abbreviationBlock.setCharsFromContent();
        block.insertBefore(abbreviationBlock);
        state.blockAdded(abbreviationBlock);
        abbreviationMap.put(abbreviationMap.normalizeKey(abbreviationBlock.getText()), abbreviationBlock);
    }
    return lastFound;
}
Also used : AbbreviationBlock(com.vladsch.flexmark.ext.abbreviation.AbbreviationBlock) Matcher(java.util.regex.Matcher) BasedSequence(com.vladsch.flexmark.util.sequence.BasedSequence)

Example 33 with BasedSequence

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

the class AttributesInlineParserExtension method parse.

@Override
public boolean parse(final InlineParser inlineParser) {
    if (inlineParser.peek(1) != '{') {
        int index = inlineParser.getIndex();
        BasedSequence input = inlineParser.getInput();
        Matcher matcher = inlineParser.matcher(parsing.ATTRIBUTES_TAG);
        if (matcher != null) {
            BasedSequence attributesOpen = input.subSequence(matcher.start(), matcher.end());
            // see what we have
            // open, see if open/close
            BasedSequence attributesText = input.subSequence(matcher.start(1), matcher.end(1));
            AttributesNode attributes = new AttributesNode(attributesOpen.subSequence(0, 1), attributesText, attributesOpen.endSequence(1));
            attributes.setCharsFromContent();
            int leadingSpaces = attributesText.countLeading(" \t");
            // give it to the text node
            if (leadingSpaces > 0) {
                inlineParser.appendText(attributesText, 0, leadingSpaces);
                attributesText = attributesText.subSequence(leadingSpaces);
            }
            inlineParser.flushTextNode();
            inlineParser.getBlock().appendChild(attributes);
            BasedSequence attributeText = attributesText.trim();
            if (!attributeText.isEmpty()) {
                // have some attribute text
                // parse attributes
                Matcher attributeMatcher = parsing.ATTRIBUTE.matcher(attributeText);
                while (attributeMatcher.find()) {
                    BasedSequence attributeName = attributeText.subSequence(attributeMatcher.start(1), attributeMatcher.end(1));
                    BasedSequence attributeSeparator = attributeMatcher.groupCount() == 1 || attributeMatcher.start(2) == -1 ? BasedSequence.NULL : attributeText.subSequence(attributeMatcher.end(1), attributeMatcher.start(2)).trim();
                    BasedSequence attributeValue = attributeMatcher.groupCount() == 1 || attributeMatcher.start(2) == -1 ? BasedSequence.NULL : attributeText.subSequence(attributeMatcher.start(2), attributeMatcher.end(2));
                    boolean isQuoted = attributeValue.length() >= 2 && (attributeValue.charAt(0) == '"' && attributeValue.endCharAt(1) == '"' || attributeValue.charAt(0) == '\'' && attributeValue.endCharAt(1) == '\'');
                    BasedSequence attributeOpen = !isQuoted ? BasedSequence.NULL : attributeValue.subSequence(0, 1);
                    BasedSequence attributeClose = !isQuoted ? BasedSequence.NULL : attributeValue.endSequence(1, 0);
                    if (isQuoted) {
                        attributeValue = attributeValue.midSequence(1, -1);
                    }
                    AttributeNode attribute;
                    if (attributeSeparator.isNull() && attributeSeparator.isNull() && attributeValue.isNull() && AttributeNode.isImplicitName(attributeName)) {
                        attribute = new AttributeNode(attributeName.subSequence(0, 1), attributeSeparator, attributeOpen, attributeName.subSequence(1), attributeClose);
                    } else {
                        attribute = new AttributeNode(attributeName, attributeSeparator, attributeOpen, attributeValue, attributeClose);
                    }
                    attributes.appendChild(attribute);
                }
                return true;
            }
            // did not process, reset to where we started
            inlineParser.setIndex(index);
        }
    }
    return false;
}
Also used : AttributeNode(com.vladsch.flexmark.ext.attributes.AttributeNode) Matcher(java.util.regex.Matcher) BasedSequence(com.vladsch.flexmark.util.sequence.BasedSequence) AttributesNode(com.vladsch.flexmark.ext.attributes.AttributesNode)

Example 34 with BasedSequence

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

the class CoreNodeFormatter method render.

private void render(IndentedCodeBlock node, NodeFormatterContext context, MarkdownWriter markdown) {
    markdown.blankLine();
    String prefix = RepeatedCharSequence.of(" ", listOptions.getCodeIndent()).toString();
    if (options.emulationProfile == ParserEmulationProfile.GITHUB_DOC) {
        if (node.getParent() instanceof ListItem) {
            BasedSequence marker = ((ListItem) node.getParent()).getOpeningMarker();
            prefix = RepeatedCharSequence.of(" ", Utils.minLimit(8 - marker.length() - 1, 4)).toString();
        }
    }
    markdown.pushPrefix().addPrefix(prefix);
    markdown.openPreFormatted(true);
    if (options.indentedCodeMinimizeIndent) {
        List<BasedSequence> lines = node.getContentLines();
        int[] leadColumns = new int[lines.size()];
        int minSpaces = Integer.MAX_VALUE;
        int i = 0;
        for (BasedSequence line : lines) {
            leadColumns[i] = line.countLeadingColumns(0, " \t");
            minSpaces = Utils.min(minSpaces, leadColumns[i]);
            i++;
        }
        if (minSpaces > 0) {
            i = 0;
            // markdown.append(out);
            for (BasedSequence line : lines) {
                if (leadColumns[i] > minSpaces)
                    markdown.repeat(' ', leadColumns[i] - minSpaces);
                markdown.append(line.trimStart());
                i++;
            }
        } else {
            markdown.append(node.getContentChars());
        }
    } else {
        markdown.append(node.getContentChars());
    }
    markdown.closePreFormatted();
    markdown.popPrefix();
    markdown.tailBlankLine();
}
Also used : BasedSequence(com.vladsch.flexmark.util.sequence.BasedSequence)

Example 35 with BasedSequence

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

the class CoreNodeFormatter method render.

private void render(FencedCodeBlock node, NodeFormatterContext context, MarkdownWriter markdown) {
    markdown.blankLine();
    CharSequence openingMarker = node.getOpeningMarker();
    CharSequence closingMarker = node.getClosingMarker();
    char openingMarkerChar = openingMarker.charAt(0);
    char closingMarkerChar = closingMarker.length() > 0 ? closingMarker.charAt(0) : '\0';
    int openingMarkerLen = openingMarker.length();
    int closingMarkerLen = closingMarker.length();
    switch(options.fencedCodeMarkerType) {
        case ANY:
            break;
        case BACK_TICK:
            openingMarkerChar = '`';
            closingMarkerChar = openingMarkerChar;
            break;
        case TILDE:
            openingMarkerChar = '~';
            closingMarkerChar = openingMarkerChar;
            break;
    }
    if (openingMarkerLen < options.fencedCodeMarkerLength)
        openingMarkerLen = options.fencedCodeMarkerLength;
    if (closingMarkerLen < options.fencedCodeMarkerLength)
        closingMarkerLen = options.fencedCodeMarkerLength;
    openingMarker = RepeatedCharSequence.of(String.valueOf(openingMarkerChar), openingMarkerLen);
    if (options.fencedCodeMatchClosingMarker || closingMarkerChar == '\0')
        closingMarker = openingMarker;
    else
        closingMarker = RepeatedCharSequence.of(String.valueOf(closingMarkerChar), closingMarkerLen);
    markdown.append(openingMarker);
    if (options.fencedCodeSpaceBeforeInfo)
        markdown.append(' ');
    markdown.append(node.getInfo());
    markdown.line();
    markdown.openPreFormatted(true);
    if (options.fencedCodeMinimizeIndent) {
        List<BasedSequence> lines = node.getContentLines();
        int[] leadColumns = new int[lines.size()];
        int minSpaces = Integer.MAX_VALUE;
        int i = 0;
        for (BasedSequence line : lines) {
            leadColumns[i] = line.countLeadingColumns(0, " \t");
            minSpaces = Utils.min(minSpaces, leadColumns[i]);
            i++;
        }
        ArrayList<BasedSequence> trimmedLines = new ArrayList<BasedSequence>();
        if (minSpaces > 0) {
            i = 0;
            // markdown.append(out);
            for (BasedSequence line : lines) {
                if (leadColumns[i] > minSpaces)
                    markdown.repeat(' ', leadColumns[i] - minSpaces);
                markdown.append(line.trimStart());
                i++;
            }
        } else {
            markdown.append(node.getContentChars());
        }
    } else {
        markdown.append(node.getContentChars());
    }
    markdown.closePreFormatted();
    markdown.line().append(closingMarker).line();
    markdown.tailBlankLine();
}
Also used : BasedSequence(com.vladsch.flexmark.util.sequence.BasedSequence) RepeatedCharSequence(com.vladsch.flexmark.util.sequence.RepeatedCharSequence)

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