Search in sources :

Example 1 with JekyllTag

use of com.vladsch.flexmark.ext.jekyll.tag.JekyllTag 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 JekyllTag

use of com.vladsch.flexmark.ext.jekyll.tag.JekyllTag in project flexmark-java by vsch.

the class JekyllTagInlineParserExtension method parse.

@Override
public boolean parse(final InlineParser inlineParser) {
    if (inlineParser.peek(1) == '%' && (inlineParser.peek(2) == ' ' || inlineParser.peek(2) == '\t')) {
        BasedSequence input = inlineParser.getInput();
        Matcher matcher = inlineParser.matcher(parsing.MACRO_TAG);
        if (matcher != null) {
            BasedSequence tag = input.subSequence(matcher.start(), matcher.end());
            BasedSequence tagName = input.subSequence(matcher.start(1), matcher.end(1));
            BasedSequence parameters = input.subSequence(matcher.end(1), matcher.end() - 2).trim();
            JekyllTag macro = new JekyllTag(tag.subSequence(0, 2), tagName, parameters, tag.endSequence(2));
            macro.setCharsFromContent();
            if (!listIncludesOnly || tagName.equals("includes")) {
                List<JekyllTag> tagList = JekyllTagExtension.TAG_LIST.getFrom(inlineParser.getDocument());
                tagList.add(macro);
            }
            inlineParser.flushTextNode();
            inlineParser.getBlock().appendChild(macro);
            return true;
        }
    }
    return false;
}
Also used : Matcher(java.util.regex.Matcher) BasedSequence(com.vladsch.flexmark.util.sequence.BasedSequence) JekyllTag(com.vladsch.flexmark.ext.jekyll.tag.JekyllTag)

Aggregations

JekyllTag (com.vladsch.flexmark.ext.jekyll.tag.JekyllTag)2 Document (com.vladsch.flexmark.ast.Document)1 Node (com.vladsch.flexmark.ast.Node)1 HtmlRenderer (com.vladsch.flexmark.html.HtmlRenderer)1 Parser (com.vladsch.flexmark.parser.Parser)1 MutableDataHolder (com.vladsch.flexmark.util.options.MutableDataHolder)1 MutableDataSet (com.vladsch.flexmark.util.options.MutableDataSet)1 BasedSequence (com.vladsch.flexmark.util.sequence.BasedSequence)1 HashMap (java.util.HashMap)1 Matcher (java.util.regex.Matcher)1