是否有一个体面的、可定制的 HTML 到 Markdown Java API?

发布于 2024-09-30 07:39:02 字数 407 浏览 3 评论 0原文

我想保存从各种来源抓取的文本,但不包含 HTML 标签,但也尽可能保留合理的结构。

Markdown 似乎是这个问题的解决方案(或者可能是 MultiMarkdown)。

一个问题提供了从 HTML 转换为 Markdown 的建议,但我想要指定一些具体的事情:

  • 所有链接(包括图像)仅在END处引用(即没有内联url)
  • 没有嵌入的HTML(我什至还没有100%确定我想如何处理困难的HTML...但它不会被嵌入!)

所以我的问题如标题中所述:是否有一个体面的、可定制的 HTML 到 Markdown Java API?

I want to save text I scrape from various sources without the HTML tags that are on it, but also keeping as much of the structure as I reasonably can.

Markdown seems to be the solution to this (or possibly MultiMarkdown).

There is a question which offers a suggestion on converting from HTML to markdown, but I want to specify some specific things:

  • ALL links (including images) are referenced at the END only (i.e. no inline urls)
  • NO embeded HTML (I'm not even 100% sure yet how I'd like to deal with difficult HTML... but it won't be embeded!)

So my question is as stated in the title: Is there a decent, customisable, HTML to Markdown Java API?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

深空失忆 2024-10-07 07:39:02

您可以尝试调整 HtmlCleaner ,它在 DOM 上提供了一个可行的接口:

TagNode root = htmlCleaner.clean( stream );
Object[] found = root.evaluateXPath( "//div[id='something']" );
if( found.length > 0 && found instanceof TagNode ) {
    ((TagNode)found[0]).removeFromTree();
}

这将允许您在 DOM 上构建输出流使用相当简单的 API 可以处理任何您想要的格式。

You could try adapting HtmlCleaner which provides a workable interface onto the DOM:

TagNode root = htmlCleaner.clean( stream );
Object[] found = root.evaluateXPath( "//div[id='something']" );
if( found.length > 0 && found instanceof TagNode ) {
    ((TagNode)found[0]).removeFromTree();
}

This would allow you to structure your output stream in any format that you want using a fairly simple API.

自由如风 2024-10-07 07:39:02

有一个很棒的 JS 库,名为 Turndown,你可以在线尝试此处。它可以部分定制。例如,可以在末尾引用链接。据我所知,没有嵌入 html,一切都被转换了。

我需要它用于 Java(作为链接的问题),所以我移植了它。 Java 库称为 CopyDown,它具有与 Turndown 相同的测试套件。

使用 gradle 安装:

dependencies {
        compile 'io.github.furstenheim:copy_down:1.0'
}

然后使用它:

CopyDown converter = new CopyDown();
String myHtml = "<h1>Some title</h1><div>Some html<p>Another paragraph</p></div>";
String markdown = converter.convert(myHtml);
System.out.println(markdown);
> Some title\n==========\n\nSome html\n\nAnother paragraph\n

There is a great library for JS called Turndown, you can try it online here. It can be partially customized. For example, links can be referenced at the end. And as far as I know there is no embedded html, everything is transformed.

I needed it for Java (as the linked question), so I ported it. The library for Java is called CopyDown, it has the same test suite as Turndown.

To install with gradle:

dependencies {
        compile 'io.github.furstenheim:copy_down:1.0'
}

Then to use it:

CopyDown converter = new CopyDown();
String myHtml = "<h1>Some title</h1><div>Some html<p>Another paragraph</p></div>";
String markdown = converter.convert(myHtml);
System.out.println(markdown);
> Some title\n==========\n\nSome html\n\nAnother paragraph\n
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文