Java Code Examples for com.vladsch.flexmark.html.HtmlRenderer#render()

The following examples show how to use com.vladsch.flexmark.html.HtmlRenderer#render() . 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: MarkdownHandler.java    From 1Rramp-Android with MIT License 6 votes vote down vote up
public static String getHtmlFromMarkdown(String md){
  MutableDataSet options = new MutableDataSet();

  // uncomment to set optional extensions
  //options.set(Parser.EXTENSIONS, Arrays.asList(TablesExtension.create(), StrikethroughExtension.create()));

  // uncomment to convert soft-breaks to hard breaks
  //options.set(HtmlRenderer.SOFT_BREAK, "<br />\n");

  Parser parser = Parser.builder(options).build();
  HtmlRenderer renderer = HtmlRenderer.builder(options).build();
  Node document = parser.parse(md);
  String html = renderer.render(document);
  //do some more formatting
  html = RegexUtils.replaceMarkdownImage(html);
  html = RegexUtils.replacePlainImageLinks(html);
  return html;
}
 
Example 2
Source File: MarkdownParser.java    From OmniList with GNU Affero General Public License v3.0 6 votes vote down vote up
@Override
protected String doInBackground(String... strings) {
    Parser parser = Parser.builder(OPTIONS)
            .extensions(EXTENSIONS)
            .build();

    HtmlRenderer renderer = HtmlRenderer.builder(OPTIONS)
            .escapeHtml(true)
            .attributeProviderFactory(new IndependentAttributeProviderFactory() {
                @Override
                public AttributeProvider create(NodeRendererContext context) {
                    return new CustomAttributeProvider();
                }
            })
            .nodeRendererFactory(new NodeRendererFactoryImpl())
            .extensions(EXTENSIONS)
            .build();

    return renderer.render(parser.parse(strings[0]));
}
 
Example 3
Source File: DocumentationGenerator.java    From TestingApp with Apache License 2.0 6 votes vote down vote up
public String getInstructionsAsHTML() {
    MutableDataSet options = new MutableDataSet();

    // uncomment to set optional extensions
    //options.set(Parser.EXTENSIONS, Arrays.asList(TablesExtension.create(), StrikethroughExtension.create()));

    // uncomment to convert soft-breaks to hard breaks
    options.set(HtmlRenderer.SOFT_BREAK, "<br />\n");

    Parser parser = Parser.builder(options).build();
    HtmlRenderer renderer = HtmlRenderer.builder(options).build();

    String markdown = getInstructionsAsMarkdown();

    // You can re-use parser and renderer instances
    Node document = parser.parse(markdown);
    return renderer.render(document);
}
 
Example 4
Source File: MarkdownView.java    From MarkdownView with Apache License 2.0 6 votes vote down vote up
private String parseBuildAndRender(String text) {
    Parser parser = Parser.builder(OPTIONS)
            .extensions(EXTENSIONS)
            .build();

    HtmlRenderer renderer = HtmlRenderer.builder(OPTIONS)
            .escapeHtml(mEscapeHtml)
            .attributeProviderFactory(new IndependentAttributeProviderFactory() {
                @Override
                public AttributeProvider create(NodeRendererContext context) {
                    return new CustomAttributeProvider();
                }
            })
            .nodeRendererFactory(new NodeRendererFactoryImpl())
            .extensions(EXTENSIONS)
            .build();

    return renderer.render(parser.parse(text));
}
 
Example 5
Source File: MarkDownModel.java    From Orienteer with Apache License 2.0 6 votes vote down vote up
@Override
protected String load() {
    String markDownValue = markDawnModel.getObject();
    if (Strings.isEmpty(markDownValue)) {
        return "";
    }
    try {
        MutableDataSet options = new MutableDataSet();
        options.set(Parser.EXTENSIONS, createExtensions());
        Parser parser = Parser.builder(options).build();
        HtmlRenderer renderer = HtmlRenderer.builder(options).build();

        Node node = parser.parse(markDawnModel.getObject());
        markDownValue = renderer.render(node);
    } catch (Exception e) {
        throw new WicketRuntimeException("Can't use flexmark-java for markups", e);
    }
    return markDownValue;
}
 
