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);
}
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;
}
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();
}
}
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");
}
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;
}
Aggregations