使用修改后的 Nokogiri 来解析维基文本?

发布于 2025-01-02 11:25:34 字数 772 浏览 3 评论 0原文

对于这个问题的长度表示歉意,这更多的是“这可能吗”而不是“我该怎么做”。

我的目标是从维基百科标记中删除除纯文本之外的所有内容——表格、模板、格式。这些是否在wiki文本标记中(例如''粗体文本'' )或 HTML(粗体文本)。

维基百科文本是自定义标签的组合:模板 {{ ... }}、表格 {| ... |}、链接 [[ ... ]] 和 HTML 元素。解析它有点像一场噩梦。您不能使用正则表达式,因为标签可以嵌套,并且它可以包含 HTML,因此几乎任何事情都是可能的。我想保留 HTML 中的一些文本(粗体文本中的内容),但其他内容(例如表格)需要完全删除。

我考虑过重新利用像 Nokogiri 这样的 XML 解析器,添加 {{/}} 作为 /的替代品

任何了解 Nokogiri(或另一个 Ruby XML 解析器)的人都知道这是否可行,甚至是一个好主意吗?

我的替代方案是重新调整现有解析器的用途,例如用于 wiki 标记的 WikiCloth,然后尝试删除任何剩余的 HTML通过另一种方法。

Apologies for the length of this question, it's more of a "is this possible" than "how do I do this".

My objective is to remove everything but plain text from Wikipedia markup -- tables, templates, formatting. Whether these are in wikitext markup (e.g. ''bold text'') or HTML (<b>bold text</b>).

Wikipedia text is a mix of custom tags: templates {{ ... }}, tables {| ... |}, links [[ ... ]] and HTML elements. Parsing it is kind of a nightmare. You can't use regular expressions because the tags can be nested, and it can contain HTML so almost anything is possible. Some of the text within the HTML I'd want to keep (stuff within bold text), but other things like tables would need to be stripped entirely.

I thought about re-purposing an XML parser like Nokogiri, adding {{/}} as alternatives to <x>/</x>.

Does anyone who knows Nokogiri (or another Ruby XML parser) know if this is possible or even a good idea?

My alternative is to repurpose an existing parser like WikiCloth for the wiki markup, and then try to remove any leftover HTML via another method.

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

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

发布评论

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

评论(1

无人问我粥可暖 2025-01-09 11:25:34

这听起来是个好主意。但是,您无法“修补”Nokogiri,“添加 {{/}} 作为 //的替代方案代码>”。这是因为 Nokogiri 完成的大部分工作(解析和 XPath 以及生成 DOM 的字符串表示形式)实际上是由 libxml2< 完成的/a> 在后端。您必须修补并重新编译 libxml2(然后根据您的新版本重建 Nokogiri)……但那时我不知道 Nokogiri 会如何表现。

您可能会更幸运地尝试修补 REXML,因为它是用纯 Ruby 编写的。

This sounds like a good idea. However, it would not be possible for you to 'patch' Nokogiri, "adding {{/}} as alternatives to <x>/</x>". This is because the bulk of the work done by Nokogiri—parsing and XPath and generating the string representation of a DOM—is actually done by libxml2 in the back end. You'd have to patch and recompile libxml2 (and then rebuild Nokogiri against your new version)…but at that point I have no idea how Nokogiri would behave.

You might have better luck trying to patch REXML, since that is written in pure Ruby.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文