Search in sources :

Example 1 with BasedSequence

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

the class CoreNodeDocxRenderer method render.

private void render(final LinkRef node, final DocxRendererContext docx) {
    ResolvedLink resolvedLink = null;
    if (!node.isDefined() && recheckUndefinedReferences) {
        if (node.getReferenceNode(referenceRepository) != null) {
            node.setDefined(true);
        }
    }
    BasedSequence urlSrc = node.getReference();
    Reference reference = null;
    if (node.isDefined()) {
        reference = node.getReferenceNode(referenceRepository);
        urlSrc = reference.getUrl();
        String url = urlSrc.unescape();
        resolvedLink = docx.resolveLink(LinkType.LINK, url, null, null);
        if (reference.getTitle().isNotNull()) {
            resolvedLink.getNonNullAttributes().replaceValue(Attribute.TITLE_ATTR, reference.getTitle().unescape());
        } else {
            resolvedLink.getNonNullAttributes().remove(Attribute.TITLE_ATTR);
        }
    } else {
        // see if have reference resolver and this is resolved
        String normalizeRef = node.getReference().unescape();
        resolvedLink = docx.resolveLink(LinkType.LINK_REF, normalizeRef, null, null);
        if (resolvedLink.getStatus() == UNKNOWN) {
            resolvedLink = null;
        }
    }
    if (resolvedLink == null) {
        // empty ref, we treat it as text
        assert !node.isDefined();
        if (!node.hasChildren()) {
            docx.text(node.getChars().unescape());
        } else {
            docx.text(node.getChars().prefixOf(node.getChildChars()).unescape());
            docx.renderChildren(node);
            docx.text(node.getChars().suffixOf(node.getChildChars()).unescape());
        }
    } else {
        Attributes attributes = resolvedLink.getNonNullAttributes();
        if (reference != null) {
            attributes = docx.extendRenderingNodeAttributes(reference, AttributablePart.NODE, attributes);
        }
        attributes = docx.extendRenderingNodeAttributes(AttributablePart.NODE, attributes);
        renderURL(urlSrc, docx, resolvedLink.getUrl(), attributes, new Runnable() {

            @Override
            public void run() {
                docx.renderChildren(node);
            }
        });
    }
}
Also used : BasedSequence(com.vladsch.flexmark.util.sequence.BasedSequence) Attributes(com.vladsch.flexmark.util.html.Attributes) ResolvedLink(com.vladsch.flexmark.html.renderer.ResolvedLink)

Example 2 with BasedSequence

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

the class DefinitionItemBlockParser method parseItemMarker.

static ItemData parseItemMarker(DefinitionOptions options, ParserState state, boolean isTight) {
    BasedSequence line = state.getLine();
    int markerIndex = state.getNextNonSpaceIndex();
    int markerColumn = state.getColumn() + state.getIndent();
    int markerIndent = state.getIndent();
    BasedSequence rest = line.subSequence(markerIndex, line.length());
    final char c1 = rest.firstChar();
    if (!(c1 == ':' && options.colonMarker) && !(c1 == '~' && options.tildeMarker)) {
        return null;
    }
    // marker doesn't include tabs, so counting them as columns directly is ok
    // the column within the line where the content starts
    int contentOffset = 0;
    // See at which column the content starts if there is content
    boolean hasContent = false;
    for (int i = markerIndex + 1; i < line.length(); i++) {
        char c = line.charAt(i);
        if (c == '\t') {
            contentOffset += Parsing.columnsToNextTabStop(markerColumn + 1 + contentOffset);
        } else if (c == ' ') {
            contentOffset++;
        } else {
            hasContent = true;
            break;
        }
    }
    if (hasContent && contentOffset < options.markerSpaces) {
        return null;
    }
    if (!hasContent || options.myParserEmulationProfile == COMMONMARK && contentOffset > options.newItemCodeIndent) {
        // If this line is blank or has a code block, default to 1 space after marker
        contentOffset = 1;
    }
    return new ItemData(!hasContent, isTight, markerIndex, markerColumn, markerIndent, contentOffset, rest.subSequence(0, 1));
}
Also used : BasedSequence(com.vladsch.flexmark.util.sequence.BasedSequence)

Example 3 with BasedSequence

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

the class DefinitionListItemBlockPreProcessor method preProcess.

