Search in sources :

Example 61 with Node

use of com.vladsch.flexmark.ast.Node in project flexmark-java by vsch.

the class TextCollectingVisitorTest method test_basic.

@Test
public void test_basic() {
    DataHolder options = new MutableDataSet().set(Parser.EXTENSIONS, Arrays.asList(TablesExtension.create()));
    Parser parser = Parser.builder(options).build();
    String markdown = "| First Header  | Second Header |\n" + "| ------------- | ------------- |\n" + "| Content Cell  | Content Cell  |\n" + "\n" + "| Left-aligned | Center-aligned | Right-aligned |\n" + "| :---         |     :---:      |          ---: |\n" + "| git status   | git status     | git status    |\n" + "| git diff     | git diff       | git diff      |\n" + "";
    Node document = parser.parse(markdown);
    TextCollectingVisitor collectingVisitor = new TextCollectingVisitor();
    final String text = collectingVisitor.collectAndGetText(document);
    System.out.println(text);
    final String astText = new AstCollectingVisitor().collectAndGetAstText(document);
    System.out.println(astText);
}
Also used : TextCollectingVisitor(com.vladsch.flexmark.ast.util.TextCollectingVisitor) DataHolder(com.vladsch.flexmark.util.options.DataHolder) AstCollectingVisitor(com.vladsch.flexmark.test.AstCollectingVisitor) Node(com.vladsch.flexmark.ast.Node) MutableDataSet(com.vladsch.flexmark.util.options.MutableDataSet) Parser(com.vladsch.flexmark.parser.Parser) Test(org.junit.Test)

Example 62 with Node

use of com.vladsch.flexmark.ast.Node in project flexmark-java by vsch.

the class RenderingTestCase method assertAst.

// protected void assertAst(String source, String expectedAst) {
// assertAst(source, expectedAst, null);
// }
protected void assertAst(String source, String expectedAst, String optionsSet) {
    DataHolder options = optionsSet == null ? null : getOptions(example(), optionsSet);
    String parseSource = source;
    if (options != null && options.get(NO_FILE_EOL)) {
        parseSource = DumpSpecReader.trimTrailingEOL(parseSource);
    }
    Node node = parser().withOptions(options).parse(parseSource);
    String ast = ast(node);
    actualAst(ast, optionsSet);
    String expected;
    String actual;
    if (example() != null && example().getSection() != null) {
        StringBuilder outExpected = new StringBuilder();
        DumpSpecReader.addSpecExample(outExpected, source, "", expectedAst, optionsSet, true, example().getSection(), example().getExampleNumber());
        expected = outExpected.toString();
        StringBuilder outActual = new StringBuilder();
        DumpSpecReader.addSpecExample(outActual, source, "", ast, optionsSet, true, example().getSection(), example().getExampleNumber());
        actual = outActual.toString();
    } else {
        expected = DumpSpecReader.addSpecExample(source, "", expectedAst, optionsSet);
        actual = DumpSpecReader.addSpecExample(source, "", ast, optionsSet);
    }
    specExample(expected, actual, optionsSet);
    if (options != null && options.get(FAIL))
        thrown.expect(ComparisonFailure.class);
    assertEquals(expected, actual);
}
Also used : DataHolder(com.vladsch.flexmark.util.options.DataHolder) ComparisonFailure(org.junit.ComparisonFailure) Node(com.vladsch.flexmark.ast.Node)

Example 63 with Node

use of com.vladsch.flexmark.ast.Node in project flexmark-java by vsch.

the class RenderingTestCase method assertRenderingAst.

// protected void assertRenderingAst(String source, String expectedHtml, String expectedAst) {
// assertRenderingAst(source, expectedHtml, expectedAst, null);
// }
protected void assertRenderingAst(String source, String expectedHtml, String expectedAst, String optionsSet) {
    // assert options != null || optionsSet == null || optionsSet.isEmpty() : "Non empty optionsSet without any option customizations";
    DataHolder options = optionsSet == null ? null : getOptions(example(), optionsSet);
    String parseSource = source;
    if (options != null && options.get(NO_FILE_EOL)) {
        parseSource = DumpSpecReader.trimTrailingEOL(parseSource);
    }
    Node node = parser().withOptions(options).parse(parseSource);
    String html = renderer().withOptions(options).render(node);
    testCase(node, options);
    actualHtml(html, optionsSet);
    String ast = ast(node);
    actualAst(ast, optionsSet);
    boolean useActualHtml = useActualHtml();
    // include source for better assertion errors
    String expected;
    String actual;
    if (example() != null && example().getSection() != null) {
        StringBuilder outExpected = new StringBuilder();
        DumpSpecReader.addSpecExample(outExpected, source, expectedHtml, expectedAst, optionsSet, true, example().getSection(), example().getExampleNumber());
        expected = outExpected.toString();
        StringBuilder outActual = new StringBuilder();
        DumpSpecReader.addSpecExample(outActual, source, useActualHtml ? html : expectedHtml, ast, optionsSet, true, example().getSection(), example().getExampleNumber());
        actual = outActual.toString();
    } else {
        expected = DumpSpecReader.addSpecExample(source, expectedHtml, expectedAst, optionsSet);
        actual = DumpSpecReader.addSpecExample(source, useActualHtml ? html : expectedHtml, ast, optionsSet);
    }
    specExample(expected, actual, optionsSet);
    if (options != null && options.get(FAIL)) {
        thrown.expect(ComparisonFailure.class);
    }
    assertEquals(expected, actual);
}
Also used : DataHolder(com.vladsch.flexmark.util.options.DataHolder) Node(com.vladsch.flexmark.ast.Node)

