HTML to Markdown with Java

2020-01-30 02:19发布

is there an easy way to transform HTML into markdown with JAVA?

I am currently using the Java MarkdownJ library to transform markdown to html.

import com.petebevin.markdown.MarkdownProcessor;
...
public static String getHTML(String markdown) {
    MarkdownProcessor markdown_processor = new MarkdownProcessor();
    return markdown_processor.markdown(markdown);
}

public static String getMarkdown(String html) {
/* TODO Ask stackoverflow */
}

标签: java markdown
4条回答
孤傲高冷的网名
2楼-- · 2020-01-30 03:13

I came across Remark for converting HTML to Markdown see: http://remark.overzealous.com/manual/index.html It depends on JSoup, a powerful Java library for working with real-world HTML.

查看更多
不美不萌又怎样
3楼-- · 2020-01-30 03:13

I am working on the same issue, and experimenting with a couple different techniques.

The answer above could work. You could use the jTidy library to do the initial cleanup work and convert from HTML to XHTML. You use the XSLT stylesheet linked above.

Unfortunately there is no library that has a one-stop function to do this in Java. You could try using the Python script html2text with Jython, but I haven't yet tried this!

查看更多
够拽才男人
4楼-- · 2020-01-30 03:14

Use this XSLT.

If you need help using XSLT and Java here's a code snippet:

public static void main(String[] args) throws Exception {

        File xsltFile = new File("mardownXSLT.xslt");

        Source xmlSource = new StreamSource(new StringReader(theHTML));
        Source xsltSource = new StreamSource(xsltFile);

        TransformerFactory transFact =
                TransformerFactory.newInstance();
        Transformer trans = transFact.newTransformer(xsltSource);

        StringWriter result = new StringWriter();
        trans.transform(xmlSource, new StreamResult(result));
    }
查看更多
神经病院院长
5楼-- · 2020-01-30 03:19

if you are using WMD editor and want to get the markdown code on the server side, just use these options before loading the wmd.js script:

wmd_options = {
        // format sent to the server.  can also be "HTML"
        output: "Markdown",

        // line wrapping length for lists, blockquotes, etc.
        lineLength: 40,

        // toolbar buttons.  Undo and redo get appended automatically.
        buttons: "bold italic | link blockquote code image | ol ul heading hr",

        // option to automatically add WMD to the first textarea found.
        autostart: true
    };
查看更多
登录 后发表回答