Java Code Examples for org.commonmark.parser.Parser#parse()
The following examples show how to use
org.commonmark.parser.Parser#parse() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MockBodyPost.java From Natty with GNU General Public License v3.0 | 6 votes |
public MockBodyPost(String bodyMarkdown) { super("","",null,null, 1, 1, "", bodyMarkdown, null, null, null, "", ""); SOUser asker = new SOUser("testUser",-1,1,"registered"); SOUser answerer = new SOUser("testUser",-1,1,"unregistered"); setAnswerCreationDate(Instant.now()); setQuestionCreationDate(Instant.EPOCH); setAsker(asker); setAnswerer(answerer); Parser parser = Parser.builder().build(); Node document = parser.parse(bodyMarkdown); HtmlRenderer renderer = HtmlRenderer.builder().build(); setBody(renderer.render(document)); }
Example 2
Source File: FUN_Markdown.java From sparql-generate with Apache License 2.0 | 6 votes |
@Override public NodeValue exec(NodeValue markdown) { if (markdown.getDatatypeURI() != null && !markdown.getDatatypeURI().equals(datatypeUri) && !markdown.getDatatypeURI().equals("http://www.w3.org/2001/XMLSchema#string")) { LOG.debug("The URI of NodeValue1 must be <" + datatypeUri + ">" + "or <http://www.w3.org/2001/XMLSchema#string>." ); } try { String md = markdown.asNode().getLiteralLexicalForm(); Parser parser = Parser.builder().build(); Node document = parser.parse(md); HtmlRenderer renderer = HtmlRenderer.builder().build(); String html = renderer.render(document); return new NodeValueString(html); } catch (Exception ex) { throw new ExprEvalException("FunctionBase: no evaluation", ex); } }
Example 3
Source File: ParserTest.java From commonmark-java with BSD 2-Clause "Simplified" License | 6 votes |
@Test public void enabledBlockTypes() { String given = "# heading 1\n\nnot a heading"; Parser parser = Parser.builder().build(); // all core parsers by default Node document = parser.parse(given); assertThat(document.getFirstChild(), instanceOf(Heading.class)); Set<Class<? extends Block>> headersOnly = new HashSet<>(); headersOnly.add(Heading.class); parser = Parser.builder().enabledBlockTypes(headersOnly).build(); document = parser.parse(given); assertThat(document.getFirstChild(), instanceOf(Heading.class)); Set<Class<? extends Block>> noCoreTypes = new HashSet<>(); parser = Parser.builder().enabledBlockTypes(noCoreTypes).build(); document = parser.parse(given); assertThat(document.getFirstChild(), not(instanceOf(Heading.class))); }
Example 4
Source File: FileViewFragment.java From lbry-android with MIT License | 6 votes |
private String buildMarkdownHtml(String markdown) { Parser parser = Parser.builder().build(); Node document = parser.parse(markdown); HtmlRenderer renderer = HtmlRenderer.builder().build(); String markdownHtml = renderer.render(document); return "<!doctype html>\n" + " <html>\n" + " <head>\n" + " <meta charset=\"utf-8\"/>\n" + " <meta name=\"viewport\" content=\"width=device-width, user-scalable=no\"/>\n" + " <style type=\"text/css\">\n" + " body { font-family: 'Inter', sans-serif; margin: 16px }\n" + " img { width: 100%; }\n" + " pre { white-space: pre-wrap; word-wrap: break-word }\n" + " </style>\n" + " </head>\n" + " <body>\n" + " <div id=\"content\">\n" + markdownHtml + " </div>\n" + " </body>\n" + " </html>"; }
Example 5
Source File: MarkDownProvider.java From mvvm-template with GNU General Public License v3.0 | 6 votes |
protected static void render(@NonNull TextView textView, String markdown, int width) { List<Extension> extensions = Arrays.asList( StrikethroughExtension.create(), AutolinkExtension.create(), TablesExtension.create(), InsExtension.create(), EmojiExtension.create(), MentionExtension.create(), YamlFrontMatterExtension.create()); Parser parser = Parser.builder() .extensions(extensions) .build(); try { Node node = parser.parse(markdown); String rendered = HtmlRenderer .builder() .extensions(extensions) .build() .render(node); HtmlHelper.htmlIntoTextView(textView, rendered, (width - (textView.getPaddingStart() + textView.getPaddingEnd()))); } catch (Exception ignored) { HtmlHelper.htmlIntoTextView(textView, markdown, (width - (textView.getPaddingStart() + textView.getPaddingEnd()))); } }
Example 6
Source File: UsageExampleTest.java From commonmark-java with BSD 2-Clause "Simplified" License | 5 votes |
@Test public void addAttributes() { Parser parser = Parser.builder().build(); HtmlRenderer renderer = HtmlRenderer.builder() .attributeProviderFactory(new AttributeProviderFactory() { public AttributeProvider create(AttributeProviderContext context) { return new ImageAttributeProvider(); } }) .build(); Node document = parser.parse("![text](/url.png)"); assertEquals("<p><img src=\"/url.png\" alt=\"text\" class=\"border\" /></p>\n", renderer.render(document)); }
Example 7
Source File: AndroidSupportTest.java From commonmark-java with BSD 2-Clause "Simplified" License | 5 votes |
private void parseWithExtensionsTest(Extension extension) throws Exception { Parser parser = Parser.builder() .extensions(Collections.singletonList(extension)) .build(); Node document = parser.parse(spec); assertNotNull(document); HtmlRenderer renderer = HtmlRenderer.builder() .extensions(Collections.singletonList(extension)) .build(); String renderedString = renderer.render(document); assertNotNull(renderedString); }
Example 8
Source File: AndroidSupportTest.java From commonmark-java with BSD 2-Clause "Simplified" License | 5 votes |
@Test public void parseTest() throws Exception { Parser parser = new Parser.Builder().build(); Node document = parser.parse(spec); assertNotNull(document); }
Example 9
Source File: MarkdownUtil.java From pybbs with GNU Affero General Public License v3.0 | 5 votes |
public static String render(String content) { List<Extension> extensions = Arrays.asList(AutolinkExtension.create(), TablesExtension.create()); Parser parser = Parser.builder().extensions(extensions).build(); // 回车一次就可以实现换行 HtmlRenderer renderer = HtmlRenderer.builder().softbreak("<br/>").attributeProviderFactory(context -> new MyAttributeProvider()).extensions(extensions).build(); Node document = parser.parse(content == null ? "" : content); return renderer.render(document); }
Example 10
Source File: MarkdownUtil.java From android with MIT License | 5 votes |
/** * Temporary work-around for Markwon.setMarkdown(view, script); while LinkMovementMethod * crashes during selection. * See https://github.com/noties/Markwon/issues/41 * and https://github.com/noties/Markwon/tree/v1.0.6#quick-start. */ public static void setUpScript(TextView view, String script, Context context) { // create a Parser instance (can be done manually) // internally creates default Parser instance & registers `strike-through` & `tables` extension final Parser parser = Markwon.createParser(); // core class to display markdown, can be obtained via this method, // which creates default instance (no images handling though), // or via `builder` method, which lets you to configure this instance // // `this` refers to a Context instance final SpannableConfiguration configuration = SpannableConfiguration.create(context); // it's better **not** to re-use this class between multiple calls final SpannableRenderer renderer = new SpannableRenderer(); final Node node = parser.parse(script); final CharSequence text = renderer.render(configuration, node); // for links in markdown to be clickable view.setMovementMethod(BetterLinkMovementMethod.getInstance()); // we need these due to the limited nature of Spannables to invalidate TextView Markwon.unscheduleDrawables(view); Markwon.unscheduleTableRows(view); view.setText(text); Markwon.scheduleDrawables(view); Markwon.scheduleTableRows(view); }
Example 11
Source File: MarkDownProvider.java From mvvm-template with GNU General Public License v3.0 | 5 votes |
@NonNull public static String stripMdText(String markdown) { if (!InputHelper.isEmpty(markdown)) { Parser parser = Parser.builder().build(); Node node = parser.parse(markdown); return stripHtml(HtmlRenderer.builder().build().render(node)); } return ""; }
Example 12
Source File: RenderMarkdown.java From symja_android_library with GNU General Public License v3.0 | 5 votes |
public static void main(String[] args) { Set<Extension> EXTENSIONS = Collections.singleton(TablesExtension.create()); Parser parser = Parser.builder().extensions(EXTENSIONS).build(); Node document = parser.parse(TABLE); HtmlRenderer renderer = HtmlRenderer.builder().extensions(EXTENSIONS).build(); String html = renderer.render(document); System.out.println(html); }
Example 13
Source File: MyUtils.java From Jantent with MIT License | 5 votes |
/** * markdown转换为html * * @param markdown * @return */ public static String mdToHtml(String markdown) { if (StringUtils.isBlank(markdown)) { return ""; } List<Extension> extensions = Arrays.asList(TablesExtension.create()); Parser parser = Parser.builder().extensions(extensions).build(); Node document = parser.parse(markdown); HtmlRenderer renderer = HtmlRenderer.builder() .attributeProviderFactory(context -> new LinkAttributeProvider()) .extensions(extensions).build(); String content = renderer.render(document); content = Commons.emoji(content); return content; }
Example 14
Source File: DocMaker.java From jeka with Apache License 2.0 | 5 votes |
private String mdToHtml(String mdContent, String title) { final StringBuilder sb = new StringBuilder(); final String rawHeader = new String(JkUtilsPath.readAllBytes(htmlTemplates.resolve("header.html")), UTF8); sb.append( rawHeader.replace("${title}", title).replace("${version}", version) ); final Parser parser = Parser.builder().build(); final Node document = parser.parse(mdContent); final List<MenuItem> menuItems = addAnchorAndNumberingToHeaders(document); addMenu(document, menuItems); final HtmlRenderer renderer = HtmlRenderer.builder().build(); sb.append(renderer.render(document)); sb.append( new String(JkUtilsPath.readAllBytes(htmlTemplates.resolve("footer.html")), UTF8) ); return sb.toString(); }
Example 15
Source File: MdToHtml.java From xian with Apache License 2.0 | 5 votes |
/** * @param md the markdown string. * @return converted html */ public static String mdToHtml(String md) { Parser parser = Parser.builder().build(); Node document = parser.parse(md); HtmlRenderer renderer = HtmlRenderer.builder().build(); return renderer.render(document); }
Example 16
Source File: ParserTest.java From commonmark-java with BSD 2-Clause "Simplified" License | 5 votes |
@Test public void customBlockParserFactory() { Parser parser = Parser.builder().customBlockParserFactory(new DashBlockParserFactory()).build(); // The dashes would normally be a ThematicBreak Node document = parser.parse("hey\n\n---\n"); assertThat(document.getFirstChild(), instanceOf(Paragraph.class)); assertEquals("hey", ((Text) document.getFirstChild().getFirstChild()).getLiteral()); assertThat(document.getLastChild(), instanceOf(DashBlock.class)); }
Example 17
Source File: UsageExampleTest.java From commonmark-java with BSD 2-Clause "Simplified" License | 5 votes |
@Test public void customizeRendering() { Parser parser = Parser.builder().build(); HtmlRenderer renderer = HtmlRenderer.builder() .nodeRendererFactory(new HtmlNodeRendererFactory() { public NodeRenderer create(HtmlNodeRendererContext context) { return new IndentedCodeBlockNodeRenderer(context); } }) .build(); Node document = parser.parse("Example:\n\n code"); assertEquals("<p>Example:</p>\n<pre>code\n</pre>\n", renderer.render(document)); }
Example 18
Source File: HierarchicalTableGenerator.java From org.hl7.fhir.core with Apache License 2.0 | 5 votes |
public Cell addMarkdown(String md) { try { Parser parser = Parser.builder().build(); Node document = parser.parse(md); HtmlRenderer renderer = HtmlRenderer.builder().escapeHtml(true).build(); String html = renderer.render(document); pieces.addAll(htmlToParagraphPieces(html)); } catch (Exception e) { e.printStackTrace(); } return this; }
Example 19
Source File: InstallService.java From zrlog with Apache License 2.0 | 4 votes |
public static String renderMd(String md) { Parser parser = Parser.builder().build(); Node document = parser.parse(md); HtmlRenderer renderer = HtmlRenderer.builder().build(); return renderer.render(document); }
Example 20
Source File: LinkReferenceDefinitionNodeTest.java From commonmark-java with BSD 2-Clause "Simplified" License | 4 votes |
private static Node parse(String input) { Parser parser = Parser.builder().build(); return parser.parse(input); }