Search in sources :

Example 26 with BasedSequence

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

the class DocumentParser method preProcessParagraph.

/**
 * pre-process a paragraph block
 *
 * @param block        paragraph block to pre-process
 * @param stage        paragraph pre-processor dependency stage
 * @param processorMap paragraph pre-processor cache
 */
private void preProcessParagraph(Paragraph block, ParagraphPreProcessorDependencyStage stage, ParagraphPreProcessorCache processorMap) {
    while (true) {
        boolean hadChanges = false;
        for (ParagraphPreProcessorFactory factory : stage.dependents) {
            ParagraphPreProcessor processor = processorMap.getItem(factory);
            int pos = processor.preProcessBlock(block, this);
            if (pos > 0) {
                hadChanges = true;
                // skip leading blanks
                BasedSequence blockChars = block.getChars();
                BasedSequence contentChars = blockChars.subSequence(pos + blockChars.countChars(BasedSequence.WHITESPACE_CHARS, pos, blockChars.length()));
                if (contentChars.isBlank()) {
                    // all used up
                    block.unlink();
                    blockRemoved(block);
                    return;
                } else {
                    // skip lines that were removed
                    int iMax = block.getLineCount();
                    int i;
                    for (i = 0; i < iMax; i++) {
                        if (block.getLineChars(i).getEndOffset() > contentChars.getStartOffset())
                            break;
                    }
                    if (i >= iMax) {
                        // all used up
                        block.unlink();
                        blockRemoved(block);
                        return;
                    } else if (block.getLineChars(i).getEndOffset() == contentChars.getStartOffset()) {
                        // full lines removed
                        block.setContent(block, i, iMax);
                    } else {
                        // need to change the first line of the line list
                        ArrayList<BasedSequence> lines = new ArrayList<BasedSequence>(iMax - i);
                        lines.addAll(block.getContentLines().subList(i, iMax));
                        int start = contentChars.getStartOffset() - lines.get(0).getStartOffset();
                        if (start > 0 && start < lines.get(0).length()) {
                            lines.set(0, lines.get(0).subSequence(start));
                        }
                        // now we copy the indents
                        int[] indents = new int[iMax - i];
                        System.arraycopy(block.getLineIndents(), i, indents, 0, indents.length);
                        block.setContentLines(lines);
                        block.setLineIndents(indents);
                        block.setChars(contentChars);
                    }
                }
            }
        }
        if (!hadChanges || stage.dependents.size() < 2)
            break;
    }
}
Also used : BasedSequence(com.vladsch.flexmark.util.sequence.BasedSequence)

Example 27 with BasedSequence

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

the class FencedCodeBlockParser method closeBlock.

@Override
public void closeBlock(ParserState state) {
    // first line, if not blank, has the info string
    List<BasedSequence> lines = content.getLines();
    if (lines.size() > 0) {
        BasedSequence info = lines.get(0);
        if (!info.isBlank()) {
            block.setInfo(info.trim());
        }
        BasedSequence chars = content.getSpanningChars();
        BasedSequence spanningChars = chars.baseSubSequence(chars.getStartOffset(), lines.get(0).getEndOffset());
        if (lines.size() > 1) {
            // have more lines
            List<BasedSequence> segments = lines.subList(1, lines.size());
            block.setContent(spanningChars, segments);
            if (codeContentBlock) {
                CodeBlock codeBlock = new CodeBlock();
                codeBlock.setContent(segments);
                codeBlock.setCharsFromContent();
                block.appendChild(codeBlock);
            } else {
                Text codeBlock = new Text(SegmentedSequence.of(segments, chars.subSequence(0, 0)));
                block.appendChild(codeBlock);
            }
        } else {
            block.setContent(spanningChars, BasedSequence.EMPTY_LIST);
        }
    } else {
        block.setContent(content);
    }
    block.setCharsFromContent();
    content = null;
}
Also used : BasedSequence(com.vladsch.flexmark.util.sequence.BasedSequence)

Example 28 with BasedSequence

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

the class TextNodeConverter method appendChild.