Example 6
Source File: MarkDownUtil.java    From smart-doc with Apache License 2.0 5 votes vote down vote up
/**
 * Convert markdown to html.
 *
 * @param content markdown contents
 * @return html contents
 */
public static String toHtml(String content) {
    MutableDataSet options = new MutableDataSet();
    options.setFrom(ParserEmulationProfile.MARKDOWN);
    // enable table parse!
    options.set(Parser.EXTENSIONS, Arrays.asList(TablesExtension.create(), StrikethroughExtension.create()));

    Parser parser = Parser.builder(options).build();
    HtmlRenderer renderer = HtmlRenderer.builder(options).build();

    Node document = parser.parse(content);
    return renderer.render(document);
}
 
Example 7
Source File: FileUtils.java    From mogu_blog_v2 with Apache License 2.0 5 votes vote down vote up
/**
 * Markdown转Html
 * @param markdown
 * @return
 */
public static String markdownToHtml(String markdown) {
    MutableDataSet options = new MutableDataSet();
    Parser parser = Parser.builder(options).build();
    HtmlRenderer renderer = HtmlRenderer.builder(options).build();
    Node document = parser.parse(markdown);
    String html = renderer.render(document);
    return html;
}
 
Example 8
Source File: HelpActivity.java    From mhzs with MIT License 5 votes vote down vote up
/**
     * 展示网页
     */
    private void showHtml(String md) {
        DataHolder OPTIONS = PegdownOptionsAdapter.flexmarkOptions(true,
                Extensions.NONE);
        Parser parser = Parser.builder(OPTIONS).build();
        HtmlRenderer renderer = HtmlRenderer.builder(OPTIONS).build();

        Node document = parser.parse(md);
        String html = renderer.render(document);

//        LogUtil.e(html);

        mWebView.getSettings().setDefaultTextEncodingName("UTF-8");
        mWebView.loadData(html, "text/html; charset=UTF-8", null);
    }
 
Example 9
Source File: Markdown2HtmlUtil.java    From plumemo with Apache License 2.0 5 votes vote down vote up
/**
 * Markdownz转为Html
 * @param content
 * @return
 */
public static String html(String content){

    MutableDataSet options = new MutableDataSet();
    options.setFrom(ParserEmulationProfile.MARKDOWN);
    options.set(Parser.EXTENSIONS, Arrays.asList(new Extension[]{TablesExtension.create()}));
    Parser parser = Parser.builder(options).build();
    HtmlRenderer renderer = HtmlRenderer.builder(options).build();
    Node document = parser.parse(content);

    return renderer.render(document);
}
 
Example 10
Source File: DefaultMarkdownManager.java    From onedev with MIT License 5 votes vote down vote up
@Override
public String render(String markdown) {
	List<Extension> extensions = new ArrayList<>();
	extensions.add(AnchorLinkExtension.create());
	extensions.add(TablesExtension.create());
	extensions.add(TaskListExtension.create());
	extensions.add(DefinitionExtension.create());
	extensions.add(TocExtension.create());
	extensions.add(AutolinkExtension.create());
	extensions.addAll(contributedExtensions);

	MutableDataHolder options = new MutableDataSet()
			.set(HtmlRenderer.GENERATE_HEADER_ID, true)
			.set(AnchorLinkExtension.ANCHORLINKS_SET_NAME, true)
			.set(AnchorLinkExtension.ANCHORLINKS_WRAP_TEXT, false)
			.set(AnchorLinkExtension.ANCHORLINKS_TEXT_PREFIX, "<span class='header-anchor'></span>")
			.set(Parser.SPACE_IN_LINK_URLS, true)
			.setFrom(ParserEmulationProfile.GITHUB_DOC)
			.set(TablesExtension.COLUMN_SPANS, false)
			.set(TablesExtension.APPEND_MISSING_COLUMNS, true)
			.set(TablesExtension.DISCARD_EXTRA_COLUMNS, true)
			.set(TablesExtension.HEADER_SEPARATOR_COLUMN_MATCH, true)
			.set(Parser.EXTENSIONS, extensions);

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

	HtmlRenderer htmlRenderer = HtmlRenderer.builder(options).build();
	
	Node document = parser.parse(markdown);
	return htmlRenderer.render(document);
}
 