@Override
public void preProcess(ParserState state, Block block) {
    if (block instanceof DefinitionItem) {
        // we chop up the previous paragraph into definition terms and add the definition item to the last one
        // we add all these to the previous DefinitionList or add a new one if there isn't one
        final DefinitionItem definitionItem = (DefinitionItem) block;
        final Node previous = block.getPreviousAnyNot(BlankLine.class);
        if (previous instanceof Paragraph) {
            final Paragraph paragraph = (Paragraph) previous;
            Node afterParagraph = previous.getNext();
            Node paragraphPrevious = paragraph.getPreviousAnyNot(BlankLine.class);
            final Node paragraphParent = paragraph.getParent();
            definitionItem.unlink();
            paragraph.unlink();
            state.blockRemovedWithChildren(paragraph);
            final boolean hadPreviousList;
            if (options.doubleBlankLineBreaksList) {
                // intervening characters between previous paragraph and definition terms
                final BasedSequence interSpace = paragraphPrevious == null ? BasedSequence.NULL : BasedSequenceImpl.of(paragraphPrevious.getChars().baseSubSequence(paragraphPrevious.getChars().getEndOffset(), paragraph.getChars().getStartOffset()).normalizeEOL());
                hadPreviousList = paragraphPrevious instanceof DefinitionList && interSpace.countChars('\n') < 2;
            } else {
                hadPreviousList = paragraphPrevious instanceof DefinitionList;
            }
            final DefinitionList definitionList = new DefinitionList();
            definitionList.setTight(true);
            final List<BasedSequence> lines = paragraph.getContentLines();
            DefinitionTerm definitionTerm = null;
            int lineIndex = 0;
            for (BasedSequence line : lines) {
                definitionTerm = new DefinitionTerm();
                ParagraphParser parser = new ParagraphParser();
                BlockContent content = new BlockContent();
                content.add(line, paragraph.getLineIndent(lineIndex++));
                parser.getBlock().setContent(content);
                parser.getBlock().setCharsFromContent();
                definitionTerm.appendChild(parser.getBlock());
                definitionTerm.setCharsFromContent();
                state.blockParserAdded(parser);
                definitionList.appendChild(definitionTerm);
                state.blockAdded(definitionTerm);
            }
            // if have blank lines after paragraph need to move them after the last term
            if (state.getProperties().get(BLANK_LINES_IN_AST) && afterParagraph instanceof BlankLine) {
                while (afterParagraph instanceof BlankLine) {
                    Node next = afterParagraph.getNext();
                    afterParagraph.unlink();
                    definitionList.appendChild(afterParagraph);
                    afterParagraph = next;
                }
            }
            definitionList.appendChild(definitionItem);
            if (hadPreviousList) {
                final DefinitionList previousList = (DefinitionList) paragraphPrevious;
                previousList.takeChildren(definitionList);
                for (Node node : definitionList.getChildren()) {
                    node.unlink();
                    previousList.appendChild(node);
                    state.blockAddedWithChildren((Block) node);
                }
                previousList.setCharsFromContent();
            } else {
                // insert new one, after paragraphPrevious
                if (paragraphPrevious != null) {
                    paragraphPrevious.insertAfter(definitionList);
                } else {
                    if (paragraphParent.getFirstChild() != null) {
                        paragraphParent.getFirstChild().insertBefore(definitionList);
                    } else {
                        paragraphParent.appendChild(definitionList);
                    }
                }
                definitionList.setCharsFromContent();
                state.blockAddedWithChildren(definitionList);
            }
        } else if (previous instanceof DefinitionList) {
            final DefinitionList previousList = (DefinitionList) previous;
            definitionItem.unlink();
            previousList.appendChild(definitionItem);
            previousList.setCharsFromContent();
        }
    }
}
Also used : DefinitionList(com.vladsch.flexmark.ext.definition.DefinitionList) DefinitionItem(com.vladsch.flexmark.ext.definition.DefinitionItem) BasedSequence(com.vladsch.flexmark.util.sequence.BasedSequence) ParagraphParser(com.vladsch.flexmark.internal.ParagraphParser) DefinitionTerm(com.vladsch.flexmark.ext.definition.DefinitionTerm)

Example 4 with BasedSequence

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

the class DefinitionNodeFormatter method render.

private void render(final DefinitionItem node, final NodeFormatterContext context, final MarkdownWriter markdown) {
    BasedSequence openMarkerChars = node.getChars().prefixOf(node.getFirstChild().getChars());
    BasedSequence openMarker = openMarkerChars.subSequence(0, 1);
    BasedSequence openMarkerSpaces = openMarkerChars.subSequence(1);
    if (options.markerSpaces >= 1 && openMarkerSpaces.length() != options.markerSpaces) {
        openMarkerSpaces = SubSequence.of(RepeatedCharSequence.of(' ', options.markerSpaces));
    }
    switch(options.markerType) {
        case ANY:
            break;
        case COLON:
            openMarker = SubSequence.of(":");
            break;
        case TILDE:
            openMarker = SubSequence.of("~");
            break;
    }
    markdown.line().append(openMarker).append(openMarkerSpaces);
    markdown.pushPrefix().addPrefix(RepeatedCharSequence.of(' ', context.getFormatterOptions().itemContentIndent ? openMarker.length() + openMarkerSpaces.length() : listOptions.getItemIndent()));
    context.renderChildren(node);
    markdown.popPrefix();
}
Also used : BasedSequence(com.vladsch.flexmark.util.sequence.BasedSequence)

Example 5 with BasedSequence

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

the class FootnoteLinkRefProcessor method createNode.

@Override
public Node createNode(BasedSequence nodeChars) {
    BasedSequence footnoteId = nodeChars.midSequence(2, -1).trim();
    FootnoteBlock footnoteBlock = footnoteId.length() > 0 ? footnoteRepository.get(footnoteId.toString()) : null;
    Footnote footnote = new Footnote(nodeChars.subSequence(0, 2), footnoteId, nodeChars.endSequence(1));
    footnote.setFootnoteBlock(footnoteBlock);
    if (footnoteBlock != null) {
        footnoteRepository.addFootnoteReference(footnoteBlock, footnote);
    }
    return footnote;
}
Also used : Footnote(com.vladsch.flexmark.ext.footnotes.Footnote) BasedSequence(com.vladsch.flexmark.util.sequence.BasedSequence) FootnoteBlock(com.vladsch.flexmark.ext.footnotes.FootnoteBlock)

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