org.commonmark.parser.Parser Java Examples

The following examples show how to use org.commonmark.parser.Parser. 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: MarkdownUtil.java    From Roothub with GNU Affero General Public License v3.0 7 votes vote down vote up
/**
 * 渲染 Markdown
 * @param content
 * @return
 */
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 #2
Source File: MarkwonImplTest.java    From Markwon with Apache License 2.0 6 votes vote down vote up
@Test
public void fallback_to_raw_false() {
    final String md = "*";

    final MarkwonImpl impl = new MarkwonImpl(
            TextView.BufferType.SPANNABLE,
            null,
            mock(Parser.class, RETURNS_MOCKS),
            // it must be sufficient to just return mocks and thus empty rendering result
            mock(MarkwonVisitorFactory.class, RETURNS_MOCKS),
            mock(MarkwonConfiguration.class),
            Collections.<MarkwonPlugin>emptyList(),
            false
    );

    final Spanned spanned = impl.toMarkdown(md);
    assertTrue(spanned.toString(), TextUtils.isEmpty(spanned));
}
 
Example #3
Source File: MarkwonImplTest.java    From Markwon with Apache License 2.0 6 votes vote down vote up
@Test
public void parse_calls_plugin_process_markdown() {

    final MarkwonPlugin plugin = mock(MarkwonPlugin.class);
    final MarkwonImpl impl = new MarkwonImpl(
            TextView.BufferType.SPANNABLE,
            null,
            mock(Parser.class),
            mock(MarkwonVisitorFactory.class),
            mock(MarkwonConfiguration.class),
            Collections.singletonList(plugin),
            true
    );

    impl.parse("whatever");

    verify(plugin, times(1)).processMarkdown(eq("whatever"));
}
 
Example #4
Source File: MarkwonBuilderImplTest.java    From Markwon with Apache License 2.0 6 votes vote down vote up
@Test
public void plugin_configured() {
    // verify that all configuration methods (applicable) are called

    final MarkwonPlugin plugin = mock(MarkwonPlugin.class);

    final MarkwonBuilderImpl impl = new MarkwonBuilderImpl(RuntimeEnvironment.application);
    impl.usePlugin(plugin).build();

    verify(plugin, times(1)).configure(any(MarkwonPlugin.Registry.class));

    verify(plugin, times(1)).configureParser(any(Parser.Builder.class));
    verify(plugin, times(1)).configureTheme(any(MarkwonTheme.Builder.class));
    verify(plugin, times(1)).configureConfiguration(any(MarkwonConfiguration.Builder.class));
    verify(plugin, times(1)).configureVisitor(any(MarkwonVisitor.Builder.class));
    verify(plugin, times(1)).configureSpansFactory(any(MarkwonSpansFactory.Builder.class));

    // note, no render props -> they must be configured on render stage
    verify(plugin, times(0)).processMarkdown(anyString());
    verify(plugin, times(0)).beforeRender(any(Node.class));
    verify(plugin, times(0)).afterRender(any(Node.class), any(MarkwonVisitor.class));
    verify(plugin, times(0)).beforeSetText(any(TextView.class), any(Spanned.class));
    verify(plugin, times(0)).afterSetText(any(TextView.class));
}
 
Example #5
Source File: ParserTest.java    From commonmark-java with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Test
public void ioReaderTest() throws IOException {
    Parser parser = Parser.builder().build();

    InputStream input1 = TestResources.getSpec().openStream();
    Node document1;
    try (InputStreamReader reader = new InputStreamReader(input1, Charset.forName("UTF-8"))) {
        document1 = parser.parseReader(reader);
    }

    String spec = TestResources.readAsString(TestResources.getSpec());
    Node document2 = parser.parse(spec);

    HtmlRenderer renderer = HtmlRenderer.builder().escapeHtml(true).build();
    assertEquals(renderer.render(document2), renderer.render(document1));
}
 
