Search in sources :

Example 6 with BasedSequence

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

the class EscapedCharacterNodePostProcessor 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);
    int lastEscaped = 0;
    boolean wrapInTextBase = !(node.getParent() instanceof TextBase);
    TextBase textBase = wrapInTextBase ? null : (TextBase) node.getParent();
    ArrayList<ReplacedTextRegion> replacedRegions = textMapper.getRegions();
    for (ReplacedTextRegion region : replacedRegions) {
        int startOffset = region.getOriginalRange().getStart();
        int endOffset = region.getOriginalRange().getEnd();
        if (original.charAt(startOffset) == '\\' && region.getReplacedRange().length() == 1 && // fix for #19, ArrayIndexOutOfBounds while parsing markdown with backslash as last character of text block
        startOffset + 1 < original.length()) {
            if (wrapInTextBase) {
                wrapInTextBase = false;
                textBase = new TextBase(original);
                node.insertBefore(textBase);
                state.nodeAdded(textBase);
            }
            if (startOffset != lastEscaped) {
                if (startOffset > original.length() || lastEscaped > original.length()) {
                    int tmp = 0;
                }
                BasedSequence escapedChars = original.subSequence(lastEscaped, startOffset);
                Node node1 = new Text(escapedChars);
                textBase.appendChild(node1);
                state.nodeAdded(node1);
            }
            BasedSequence origToDecorateText = original.subSequence(startOffset, endOffset);
            BasedSequence text = origToDecorateText.subSequence(1);
            EscapedCharacter decorationNode = new EscapedCharacter(origToDecorateText.subSequence(0, 1), text);
            textBase.appendChild(decorationNode);
            // Text undecoratedTextNode = new Text(origToDecorateText);
            // decorationNode.appendChild(undecoratedTextNode);
            // state.nodeAddedWithChildren(decorationNode);
            state.nodeAdded(decorationNode);
            lastEscaped = endOffset;
        }
    }
    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 : ReplacedTextMapper(com.vladsch.flexmark.util.sequence.ReplacedTextMapper) ReplacedTextRegion(com.vladsch.flexmark.util.sequence.ReplacedTextRegion) BasedSequence(com.vladsch.flexmark.util.sequence.BasedSequence) EscapedCharacter(com.vladsch.flexmark.ext.escaped.character.EscapedCharacter)

Example 7 with BasedSequence

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

the class IParseBase method parseReader.

@Override
public Node parseReader(Reader input) throws IOException {
    BufferedReader bufferedReader;
    if (input instanceof BufferedReader) {
        bufferedReader = (BufferedReader) input;
    } else {
        bufferedReader = new BufferedReader(input);
    }
    StringBuilder file = new StringBuilder();
    char[] buffer = new char[16384];
    while (true) {
        int charsRead = bufferedReader.read(buffer);
        file.append(buffer, 0, charsRead);
        if (charsRead < buffer.length)
            break;
    }
    BasedSequence source = CharSubSequence.of(file.toString());
    return parse(source);
}
Also used : BasedSequence(com.vladsch.flexmark.util.sequence.BasedSequence) BufferedReader(java.io.BufferedReader)

Example 8 with BasedSequence

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

the class Document method getLineNumber.

public int getLineNumber(int offset) {
    if (lineSegments == EMPTY_LIST) {
        BasedSequence preText = getChars().baseSubSequence(0, Utils.maxLimit(offset, getChars().length()));
        if (preText.isEmpty())
            return 0;
        int lineNumber = 0;
        int nextLineEnd = preText.endOfLineAnyEOL(0);
        final int length = preText.length();
        while (nextLineEnd < length) {
            lineNumber++;
            nextLineEnd = preText.endOfLineAnyEOL(nextLineEnd + 1);
        }
        return lineNumber;
    } else {
        final int iMax = lineSegments.size();
        for (int i = 0; i < iMax; i++) {
            if (offset < lineSegments.get(i).getEndOffset()) {
                return i;
            }
        }
        return iMax;
    }
}
Also used : BasedSequence(com.vladsch.flexmark.util.sequence.BasedSequence)

Example 9 with BasedSequence

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

the class Node method setCharsFromContent.

public void setCharsFromContent() {
    BasedSequence[] segments = getSegments();
    BasedSequence spanningChars = null;
    if (segments.length > 0) {
        BasedSequence leadSegment = getLeadSegment(segments);
        BasedSequence trailSegment = getTrailSegment(segments);
        if (firstChild == null || lastChild == null) {
            BasedSequence[] sequences = new BasedSequence[] { leadSegment, trailSegment };
            spanningChars = spanningChars(sequences);
        } else {
            BasedSequence[] sequences = new BasedSequence[] { leadSegment, trailSegment, firstChild.chars, lastChild.chars };
            spanningChars = spanningChars(sequences);
        }
    } else if (firstChild != null && lastChild != null) {
        BasedSequence[] sequences = new BasedSequence[] { firstChild.chars, lastChild.chars };
        spanningChars = spanningChars(sequences);
    }
    if (spanningChars != null) {
        // see if these are greater than already assigned chars
        if (chars.isNull()) {
            setChars(spanningChars);
        } else {
            int start = Utils.min(chars.getStartOffset(), spanningChars.getStartOffset());
            int end = Utils.max(chars.getEndOffset(), spanningChars.getEndOffset());
            setChars(chars.baseSubSequence(start, end));
        }
    }
}
Also used : BasedSequence(com.vladsch.flexmark.util.sequence.BasedSequence)

Example 10 with BasedSequence

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

the class Node method spanningChars.

public static BasedSequence spanningChars(BasedSequence... segments) {
    int startOffset = Integer.MAX_VALUE;
    int endOffset = -1;
    BasedSequence firstSequence = null;
    BasedSequence lastSequence = null;
    for (BasedSequence segment : segments) {
        if (segment != null && segment != BasedSequence.NULL) {
            if (startOffset > segment.getStartOffset()) {
                startOffset = segment.getStartOffset();
                firstSequence = segment;
            }
            if (endOffset <= segment.getEndOffset()) {
                endOffset = segment.getEndOffset();
                lastSequence = segment;
            }
        }
    }
    if (firstSequence != null && lastSequence != null) {
        return firstSequence.baseSubSequence(firstSequence.getStartOffset(), lastSequence.getEndOffset());
    } else {
        return BasedSequence.NULL;
    }
}
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