Example 64 with Node

use of com.vladsch.flexmark.ast.Node in project flexmark-java by vsch.

the class NodeClassifierVisitor method updateNodeAncestry.

boolean updateNodeAncestry(Node node, CopyOnWriteRef<BitSet> nodeAncestryBitSet) {
    Node parent = node.getParent();
    if (!myExclusionMap.isEmpty() && !(node instanceof Document)) {
        // add flags if needed
        node.getClass();
        BitSet bitSet = nodeAncestryBitSet.getPeek();
        int index = myClassifyingNodeTracker.getItems().indexOf(node);
        if (index == -1) {
            throw new IllegalStateException("Node: " + node + " is not tracked, some post processor forgot to call tracker.nodeAdded().");
        }
        if (myExclusionSet != null && !myExclusionSet.isEmpty()) {
            Iterator<Class<?>> iterator = ((Set<Class<?>>) myExclusionSet).iterator();
            while (iterator.hasNext()) {
                Class<?> nodeType = iterator.next();
                if (nodeType.isInstance(node)) {
                    // get the index of this exclusion
                    int i = myExclusionSet.indexOf(nodeType);
                    assert i != -1;
                    if (!bitSet.get(i) && !nodeAncestryBitSet.isMutable()) {
                        bitSet = nodeAncestryBitSet.getMutable();
                        bitSet.set(i);
                    }
                }
            }
        }
        if (myClassificationDone && myNodeAncestryBitSetStack.size() > 1) {
            // see if we can stop
            // now store the stuff for the node index
            BitSet oldBitSet = myNodeAncestryMap.get(index);
            if (oldBitSet != null && oldBitSet.equals(bitSet)) {
                // no need to process descendants of this node
                return false;
            }
        }
        if (!bitSet.isEmpty()) {
            myNodeAncestryMap.put(index, nodeAncestryBitSet.getImmutable());
        }
    }
    return true;
}
Also used : Node(com.vladsch.flexmark.ast.Node) Document(com.vladsch.flexmark.ast.Document)

Example 65 with Node

use of com.vladsch.flexmark.ast.Node in project flexmark-java by vsch.

the class EnumeratedReferenceNodePostProcessor method process.

@Override
public void process(NodeTracker state, Node node) {
    if (node instanceof AttributesNode) {
        AttributesNode attributesNode = (AttributesNode) node;
        for (Node attributeNode : attributesNode.getChildren()) {
            if (attributeNode instanceof AttributeNode) {
                if (((AttributeNode) attributeNode).isId()) {
                    final String text = ((AttributeNode) attributeNode).getValue().toString();
                    enumeratedReferences.add(text);
                }
            }
        }
    }
}
Also used : AttributeNode(com.vladsch.flexmark.ext.attributes.AttributeNode) Node(com.vladsch.flexmark.ast.Node) AttributesNode(com.vladsch.flexmark.ext.attributes.AttributesNode) AttributeNode(com.vladsch.flexmark.ext.attributes.AttributeNode) AttributesNode(com.vladsch.flexmark.ext.attributes.AttributesNode)

Aggregations

Node (com.vladsch.flexmark.ast.Node)87 Parser (com.vladsch.flexmark.parser.Parser)29 HtmlRenderer (com.vladsch.flexmark.html.HtmlRenderer)21 Test (org.junit.Test)19 MutableDataSet (com.vladsch.flexmark.util.options.MutableDataSet)14 DataHolder (com.vladsch.flexmark.util.options.DataHolder)11 List (java.util.List)8 BasedSequence (com.vladsch.flexmark.util.sequence.BasedSequence)6 MutableDataHolder (com.vladsch.flexmark.util.options.MutableDataHolder)5 Text (com.vladsch.flexmark.ast.Text)3 TextCollectingVisitor (com.vladsch.flexmark.ast.util.TextCollectingVisitor)3 Extension (com.vladsch.flexmark.Extension)2 Block (com.vladsch.flexmark.ast.Block)2 CustomNode (com.vladsch.flexmark.ast.CustomNode)2 Document (com.vladsch.flexmark.ast.Document)2 Heading (com.vladsch.flexmark.ast.Heading)2 DocxRenderer (com.vladsch.flexmark.docx.converter.internal.DocxRenderer)2 MacroClose (com.vladsch.flexmark.ext.xwiki.macros.MacroClose)2 AstCollectingVisitor (com.vladsch.flexmark.test.AstCollectingVisitor)2 Attributes (com.vladsch.flexmark.util.html.Attributes)2