Example #6
Source File: MarkdownController.java    From java-docs-samples with Apache License 2.0 6 votes vote down vote up
@PostMapping("/")
public String markdownRenderer(@RequestBody String payload) {
  // Set up HTML renderer
  // https://github.com/atlassian/commonmark-java#extensions
  List<Extension> extensions =
      Arrays.asList(TablesExtension.create(), StrikethroughExtension.create());
  Parser parser = Parser.builder().extensions(extensions).build();
  Node document = parser.parse(payload);
  HtmlRenderer renderer = HtmlRenderer.builder().extensions(extensions).build();
  // Convert Markdown to HTML
  String converted = renderer.render(document);

  // Use prepackaged policies to sanitize HTML. Cusomized and tighter standards
  // are recommended.
  PolicyFactory policy =
      Sanitizers.FORMATTING
          .and(Sanitizers.BLOCKS)
          .and(Sanitizers.LINKS)
          .and(Sanitizers.IMAGES)
          .and(Sanitizers.TABLES);
  String safeHtml = policy.sanitize(converted);

  return safeHtml;
}
 
Example #7
Source File: MarkwonImpl.java    From Markwon with Apache License 2.0 6 votes vote down vote up
MarkwonImpl(
        @NonNull TextView.BufferType bufferType,
        @Nullable TextSetter textSetter,
        @NonNull Parser parser,
        @NonNull MarkwonVisitorFactory visitorFactory,
        @NonNull MarkwonConfiguration configuration,
        @NonNull List<MarkwonPlugin> plugins,
        boolean fallbackToRawInputWhenEmpty
) {
    this.bufferType = bufferType;
    this.textSetter = textSetter;
    this.parser = parser;
    this.visitorFactory = visitorFactory;
    this.configuration = configuration;
    this.plugins = plugins;
    this.fallbackToRawInputWhenEmpty = fallbackToRawInputWhenEmpty;
}
 
Example #8
Source File: TaleUtils.java    From tale with MIT License 6 votes vote down vote up
/**
 * markdown转换为html
 *
 * @param markdown
 * @return
 */
