Search in sources :

Example 1 with HtmlRenderer

use of com.vladsch.flexmark.html.HtmlRenderer in project summer-mis by cn-cerc.

the class MarkdownDoc method mdToHtml.

/**
 * @param inputText
 *            传入的md字符串
 * @return 返回经转化后的html
 */
public String mdToHtml(String inputText) {
    MutableDataSet options = new MutableDataSet();
    // 使用github的markdown扩展语法
    options.setFrom(ParserEmulationProfile.GITHUB_DOC);
    Parser parser = Parser.builder(options).build();
    HtmlRenderer renderer = HtmlRenderer.builder(options).build();
    Node document = parser.parse(inputText);
    return renderer.render(document);
}
Also used : Node(com.vladsch.flexmark.ast.Node) HtmlRenderer(com.vladsch.flexmark.html.HtmlRenderer) MutableDataSet(com.vladsch.flexmark.util.options.MutableDataSet) Parser(com.vladsch.flexmark.parser.Parser)

Example 2 with HtmlRenderer

use of com.vladsch.flexmark.html.HtmlRenderer in project commons by terran4j.

the class DocPageBuilder method md2Html.

public String md2Html(String content) throws Exception {
    MutableDataSet options = new MutableDataSet();
    options.setFrom(ParserEmulationProfile.GITHUB_DOC);
    // 
    options.set(// 
    Parser.EXTENSIONS, Arrays.asList(TablesExtension.create()));
    // References compatibility
    options.set(Parser.REFERENCES_KEEP, KeepType.LAST);
    // Set GFM table parsing options
    // 
    options.set(TablesExtension.COLUMN_SPANS, false).set(TablesExtension.MIN_HEADER_ROWS, // 
    1).set(TablesExtension.MAX_HEADER_ROWS, // 
    1).set(TablesExtension.APPEND_MISSING_COLUMNS, // 
    true).set(TablesExtension.DISCARD_EXTRA_COLUMNS, // 
    true).set(TablesExtension.WITH_CAPTION, // 
    false).set(TablesExtension.HEADER_SEPARATOR_COLUMN_MATCH, true);
    // Setup List Options for GitHub profile which is kramdown for documents
    options.setFrom(ParserEmulationProfile.GITHUB_DOC);
    Parser parser = Parser.builder(options).build();
    HtmlRenderer renderer = HtmlRenderer.builder(options).build();
    // You can re-use parser and renderer instances
    Node document = parser.parse(content);
    String html = renderer.render(document);
    return html;
}
Also used : Node(com.vladsch.flexmark.ast.Node) HtmlRenderer(com.vladsch.flexmark.html.HtmlRenderer) MutableDataSet(com.vladsch.flexmark.util.options.MutableDataSet) Parser(com.vladsch.flexmark.parser.Parser)

Example 3 with HtmlRenderer

use of com.vladsch.flexmark.html.HtmlRenderer 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 4 with HtmlRenderer

use of com.vladsch.flexmark.html.HtmlRenderer in project flexmark-java by vsch.

the class NodeRendererSample method commonMark.

static String commonMark(String markdown) {
    MutableDataHolder options = new MutableDataSet();
    options.set(Parser.EXTENSIONS, Arrays.asList(AutolinkExtension.create(), SampleExtension.create()));
    // change soft break to hard break
    options.set(HtmlRenderer.SOFT_BREAK, "<br/>");
    options.set(HtmlRenderer.ESCAPE_HTML, true);
    Parser parser = Parser.builder(options).build();
    Node document = parser.parse(markdown);
    HtmlRenderer renderer = HtmlRenderer.builder(options).build();
    final String html = renderer.render(document);
    return html;
}
Also used : MutableDataHolder(com.vladsch.flexmark.util.options.MutableDataHolder) HtmlRenderer(com.vladsch.flexmark.html.HtmlRenderer) MutableDataSet(com.vladsch.flexmark.util.options.MutableDataSet) Parser(com.vladsch.flexmark.parser.Parser)

Example 5 with HtmlRenderer

use of com.vladsch.flexmark.html.HtmlRenderer in project flexmark-java by vsch.

the class PdfConverter method main.

public static void main(String[] args) {
    final String pegdown = "#Heading\n" + "-----\n" + "paragraph text \n" + "lazy continuation\n" + "\n" + "* list item\n" + "    > block quote\n" + "    lazy continuation\n" + "\n" + "~~~info\n" + "   with uneven indent\n" + "      with uneven indent\n" + "indented code\n" + "~~~ \n" + "\n" + "       with uneven indent\n" + "          with uneven indent\n" + "    indented code\n" + "1. numbered item 1   \n" + "1. numbered item 2   \n" + "1. numbered item 3   \n" + "    - bullet item 1   \n" + "    - bullet item 2   \n" + "    - bullet item 3   \n" + "        1. numbered sub-item 1   \n" + "        1. numbered sub-item 2   \n" + "        1. numbered sub-item 3   \n" + "    \n" + "    ~~~info\n" + "       with uneven indent\n" + "          with uneven indent\n" + "    indented code\n" + "    ~~~ \n" + "    \n" + "           with uneven indent\n" + "              with uneven indent\n" + "        indented code\n" + "";
    System.out.println("pegdown\n");
    System.out.println(pegdown);
    final Parser PARSER = Parser.builder(OPTIONS).build();
    final HtmlRenderer RENDERER = HtmlRenderer.builder(OPTIONS).build();
    Node document = PARSER.parse(pegdown);
    String html = RENDERER.render(document);
    PdfConverterExtension.exportToPdf("/Users/vlad/src/pdf/flexmark-java.pdf", html, "", OPTIONS);
}
Also used : Node(com.vladsch.flexmark.ast.Node) HtmlRenderer(com.vladsch.flexmark.html.HtmlRenderer) Parser(com.vladsch.flexmark.parser.Parser)

Aggregations

HtmlRenderer (com.vladsch.flexmark.html.HtmlRenderer)30 Parser (com.vladsch.flexmark.parser.Parser)28 Node (com.vladsch.flexmark.ast.Node)21 MutableDataSet (com.vladsch.flexmark.util.options.MutableDataSet)16 MutableDataHolder (com.vladsch.flexmark.util.options.MutableDataHolder)7 Test (org.junit.Test)6 Extension (com.vladsch.flexmark.Extension)2 Document (com.vladsch.flexmark.ast.Document)2 InputStreamReader (java.io.InputStreamReader)2 AutolinkExtension (com.vladsch.flexmark.ext.autolink.AutolinkExtension)1 JekyllTag (com.vladsch.flexmark.ext.jekyll.tag.JekyllTag)1 CustomNodeFormatter (com.vladsch.flexmark.formatter.CustomNodeFormatter)1 AttributeProvider (com.vladsch.flexmark.html.AttributeProvider)1 AttributeProviderFactory (com.vladsch.flexmark.html.AttributeProviderFactory)1 IndependentAttributeProviderFactory (com.vladsch.flexmark.html.IndependentAttributeProviderFactory)1 AttributablePart (com.vladsch.flexmark.html.renderer.AttributablePart)1 LinkResolverContext (com.vladsch.flexmark.html.renderer.LinkResolverContext)1 Node (com.vladsch.flexmark.util.ast.Node)1 Attributes (com.vladsch.flexmark.util.html.Attributes)1 DataHolder (com.vladsch.flexmark.util.options.DataHolder)1