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);
}
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);
}
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);
}
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;
}
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);
}
}
}
}
}
Aggregations