Search in sources :

Example 91 with BasedSequence

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

the class InlineParserImpl method parseBackticks.

/**
 * Attempt to parse backticks, adding either a backtick code span or a literal sequence of backticks.
 *
 * @return true if matched backticks, false otherwise
 */
protected boolean parseBackticks() {
    BasedSequence ticks = match(myParsing.TICKS_HERE);
    if (ticks == null) {
        return false;
    }
    int afterOpenTicks = index;
    BasedSequence matched;
    while ((matched = match(myParsing.TICKS)) != null) {
        if (matched.equals(ticks)) {
            int ticksLength = ticks.length();
            BasedSequence content = input.subSequence(afterOpenTicks - ticksLength, index - ticksLength);
            final BasedSequence codeText = input.subSequence(afterOpenTicks, index - ticksLength);
            Code node = new Code(input.subSequence(afterOpenTicks - ticksLength, afterOpenTicks), codeText, input.subSequence(index - ticksLength, index));
            if (options.codeSoftLineBreaks) {
                // add softbreaks to code ast
                final int length = codeText.length();
                int lastPos = 0;
                while (lastPos < length) {
                    int softBreak = codeText.indexOfAny("\n\r", lastPos);
                    int pos = softBreak == -1 ? length : softBreak;
                    int lineBreak = pos;
                    final Text textNode = new Text(codeText.subSequence(lastPos, pos));
                    node.appendChild(textNode);
                    lastPos = pos;
                    if (lastPos >= length)
                        break;
                    if (codeText.charAt(lastPos) == '\r') {
                        lastPos++;
                        if (lastPos >= length)
                            break;
                        if (codeText.charAt(lastPos) == '\n')
                            lastPos++;
                    } else {
                        lastPos++;
                    }
                    if (lastPos >= length)
                        break;
                    if (lineBreak < lastPos) {
                        SoftLineBreak softLineBreak = new SoftLineBreak(codeText.subSequence(softBreak, lastPos));
                        node.appendChild(softLineBreak);
                    }
                }
            } else {
                final Text textNode = new Text(codeText);
                node.appendChild(textNode);
            }
            appendNode(node);
            return true;
        }
    }
    // If we got here, we didn't match a closing backtick sequence.
    index = afterOpenTicks;
    appendText(ticks);
    return true;
}
Also used : 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