HTML到Java的Markdown
时间:2020-03-05 18:52:24 来源:igfitidea点击:
有没有一种简单的方法可以使用JAVA将HTML转换为markdown?
我目前正在使用Java MarkdownJ库将markdown转换为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 */ }
解决方案
回答
使用此XSLT。
如果我们需要使用XSLT和Java的帮助,请使用以下代码段:
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)); }
回答
我正在研究同一问题,并尝试了几种不同的技术。
上面的答案可能有效。我们可以使用jTidy库进行初始清理工作,并将其从HTML转换为XHTML。我们使用上面链接的XSLT样式表。
不幸的是,在Java中没有库具有一站式功能。我们可以尝试将Python脚本html2text与Jython结合使用,但是我还没有尝试过!
回答
如果我们正在使用WMD编辑器,并且想要在服务器端获取markdown代码,则在加载wmd.js
脚本之前使用以下选项:
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 };