public static String mdToHtml(String markdown) {
    if (StringKit.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().extensions(extensions).build();
    String          content    = renderer.render(document);
    content = Commons.emoji(content);

    // 支持网易云音乐输出
    if (TaleConst.BCONF.getBoolean("app.support_163_music", true) && content.contains("[mp3:")) {
        content = content.replaceAll("\\[mp3:(\\d+)\\]", "<iframe frameborder=\"no\" border=\"0\" marginwidth=\"0\" marginheight=\"0\" width=350 height=106 src=\"//music.163.com/outchain/player?type=2&id=$1&auto=0&height=88\"></iframe>");
    }
    // 支持gist代码输出
    if (TaleConst.BCONF.getBoolean("app.support_gist", true) && content.contains("https://gist.github.com/")) {
        content = content.replaceAll("&lt;script src=\"https://gist.github.com/(\\w+)/(\\w+)\\.js\">&lt;/script>", "<script src=\"https://gist.github.com/$1/$2\\.js\"></script>");
    }

    return content;
}
 
Example #9
Source File: MarkwonImplTest.java    From Markwon with Apache License 2.0 6 votes vote down vote up
@Test
    public void render_clears_visitor() {
        // each render call should have empty-state visitor (no previous rendering info)

        final MarkwonVisitorFactory visitorFactory = mock(MarkwonVisitorFactory.class);
        final MarkwonVisitor visitor = mock(MarkwonVisitor.class, RETURNS_MOCKS);

        when(visitorFactory.create()).thenReturn(visitor);

        final MarkwonImpl impl = new MarkwonImpl(
                TextView.BufferType.SPANNABLE,
                null,
                mock(Parser.class),
                visitorFactory,
                mock(MarkwonConfiguration.class),
                Collections.<MarkwonPlugin>emptyList(),
                true
        );

        impl.render(mock(Node.class));

        // obsolete starting with 4.1.1
//        verify(visitor, times(1)).clear();
        verify(visitor, never()).clear();
    }
 
Example #10
Source File: MarkDownProvider.java    From mvvm-template with GNU General Public License v3.0 6 votes vote down vote up
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 #11
Source File: FUN_Markdown.java    From sparql-generate with Apache License 2.0 6 votes vote down vote up
@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 #12
Source File: FUN_Markdown.java    From sparql-generate with Apache License 2.0 6 votes vote down vote up
@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 #13
Source File: MockBodyPost.java    From Natty with GNU General Public License v3.0 6 votes vote down vote up
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 #14
Source File: MarkwonImplTest.java    From Markwon with Apache License 2.0 6 votes vote down vote up
@Test
public void fallback_to_raw() {
    final String md = "*";

    final MarkwonImpl impl = new MarkwonImpl(
            TextView.BufferType.SPANNABLE,
            null,
            mock(Parser.class, RETURNS_MOCKS),
            // it must be sufficient to just return mocks and thus empty rendering result
            mock(MarkwonVisitorFactory.class, RETURNS_MOCKS),
            mock(MarkwonConfiguration.class),
            Collections.<MarkwonPlugin>emptyList(),
            true
    );

    final Spanned spanned = impl.toMarkdown(md);
    assertEquals(md, spanned.toString());
}
 
Example #15
Source File: StrikethroughPluginTest.java    From Markwon with Apache License 2.0 6 votes vote down vote up
@Test
public void plugin_parser_extension_registered() {
    // configure parser is called with proper parser extension

    final StrikethroughPlugin plugin = StrikethroughPlugin.create();
    final Parser.Builder parserBuilder = mock(Parser.Builder.class);
    plugin.configureParser(parserBuilder);

    //noinspection unchecked
    final ArgumentCaptor<Iterable<Extension>> captor = ArgumentCaptor.forClass(Iterable.class);

    //noinspection unchecked
    verify(parserBuilder, times(1)).extensions(captor.capture());

    final List<Extension> list = Ix.from(captor.getValue()).toList();
    assertEquals(1, list.size());

    assertTrue(list.get(0) instanceof StrikethroughExtension);
}
 
Example #16
Source File: RuleService.java    From clouditor with Apache License 2.0 6 votes vote down vote up
/**
 * Loads a rule from a Markdown-style document specified by the path.
 *
 * @param path The path to the Markdown file
 * @return a parsed {@link Rule}
 * @throws IOException if a parsing error occurred
 */
public Rule loadRule(Path path) throws IOException {
  var rule = new Rule();

  LOGGER.info("Trying to load rule from {}...", path.getFileName());

  var parser = Parser.builder().build();

  var doc = parser.parseReader(new InputStreamReader(Files.newInputStream(path)));

  doc.accept(new RuleVisitor(rule));

  rule.setId(
      path.getParent().getParent().getFileName()
          + "-"
          + path.getParent().getFileName()
          + "-"
          + path.getFileName().toString().split("\\.")[0]);

  rule.setActive(true);

  return rule;
}
 
Example #17
Source File: ParserTest.java    From commonmark-java with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Test
public void indentation() {
    String given = " - 1 space\n   - 3 spaces\n     - 5 spaces\n\t - tab + space";
    Parser parser = Parser.builder().build();
    Node document = parser.parse(given);

    assertThat(document.getFirstChild(), instanceOf(BulletList.class));

    Node list = document.getFirstChild(); // first level list
    assertEquals("expect one child", list.getFirstChild(), list.getLastChild());
    assertEquals("1 space", firstText(list.getFirstChild()));

    list = list.getFirstChild().getLastChild(); // second level list
    assertEquals("expect one child", list.getFirstChild(), list.getLastChild());
    assertEquals("3 spaces", firstText(list.getFirstChild()));

    list = list.getFirstChild().getLastChild(); // third level list
    assertEquals("5 spaces", firstText(list.getFirstChild()));
    assertEquals("tab + space", firstText(list.getFirstChild().getNext()));
}
 
Example #18
Source File: ParserTest.java    From commonmark-java with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@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 #19
Source File: FileViewFragment.java    From lbry-android with MIT License 6 votes vote down vote up
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 #20
Source File: ParserTest.java    From commonmark-java with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@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 #21
Source File: UsageExampleTest.java    From commonmark-java with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Test
public void parseAndRender() {
    Parser parser = Parser.builder().build();
    Node document = parser.parse("This is *Sparta*");
    HtmlRenderer renderer = HtmlRenderer.builder().escapeHtml(true).build();
    assertEquals("<p>This is <em>Sparta</em></p>\n", renderer.render(document));
}
 
Example #22
Source File: UsageExampleTest.java    From commonmark-java with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Test
@Ignore
public void parseReaderRender() throws IOException {
    Parser parser = Parser.builder().build();
    try (InputStreamReader reader = new InputStreamReader(new FileInputStream("file.md"), StandardCharsets.UTF_8)) {
        Node document = parser.parseReader(reader);
        // ...
    }
}
 
Example #23
Source File: JLatexMathPluginTest.java    From Markwon with Apache License 2.0 5 votes vote down vote up
@Test
public void block_parser_registered() {
    final JLatexMathPlugin plugin = JLatexMathPlugin.create(0);
    final Parser.Builder builder = mock(Parser.Builder.class);
    plugin.configureParser(builder);
    verify(builder, times(1)).customBlockParserFactory(any(BlockParserFactory.class));
}
 
Example #24
Source File: DelimiterProcessorTest.java    From commonmark-java with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Test
public void delimiterProcessorWithInvalidDelimiterUse() {
    Parser parser = Parser.builder()
            .customDelimiterProcessor(new CustomDelimiterProcessor(':', 0))
            .customDelimiterProcessor(new CustomDelimiterProcessor(';', -1))
            .build();
    assertEquals("<p>:test:</p>\n", RENDERER.render(parser.parse(":test:")));
    assertEquals("<p>;test;</p>\n", RENDERER.render(parser.parse(";test;")));
}
 
Example #25
Source File: AndroidSupportTest.java    From commonmark-java with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Test
public void htmlRendererTest() throws Exception {
    Parser parser = Parser.builder().build();
    HtmlRenderer renderer = HtmlRenderer.builder().build();

    String renderedString = renderer.render(parser.parse(spec));

    assertNotNull(renderedString);
}
 
Example #26
Source File: AndroidSupportTest.java    From commonmark-java with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Test
public void parseTest() throws Exception {
    Parser parser = new Parser.Builder().build();

    Node document = parser.parse(spec);

    assertNotNull(document);
}
 
Example #27
Source File: AndroidSupportTest.java    From commonmark-java with BSD 2-Clause "Simplified" License 5 votes vote down vote up
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 #28
Source File: DocumentationPod.java    From symja_android_library with GNU General Public License v3.0 5 votes vote down vote up
private static String generateHTMLString(final String markdownStr) {
	Set<Extension> EXTENSIONS = Collections.singleton(TablesExtension.create());
	Parser parser = Parser.builder().extensions(EXTENSIONS).build();
	Node document = parser.parse(markdownStr);
	HtmlRenderer renderer = HtmlRenderer.builder().extensions(EXTENSIONS).build();
	return renderer.render(document);
}
 
Example #29
Source File: DelimiterProcessorTest.java    From commonmark-java with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Test(expected = IllegalArgumentException.class)
public void multipleDelimitersWithSameLength() {
    Parser.builder()
            .customDelimiterProcessor(new OneDelimiterProcessor())
            .customDelimiterProcessor(new OneDelimiterProcessor())
            .build();
}
 
Example #30
Source File: UsageExampleTest.java    From commonmark-java with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@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));
}