Search in sources :

Example 1 with Parser

use of com.vladsch.flexmark.parser.Parser 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 Parser

use of com.vladsch.flexmark.parser.Parser 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 Parser

use of com.vladsch.flexmark.parser.Parser in project flexmark-java by vsch.

the class DocxConverterCommonMark method main.

public static void main(String[] args) {
    final String markdown = "### header\n" + "1. List item started from 1\n" + "### header \n" + "1. List item started from 1\n" + "1. List item continued 2\n" + "### test\n" + "1. List item started from 1\n" + "";
    System.out.println("markdown\n");
    System.out.println(markdown);
    final Parser PARSER = Parser.builder(OPTIONS).build();
    final DocxRenderer RENDERER = DocxRenderer.builder(OPTIONS).build();
    Node document = PARSER.parse(markdown);
    // to get XML
    String xml = RENDERER.render(document);
    // or to control the package
    final WordprocessingMLPackage template = DocxRenderer.getDefaultTemplate();
    RENDERER.render(document, template);
    File file = new File("/Users/vlad/src/pdf/flexmark-java-issue-176.docx");
    try {
        template.save(file, Docx4J.FLAG_SAVE_ZIP_FILE);
    } catch (Docx4JException e) {
        e.printStackTrace();
    }
}
Also used : Node(com.vladsch.flexmark.ast.Node) DocxRenderer(com.vladsch.flexmark.docx.converter.internal.DocxRenderer) WordprocessingMLPackage(org.docx4j.openpackaging.packages.WordprocessingMLPackage) File(java.io.File) Docx4JException(org.docx4j.openpackaging.exceptions.Docx4JException) Parser(com.vladsch.flexmark.parser.Parser)

Example 4 with Parser

use of com.vladsch.flexmark.parser.Parser in project flexmark-java by vsch.

the class FormatConverterCommonMark method main.

public static void main(String[] args) {
    final String markdown = "Text\n" + "\n" + "1. numbered list one\n" + " - unnumbered list\n" + " unnumbered list cont. same line\n" + " - unnumbered list  \n" + " unnumbered list cont. next line\n" + "\n" + " numbered list one cont. after unnumbered list" + "";
    System.out.println("\nMarkdown: --------------------------------------------------------------------------------\n");
    System.out.println(markdown);
    System.out.println("\n--------------------------------------------------------------------------------\n");
    final Parser PARSER = Parser.builder(OPTIONS).build();
    final Formatter RENDERER = Formatter.builder(OPTIONS).build();
    final Formatter RENDERER_FIXED4 = Formatter.builder(FORMATTER_OPTIONS).build();
    Node document = PARSER.parse(markdown);
    System.out.println(new AstCollectingVisitor().collectAndGetAstText(document));
    System.out.println("\n--------------------------------------------------------------------------------\n");
    String formatted = RENDERER.render(document);
    // or to control the package
    System.out.println("\nFormatted as is: --------------------------------------------------------------------------------\n");
    System.out.println(formatted);
    System.out.println("\n--------------------------------------------------------------------------------\n");
    String formattedFixed4 = RENDERER_FIXED4.render(document);
    // or to control the package
    System.out.println("\nFormatted fixed 4: --------------------------------------------------------------------------------\n");
    System.out.println(formattedFixed4);
    System.out.println("\n--------------------------------------------------------------------------------\n");
}
Also used : Formatter(com.vladsch.flexmark.formatter.internal.Formatter) AstCollectingVisitor(com.vladsch.flexmark.test.AstCollectingVisitor) Node(com.vladsch.flexmark.ast.Node) Parser(com.vladsch.flexmark.parser.Parser)

Example 5 with Parser

use of com.vladsch.flexmark.parser.Parser 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)

Aggregations

Parser (com.vladsch.flexmark.parser.Parser)44 Node (com.vladsch.flexmark.ast.Node)29 HtmlRenderer (com.vladsch.flexmark.html.HtmlRenderer)27 Test (org.junit.Test)18 MutableDataSet (com.vladsch.flexmark.util.options.MutableDataSet)17 MutableDataHolder (com.vladsch.flexmark.util.options.MutableDataHolder)8 Extension (com.vladsch.flexmark.Extension)2 Document (com.vladsch.flexmark.ast.Document)2 DocxRenderer (com.vladsch.flexmark.docx.converter.internal.DocxRenderer)2 AstCollectingVisitor (com.vladsch.flexmark.test.AstCollectingVisitor)2 DataHolder (com.vladsch.flexmark.util.options.DataHolder)2 File (java.io.File)2 Docx4JException (org.docx4j.openpackaging.exceptions.Docx4JException)2 WordprocessingMLPackage (org.docx4j.openpackaging.packages.WordprocessingMLPackage)2 TextCollectingVisitor (com.vladsch.flexmark.ast.util.TextCollectingVisitor)1 AutolinkExtension (com.vladsch.flexmark.ext.autolink.AutolinkExtension)1 JekyllTag (com.vladsch.flexmark.ext.jekyll.tag.JekyllTag)1 CustomNodeFormatter (com.vladsch.flexmark.formatter.CustomNodeFormatter)1 Formatter (com.vladsch.flexmark.formatter.internal.Formatter)1 InlineParser (com.vladsch.flexmark.parser.InlineParser)1