Search in sources :

Example 1 with ReplacedTextMapper

use of com.vladsch.flexmark.util.sequence.ReplacedTextMapper 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 2 with ReplacedTextMapper

use of com.vladsch.flexmark.util.sequence.ReplacedTextMapper 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)

Example 3 with ReplacedTextMapper

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

the class AbbreviationNodePostProcessor method process.

@Override
public void process(NodeTracker state, Node node) {
    if (abbreviations == null)
        return;
    BasedSequence original = node.getChars();
    ReplacedTextMapper textMapper = new ReplacedTextMapper(original);
    BasedSequence literal = Escaping.unescape(original, textMapper);
    Matcher m = abbreviations.matcher(literal);
    int lastEscaped = 0;
    boolean wrapInTextBase = !(node.getParent() instanceof TextBase);
    TextBase textBase = wrapInTextBase ? null : (TextBase) node.getParent();
    while (m.find()) {
        // String found = m.group();
        if (abbreviationMap.containsKey(m.group(0))) {
            BasedSequence abbreviation = abbreviationMap.get(m.group(0));
            BasedSequence toDecorateText = literal.subSequence(m.start(0), m.end(0));
            int startOffset = textMapper.originalOffset(m.start(0));
            int endOffset = textMapper.originalOffset(m.end(0));
            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);
            }
            BasedSequence origToDecorateText = original.subSequence(startOffset, endOffset);
            Abbreviation decorationNode = new Abbreviation(origToDecorateText, abbreviation);
            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 : Abbreviation(com.vladsch.flexmark.ext.abbreviation.Abbreviation) ReplacedTextMapper(com.vladsch.flexmark.util.sequence.ReplacedTextMapper) Matcher(java.util.regex.Matcher) BasedSequence(com.vladsch.flexmark.util.sequence.BasedSequence)

Aggregations

BasedSequence (com.vladsch.flexmark.util.sequence.BasedSequence)3 ReplacedTextMapper (com.vladsch.flexmark.util.sequence.ReplacedTextMapper)3 Abbreviation (com.vladsch.flexmark.ext.abbreviation.Abbreviation)1 EscapedCharacter (com.vladsch.flexmark.ext.escaped.character.EscapedCharacter)1 ReplacedTextRegion (com.vladsch.flexmark.util.sequence.ReplacedTextRegion)1 Matcher (java.util.regex.Matcher)1 LinkSpan (org.nibor.autolink.LinkSpan)1