Example 11
Source File: DataRequirementsProvider.java    From cqf-ruler with Apache License 2.0 5 votes vote down vote up
private String markdownToHtml(Parser parser, HtmlRenderer renderer, String markdown) {
    if (Strings.isNullOrEmpty(markdown)) {
        return null;
    }

    Node document = parser.parse(markdown);
    return renderer.render(document);
}
 
Example 12
Source File: WikiFormatter.java    From webdsl with Apache License 2.0 5 votes vote down vote up
public static String wikiFormat(String text, boolean useHardWraps, String rootUrl){
    	try {
            Node document = MARKDOWN_PARSER.parse( text );
            HtmlRenderer renderer = getHTMLRenderer(rootUrl, useHardWraps);
            return renderer.render(document);
//            		 + "<!--end-->"; //This forces an unclosed HTML-comment in the rendered output to be closed. This fixes the issue where commonmark may escape a closing `-->` when a blank line exists in the HTML comment.  
//    		return processor.markdownToHtml( processVerbatim(text), getLinkRenderer( rootUrl ) );
    	} catch (Exception e) {
			Logger.error(e);
			return errorMessage(text);
		}
    }
 
Example 13
Source File: MarkdownDialog.java    From gpx-animator with Apache License 2.0 5 votes vote down vote up
private String convertMarkdownToHTML(final String md) {
    final MutableDataSet options = new MutableDataSet();
    final Parser parser = Parser.builder(options).build();
    final HtmlRenderer renderer = HtmlRenderer.builder(options).build();
    final Node document = parser.parse(md);
    return renderer.render(document);
}
 
Example 14
Source File: BloggerBlogServiceImpl.java    From BlogSystem with Apache License 2.0 4 votes vote down vote up
private BlogTitleIdDTO analysisAndInsertMdFile(Parser parser, HtmlRenderer renderer, ZipEntry entry,
                                               InputStreamReader reader, int bloggerId, Long cateId) throws IOException {
    String name = entry.getName();

    if (!name.endsWith(".md")) return null;

    StringBuilder b = new StringBuilder((int) entry.getSize());
    int len = 0;
    char[] buff = new char[1024];
    while ((len = reader.read(buff)) > 0) {
        b.append(buff, 0, len);
    }

    // reader.close();
    // zip 文件关闭由 370行:zipFile.close() 统一关闭

    // 内容
    String mdContent = b.toString();

    // 对应的 html 内容
    Document document = parser.parse(mdContent);
    String htmlContent = renderer.render(document);

    // 摘要
    String firReg = htmlContent.replaceAll("<.*?>", ""); // 避免 subString 使有遗留的 html 标签,前端显示时会出错
    String tmpStr = firReg.length() > 500 ? firReg.substring(0, 500) : firReg;
    String aftReg = tmpStr.replaceAll("\\n", "");
    String summary = aftReg.length() > 200 ? aftReg.substring(0, 200) : aftReg;

    // UPDATE: 2018/4/4 更新 图片引用

    // 文件名作为标题
    String title = cateId == -1 ? name.replace(".md", "") :
            name.substring(name.lastIndexOf("/") + 1).replace(".md", "");

    int[] cts = {Math.toIntExact(cateId)};
    int id = insertBlog(bloggerId,
            cts,
            null,
            PUBLIC,
            title,
            htmlContent,
            mdContent,
            summary,
            null,
            false);
    if (id < 0) return null;

    BlogTitleIdDTO node = new BlogTitleIdDTO();
    node.setTitle(title);
    node.setId(id);

    return node;
}
 
Example 15
Source File: MarkdownContext.java    From allure2 with Apache License 2.0 4 votes vote down vote up
@Override
public Function<String, String> getValue() {
    final Parser parser = Parser.builder().build();
    final HtmlRenderer renderer = HtmlRenderer.builder().build();
    return s -> renderer.render(parser.parse(s));
}
 
Example 16
Source File: HtmlExportHelper.java    From mdw with Apache License 2.0 4 votes vote down vote up
public String getHtml(String markdown) {
    Parser parser = FlexmarkInstances.getParser(null);
    HtmlRenderer renderer = FlexmarkInstances.getRenderer(null);
    Node document = parser.parse(markdown);
    return renderer.render(document);
}