Search in sources :

Example 1 with DefinitionItem

use of com.vladsch.flexmark.ext.definition.DefinitionItem in project flexmark-java by vsch.

the class DefinitionListBlockPreProcessor method preProcess.

@Override
public void preProcess(ParserState state, Block block) {
    Boolean blankLinesInAST = state.getProperties().get(BLANK_LINES_IN_AST);
    if (block instanceof DefinitionList) {
        // need to propagate loose/tight
        final DefinitionList definitionList = (DefinitionList) block;
        boolean isTight = definitionList.isTight();
        if (options.autoLoose && isTight) {
            for (Node child : definitionList.getChildren()) {
                if (child instanceof DefinitionItem) {
                    if (((DefinitionItem) child).isLoose()) {
                        isTight = false;
                        if (!blankLinesInAST)
                            break;
                    }
                    if (blankLinesInAST) {
                        // transfer its trailing blank lines to uppermost level
                        child.moveTrailingBlankLines();
                    }
                }
            }
            definitionList.setTight(isTight);
        }
    }
}
Also used : DefinitionList(com.vladsch.flexmark.ext.definition.DefinitionList) DefinitionItem(com.vladsch.flexmark.ext.definition.DefinitionItem) Node(com.vladsch.flexmark.ast.Node)

Example 2 with DefinitionItem

use of com.vladsch.flexmark.ext.definition.DefinitionItem 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)

Aggregations

DefinitionItem (com.vladsch.flexmark.ext.definition.DefinitionItem)2 DefinitionList (com.vladsch.flexmark.ext.definition.DefinitionList)2 Node (com.vladsch.flexmark.ast.Node)1 DefinitionTerm (com.vladsch.flexmark.ext.definition.DefinitionTerm)1 ParagraphParser (com.vladsch.flexmark.internal.ParagraphParser)1 BasedSequence (com.vladsch.flexmark.util.sequence.BasedSequence)1