Search in sources :

Example 1 with Document

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;
}
Also used : MutableDataHolder(com.vladsch.flexmark.util.options.MutableDataHolder) HashMap(java.util.HashMap) Node(com.vladsch.flexmark.ast.Node) HtmlRenderer(com.vladsch.flexmark.html.HtmlRenderer) JekyllTag(com.vladsch.flexmark.ext.jekyll.tag.JekyllTag) MutableDataSet(com.vladsch.flexmark.util.options.MutableDataSet) Document(com.vladsch.flexmark.ast.Document) Parser(com.vladsch.flexmark.parser.Parser)

Example 2 with 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(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);
}
Also used : DocumentParser(com.vladsch.flexmark.internal.DocumentParser) Document(com.vladsch.flexmark.ast.Document)

Example 3 with 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);
}
Also used : DocumentParser(com.vladsch.flexmark.internal.DocumentParser) Document(com.vladsch.flexmark.ast.Document)

Example 4 with 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);
}
Also used : DataHolder(com.vladsch.flexmark.util.options.DataHolder) MutableDataHolder(com.vladsch.flexmark.util.options.MutableDataHolder) HtmlRenderer(com.vladsch.flexmark.html.HtmlRenderer) Document(com.vladsch.flexmark.ast.Document) Parser(com.vladsch.flexmark.parser.Parser)

Example 5 with Document

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;
}
Also used : Node(com.vladsch.flexmark.ast.Node) Document(com.vladsch.flexmark.ast.Document)

Aggregations

Document (com.vladsch.flexmark.ast.Document)6 DocumentParser (com.vladsch.flexmark.internal.DocumentParser)3 Node (com.vladsch.flexmark.ast.Node)2 HtmlRenderer (com.vladsch.flexmark.html.HtmlRenderer)2 Parser (com.vladsch.flexmark.parser.Parser)2 MutableDataHolder (com.vladsch.flexmark.util.options.MutableDataHolder)2 JekyllTag (com.vladsch.flexmark.ext.jekyll.tag.JekyllTag)1 DataHolder (com.vladsch.flexmark.util.options.DataHolder)1 MutableDataSet (com.vladsch.flexmark.util.options.MutableDataSet)1 HashMap (java.util.HashMap)1