use of com.vladsch.flexmark.ast.Document in project flexmark-java by vsch.
the class JekyllIncludeFileSample method commonMark.
static String commonMark(String markdown, Map<String, String> included) {
MutableDataHolder options = new MutableDataSet();
options.set(Parser.EXTENSIONS, Arrays.asList(AutolinkExtension.create(), JekyllTagExtension.create()));
// change soft break to hard break
options.set(HtmlRenderer.SOFT_BREAK, "<br/>");
Parser parser = Parser.builder(options).build();
HtmlRenderer renderer = HtmlRenderer.builder(options).build();
Node document = parser.parse(markdown);
// see if document has includes
if (document instanceof Document) {
Document doc = (Document) document;
if (doc.contains(JekyllTagExtension.TAG_LIST)) {
List<JekyllTag> tagList = JekyllTagExtension.TAG_LIST.getFrom(doc);
Map<String, String> includeHtmlMap = new HashMap<String, String>();
for (JekyllTag tag : tagList) {
String includeFile = tag.getParameters().toString();
if (tag.getTag().equals("include") && !includeFile.isEmpty() && !includeHtmlMap.containsKey(includeFile)) {
// see if it exists
if (included.containsKey(includeFile)) {
// have the file
String text = included.get(includeFile);
if (includeFile.endsWith(".md")) {
Node includeDoc = parser.parse(text);
String includeHtml = renderer.render(includeDoc);
includeHtmlMap.put(includeFile, includeHtml);
if (includeDoc instanceof Document) {
// copy any definition of reference elements from included file to our document
parser.transferReferences(doc, (Document) includeDoc);
}
} else {
includeHtmlMap.put(includeFile, text);
}
}
}
if (!includeHtmlMap.isEmpty()) {
doc.set(JekyllTagExtension.INCLUDED_HTML, includeHtmlMap);
}
}
}
}
final String html = renderer.render(document);
return html;
}
use of com.vladsch.flexmark.ast.Document in project flexmark-java by vsch.
the class Parser method parse.
/**
* Parse the specified input text into a tree of nodes.
* <p>
* Note that this method is thread-safe (a new parser state is used for each invocation).
*
* @param input the text to parse
* @return the root node
*/
public Document parse(BasedSequence input) {
DocumentParser documentParser = new DocumentParser(options, blockParserFactories, paragraphPreProcessorFactories, blockPreProcessorDependencies, inlineParserFactory.inlineParser(options, specialCharacters, delimiterCharacters, delimiterProcessors, linkRefProcessors, inlineParserExtensionFactories));
Document document = documentParser.parse(input);
return postProcess(document);
}
use of com.vladsch.flexmark.ast.Document in project flexmark-java by vsch.
the class Parser method parse.
/**
* Parse the specified input text into a tree of nodes.
* <p>
* Note that this method is thread-safe (a new parser state is used for each invocation).
*
* @param input the text to parse
* @return the root node
*/
public Document parse(String input) {
DocumentParser documentParser = new DocumentParser(options, blockParserFactories, paragraphPreProcessorFactories, blockPreProcessorDependencies, inlineParserFactory.inlineParser(options, specialCharacters, delimiterCharacters, delimiterProcessors, linkRefProcessors, inlineParserExtensionFactories));
Document document = documentParser.parse(CharSubSequence.of(input));
return postProcess(document);
}
use of com.vladsch.flexmark.ast.Document in project flexmark-java by vsch.
the class CustomContextDataSample method main.
public static void main(String[] args) {
final DataHolder options = PegdownOptionsAdapter.flexmarkOptions(Extensions.ALL, CustomExtension.create());
Parser parser = Parser.builder(options).build();
HtmlRenderer renderer = HtmlRenderer.builder(options).build();
String markdown = "";
XhtmlContent xhtmlContent = null;
// You can re-use parser and renderer instances
Document document = (Document) parser.parse(markdown);
document.set(XHTML_CONTENT, xhtmlContent);
// "<p>This is <em>Sparta</em></p>\n"
String html = renderer.render(document);
System.out.println(html);
}
use of com.vladsch.flexmark.ast.Document 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;
}
Aggregations