Search in sources :

Example 66 with BasedSequence

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

the class FormattingAppendableImpl method appendImpl.

private void appendImpl(final CharSequence csq, final int start, final int end) throws IOException {
    int lastPos = start;
    BasedSequence seq = BasedSequenceImpl.of(csq);
    if (myPreFormattedNesting > 0) {
        setOffsetBefore(myAppendable.getLength());
        int endNoEOL = start + seq.subSequence(start, end).removeSuffix("\n").length();
        if (lastPos < end) {
            beforePre();
        }
        while (lastPos < endNoEOL) {
            int pos = seq.indexOf(myEOL, lastPos, endNoEOL);
            int endPos = pos == -1 ? endNoEOL : pos + 1;
            if (lastPos < endPos) {
                if (myPendingPreFormattedPrefix && !myPrefix.isEmpty()) {
                    myAppendable.append(myPrefix);
                }
                myPendingPreFormattedPrefix = false;
                myAppendable.append(csq, lastPos, endPos);
                lastPos = endPos;
            }
            if (pos == -1)
                break;
            myLineCount++;
            myPendingPreFormattedPrefix = true;
            lastPos = endPos;
        }
        myModCount++;
        // if EOL is last then we reset pending and eol mod count
        if (lastPos == endNoEOL && endNoEOL != end) {
            myPendingEOL = 1;
            myPendingPreFormattedPrefix = true;
        }
    } else {
        // have to handle \n, white spaces, etc
        boolean firstAppend = true;
        while (lastPos < end) {
            int pos = seq.indexOfAny(myWhitespaceEOL, lastPos, end);
            // output what has accumulated before
            int spanEnd = pos == -1 ? end : pos;
            if (lastPos < spanEnd) {
                beforeAppendText(true, true, true);
                if (firstAppend) {
                    setOffsetBefore(myAppendable.getLength());
                    firstAppend = false;
                }
                myAppendable.append(csq, lastPos, spanEnd);
                myModCount++;
            }
            if (pos == -1)
                break;
            // spaces and tabs are only output if we don't have a pending EOL and they don't come before an EOL
            int span = seq.countChars(myWhitespaceEOL, pos, end);
            if (myPendingEOL == 0) {
                int eolPos = seq.indexOf(myEOL, pos, pos + span);
                if (eolPos != -1) {
                    // we don't output the spaces after the EOL but make EOL pending
                    if (eolPos > pos && !haveOptions(SUPPRESS_TRAILING_WHITESPACE))
                        addPendingSpaces(eolPos - pos);
                    setPendingEOL(1);
                } else {
                    // no eol, make spaces pending
                    addPendingSpaces(span);
                }
            } else {
            // we are coming after an EOL, all whitespaces are ignored
            }
            pos += span;
            lastPos = pos;
        }
    }
}
Also used : BasedSequence(com.vladsch.flexmark.util.sequence.BasedSequence)

Example 67 with BasedSequence

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

the class BlockContent method getString.

public String getString() {
    if (lines.size() == 0)
        return "";
    StringBuilder sb = new StringBuilder();
    for (BasedSequence line : lines) {
        sb.append(line.trimEOL());
        sb.append('\n');
    }
    return sb.toString();
}
Also used : BasedSequence(com.vladsch.flexmark.util.sequence.BasedSequence)

Example 68 with BasedSequence

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

the class EnumRefTextCollectingVisitor method visit.

private void visit(HardLineBreak node) {
    final BasedSequence chars = node.getChars();
    out.append(chars.subSequence(chars.length() - 1, chars.length()));
}
Also used : BasedSequence(com.vladsch.flexmark.util.sequence.BasedSequence)

Example 69 with BasedSequence

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

the class EnumeratedReferenceLinkRefProcessor method createNode.

@Override
public Node createNode(BasedSequence nodeChars) {
    BasedSequence enumeratedReferenceId = nodeChars.midSequence(2, -1).trim();
    EnumeratedReferenceBlock enumeratedReferenceBlock = enumeratedReferenceId.length() > 0 ? enumeratedReferenceRepository.get(enumeratedReferenceId.toString()) : null;
    if (nodeChars.charAt(1) == '@') {
        // reference link
        EnumeratedReferenceLink enumeratedReference = new EnumeratedReferenceLink(nodeChars.subSequence(0, 2), enumeratedReferenceId, nodeChars.endSequence(1));
        enumeratedReference.setEnumeratedReferenceBlock(enumeratedReferenceBlock);
        return enumeratedReference;
    } else {
        // reference text
        EnumeratedReferenceText enumeratedReferenceText = new EnumeratedReferenceText(nodeChars.subSequence(0, 2), enumeratedReferenceId, nodeChars.endSequence(1));
        enumeratedReferenceText.setEnumeratedReferenceBlock(enumeratedReferenceBlock);
        return enumeratedReferenceText;
    }
}
Also used : BasedSequence(com.vladsch.flexmark.util.sequence.BasedSequence)

Example 70 with BasedSequence

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

the class YouTubeLinkNodePostProcessor method process.

@Override
public void process(NodeTracker state, Node node) {
    if (node instanceof Link) {
        Node previous = node.getPrevious();
        if (previous instanceof Text) {
            final BasedSequence chars = previous.getChars();
            if (chars.endsWith("@") && chars.isContinuedBy(node.getChars())) {
                // trim previous chars to remove '@'
                previous.setChars(chars.subSequence(0, chars.length() - 1));
                YouTubeLink youTubeLink = new YouTubeLink((Link) node);
                youTubeLink.takeChildren(node);
                node.unlink();
                previous.insertAfter(youTubeLink);
                state.nodeRemoved(node);
                state.nodeAddedWithChildren(youTubeLink);
            }
        }
    }
}
Also used : YouTubeLink(com.vladsch.flexmark.ext.youtube.embedded.YouTubeLink) Node(com.vladsch.flexmark.ast.Node) BasedSequence(com.vladsch.flexmark.util.sequence.BasedSequence) Text(com.vladsch.flexmark.ast.Text) Link(com.vladsch.flexmark.ast.Link) YouTubeLink(com.vladsch.flexmark.ext.youtube.embedded.YouTubeLink)

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