public void appendChild(Node child) {
    BasedSequence childChars = child.getChars();
    assert nodeChars.containsAllOf(childChars) : "child " + child.toAstString(false) + " is not within parent sequence " + Node.toSegmentSpan(nodeChars, null);
    assert remainingChars.containsAllOf(childChars) : "child " + child.toAstString(false) + " is not within remaining sequence " + Node.toSegmentSpan(remainingChars, null);
    child.unlink();
    if (!(child instanceof Text)) {
        if (remainingChars.getStartOffset() < childChars.getStartOffset()) {
            // add preceding chars as Text
            list.add(new Text(remainingChars.subSequence(0, childChars.getStartOffset() - remainingChars.getStartOffset())));
        }
        // punch out remaining node chars
        remainingChars = remainingChars.subSequence(childChars.getEndOffset() - remainingChars.getStartOffset());
        list.add(child);
    }
}
Also used : BasedSequence(com.vladsch.flexmark.util.sequence.BasedSequence) Text(com.vladsch.flexmark.ast.Text)

Example 29 with BasedSequence

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

the class HtmlWriter method srcPosWithTrailingEOL.

@SuppressWarnings("WeakerAccess")
public HtmlWriter srcPosWithTrailingEOL(BasedSequence sourceText) {
    if (sourceText.isNotNull()) {
        int endOffset = sourceText.getEndOffset();
        BasedSequence base = sourceText.getBaseSequence();
        while (endOffset < base.length()) {
            char c = base.charAt(endOffset);
            if (c != ' ' && c != '\t')
                break;
            endOffset++;
        }
        if (endOffset < base.length() && base.charAt(endOffset) == '\r') {
            endOffset++;
        }
        if (endOffset < base.length() && base.charAt(endOffset) == '\n') {
            endOffset++;
        }
        return srcPos(sourceText.getStartOffset(), endOffset);
    }
    return this;
}
Also used : BasedSequence(com.vladsch.flexmark.util.sequence.BasedSequence)

Example 30 with BasedSequence

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

the class AutolinkNodePostProcessor method process.

@Override
public void process(NodeTracker state, Node node) {
    BasedSequence original = node.getChars();
    ReplacedTextMapper textMapper = new ReplacedTextMapper(original);
    BasedSequence literal = Escaping.unescape(original, textMapper);
    Iterable<LinkSpan> links = linkExtractor.extractLinks(literal);
    int lastEscaped = 0;
    boolean wrapInTextBase = !(node.getParent() instanceof TextBase);
    TextBase textBase = wrapInTextBase ? null : (TextBase) node.getParent();
    for (LinkSpan link : links) {
        BasedSequence linkText = literal.subSequence(link.getBeginIndex(), link.getEndIndex()).trimEnd();
        int startOffset = textMapper.originalOffset(link.getBeginIndex());
        if (wrapInTextBase) {
            wrapInTextBase = false;
            textBase = new TextBase(original);
            node.insertBefore(textBase);
            state.nodeAdded(textBase);
        }
        if (startOffset != lastEscaped) {
            BasedSequence escapedChars = original.subSequence(lastEscaped, startOffset);
            Node node1 = new Text(escapedChars);
            textBase.appendChild(node1);
            state.nodeAdded(node1);
        }
        Text contentNode = new Text(linkText);
        LinkNode linkNode;
        if (link.getType() == LinkType.EMAIL) {
            linkNode = new MailLink();
            ((MailLink) linkNode).setText(linkText);
        } else {
            linkNode = new AutoLink();
            ((AutoLink) linkNode).setText(linkText);
        }
        linkNode.setCharsFromContent();
        linkNode.appendChild(contentNode);
        textBase.appendChild(linkNode);
        state.nodeAddedWithChildren(linkNode);
        lastEscaped = textMapper.originalOffset(link.getBeginIndex() + linkText.length());
    }
    if (lastEscaped > 0) {
        if (lastEscaped != original.length()) {
            BasedSequence escapedChars = original.subSequence(lastEscaped, original.length());
            Node node1 = new Text(escapedChars);
            textBase.appendChild(node1);
            state.nodeAdded(node1);
        }
        node.unlink();
        state.nodeRemoved(node);
    }
}
Also used : LinkSpan(org.nibor.autolink.LinkSpan) ReplacedTextMapper(com.vladsch.flexmark.util.sequence.ReplacedTextMapper) 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