跳转到主要内容

MWParserFromHell 是 MediaWiki wikicode 的解析器。

项目描述

Build Status Coverage Status

mwparserfromhell (MediaWiki 解析器从地狱) 是一个 Python 包,提供了一个易于使用且功能强大的 MediaWiki wikicode 解析器。它支持 Python 3.8+。

Earwig 开发,并得到了 ΣLegoktm 以及其他人的贡献。完整文档可在 ReadTheDocs 上找到。开发在 GitHub 上进行。

安装

安装解析器的最简单方法是通过Python包索引;您可以使用

pip install mwparserfromhell

安装最新版本(获取pip)。请确保您的pip已更新,尤其是在Windows上。

另外,获取最新开发版本

git clone https://github.com/earwig/mwparserfromhell.git
cd mwparserfromhell
python setup.py install

完整的单元测试套件需要pytest

pip install pytest

)并且可以使用

python -m pytest

运行。

用法

正常使用相当简单(其中

text

是页面文本)

>>> import mwparserfromhell
>>> wikicode = mwparserfromhell.parse(text)

wikicode

是一个

mwparserfromhell.Wikicode

对象,它类似于一个普通的

str

对象,并具有一些额外的方法。例如

>>> text = "I has a template! {{foo|bar|baz|eggs=spam}} See it?"
>>> wikicode = mwparserfromhell.parse(text)
>>> print(wikicode)
I has a template! {{foo|bar|baz|eggs=spam}} See it?
>>> templates = wikicode.filter_templates()
>>> print(templates)
['{{foo|bar|baz|eggs=spam}}']
>>> template = templates[0]
>>> print(template.name)
foo
>>> print(template.params)
['bar', 'baz', 'eggs=spam']
>>> print(template.get(1).value)
bar
>>> print(template.get("eggs").value)
spam

由于节点可以包含其他节点,因此获取嵌套模板是微不足道的

>>> text = "{{foo|{{bar}}={{baz|{{spam}}}}}}"
>>> mwparserfromhell.parse(text).filter_templates()
['{{foo|{{bar}}={{baz|{{spam}}}}}}', '{{bar}}', '{{baz|{{spam}}}}', '{{spam}}']

您还可以传递

recursive=False

filter_templates()

并手动探索模板。这是可能的,因为节点可以包含额外的

Wikicode

对象

>>> code = mwparserfromhell.parse("{{foo|this {{includes a|template}}}}")
>>> print(code.filter_templates(recursive=False))
['{{foo|this {{includes a|template}}}}']
>>> foo = code.filter_templates(recursive=False)[0]
>>> print(foo.get(1).value)
this {{includes a|template}}
>>> print(foo.get(1).value.filter_templates()[0])
{{includes a|template}}
>>> print(foo.get(1).value.filter_templates()[0].get(1).value)
template

模板可以轻松修改以添加、删除或更改参数。

Wikicode

对象可以像列表一样处理,有

append()

insert()

remove()

replace()

等方法。它们还有一个

matches()

方法,用于比较页面或模板名称,它会处理大小写和空白

>>> text = "{{cleanup}} '''Foo''' is a [[bar]]. {{uncategorized}}"
>>> code = mwparserfromhell.parse(text)
>>> for template in code.filter_templates():
...     if template.name.matches("Cleanup") and not template.has("date"):
...         template.add("date", "July 2012")
...
>>> print(code)
{{cleanup|date=July 2012}} '''Foo''' is a [[bar]]. {{uncategorized}}
>>> code.replace("{{uncategorized}}", "{{bar-stub}}")
>>> print(code)
{{cleanup|date=July 2012}} '''Foo''' is a [[bar]]. {{bar-stub}}
>>> print(code.filter_templates())
['{{cleanup|date=July 2012}}', '{{bar-stub}}']

然后,通过在它上面调用

str()

将其转换回一个普通的

str

对象(用于保存页面!)

>>> text = str(code)
>>> print(text)
{{cleanup|date=July 2012}} '''Foo''' is a [[bar]]. {{bar-stub}}
>>> text == code
True

限制

虽然MediaWiki解析器生成HTML并可以访问模板内容等,但mwparserfromhell仅作为源代码的直接接口。这有几个含义

  • 无法检测由模板转译产生的语法元素。例如,想象一个假设的页面

    "Template:End-bold"

    ,其中包含文本

    </b>

    。虽然MediaWiki会正确理解

    <b>foobar{{end-bold}}

    表示

    <b>foobar</b>

    ,但mwparserfromhell没有检查

    {{end-bold}}

    内容的方法。相反,它会将粗体标签视为未完成,可能延伸到页面的更下方。

  • 与外部链接相邻的模板,如

    http://example.com{{foo}}

    ,被视为链接的一部分。实际上,这取决于模板的内容。

  • 当不同的语法元素交叉时,如

    {{echo|''Hello}}, world!''

    ,解析器会感到困惑,因为这不能用普通的语法树来表示。相反,解析器会将第一个语法结构视为纯文本。在这种情况下,只有斜体标签会被正确解析。

    解决方案:由于这种情况通常与文本格式化有关,而文本格式化通常对用户不感兴趣,您可以在

    mwparserfromhell.parse()

    中传递

    skip_style_tags=True

    。这将

    ''

    '''

    视为纯文本。

    mwparserfromhell的未来版本可能包括多个解析模式,以更合理地绕过此限制。

此外,解析器缺乏对某些wiki特定设置的感知

  • 单词结尾链接不受支持,因为链接尾随规则是语言特定的。

  • 未识别本地化命名空间名称,因此文件链接(如

    [[File:...]]

    )被视为普通维基链接。

  • 任何看起来像XML标签的东西都被视为标签,即使它不是已识别的标签名称,因为有效标签的列表取决于已加载的MediaWiki扩展。

集成

mwparserfromhellEarwigBot 使用,最初是为其开发的;Page 对象有一个 parse 方法,该方法本质上会在 page.get() 上调用 mwparserfromhell.parse()

如果您正在使用 Pywikibot,则您的代码可能如下所示

import mwparserfromhell
import pywikibot

def parse(title):
    site = pywikibot.Site()
    page = pywikibot.Page(site, title)
    text = page.get()
    return mwparserfromhell.parse(text)

如果您没有使用库,可以使用以下 Python 3 代码解析任何页面(使用 APIrequests 库)

import mwparserfromhell
import requests

API_URL = "https://en.wikipedia.org/w/api.php"

def parse(title):
    params = {
        "action": "query",
        "prop": "revisions",
        "rvprop": "content",
        "rvslots": "main",
        "rvlimit": 1,
        "titles": title,
        "format": "json",
        "formatversion": "2",
    }
    headers = {"User-Agent": "My-Bot-Name/1.0"}
    req = requests.get(API_URL, headers=headers, params=params)
    res = req.json()
    revision = res["query"]["pages"][0]["revisions"][0]
    text = revision["slots"]["main"]["content"]
    return mwparserfromhell.parse(text)

项目详情


下载文件

下载您平台上的文件。如果您不确定选择哪个,请了解更多关于 安装包 的信息。

源代码发行版

mwparserfromhell-0.6.6.tar.gz (138.9 kB 查看哈希)

上传时间 源代码

构建发行版

mwparserfromhell-0.6.6-cp312-cp312-win_amd64.whl (101.1 kB 查看哈希)

上传时间 CPython 3.12 Windows x86-64

mwparserfromhell-0.6.6-cp312-cp312-win32.whl (98.7 kB 查看哈希)

上传时间 CPython 3.12 Windows x86

mwparserfromhell-0.6.6-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (201.9 kB 查看哈希)

上传时间 CPython 3.12 manylinux: glibc 2.17+ x86-64

mwparserfromhell-0.6.6-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (203.0 kB 查看哈希)

上传时间 CPython 3.12 manylinux: glibc 2.17+ ARM64

mwparserfromhell-0.6.6-cp312-cp312-macosx_10_9_universal2.whl (124.2 kB 查看哈希)

上传时间 CPython 3.12 macOS 10.9+ universal2 (ARM64, x86-64)

mwparserfromhell-0.6.6-cp311-cp311-win_amd64.whl (100.7 kB 查看哈希)

上传时间: CPython 3.11 Windows x86-64

mwparserfromhell-0.6.6-cp311-cp311-win32.whl (98.4 kB 查看哈希)

上传时间: CPython 3.11 Windows x86

mwparserfromhell-0.6.6-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (196.3 kB 查看哈希)

上传时间: CPython 3.11 manylinux: glibc 2.17+ x86-64

mwparserfromhell-0.6.6-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (197.7 kB 查看哈希)

上传时间: CPython 3.11 manylinux: glibc 2.17+ ARM64

mwparserfromhell-0.6.6-cp311-cp311-macosx_10_9_universal2.whl (123.8 kB 查看哈希)

上传时间: CPython 3.11 macOS 10.9+ universal2 (ARM64, x86-64)

mwparserfromhell-0.6.6-cp311-cp311-macosx_10_9_universal2.whl (100.5 kB 查看哈希)

上传时间: CPython 3.10 Windows x86-64

mwparserfromhell-0.6.6-cp310-cp310-win32.whl (98.5 kB 查看哈希)

上传时间: CPython 3.10 Windows x86

mwparserfromhell-0.6.6-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (191.0 kB 查看哈希)

上传时间: CPython 3.10 manylinux: glibc 2.17+ x86-64

mwparserfromhell-0.6.6-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (192.6 kB 查看哈希)

上传时间: CPython 3.10 manylinux: glibc 2.17+ ARM64

mwparserfromhell-0.6.6-cp310-cp310-macosx_11_0_x86_64.whl (99.4 kB 查看哈希)

上传时间: CPython 3.10 macOS 11.0+ x86-64

mwparserfromhell-0.6.6-cp39-cp39-win_amd64.whl (103.9 kB 查看哈希)

上传于 CPython 3.9 Windows x86-64

mwparserfromhell-0.6.6-cp39-cp39-win32.whl (101.0 kB 查看哈希值)

上传于 CPython 3.9 Windows x86

mwparserfromhell-0.6.6-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (190.5 kB 查看哈希值)

上传于 CPython 3.9 manylinux: glibc 2.17+ x86-64

mwparserfromhell-0.6.6-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (192.1 kB 查看哈希值)

上传于 CPython 3.9 manylinux: glibc 2.17+ ARM64

mwparserfromhell-0.6.6-cp39-cp39-macosx_11_0_x86_64.whl (99.4 kB 查看哈希值)

上传于 CPython 3.9 macOS 11.0+ x86-64

mwparserfromhell-0.6.6-cp38-cp38-win_amd64.whl (103.9 kB 查看哈希值)

上传于 CPython 3.8 Windows x86-64

mwparserfromhell-0.6.6-cp38-cp38-win32.whl (100.9 kB 查看哈希值)

上传于 CPython 3.8 Windows x86

mwparserfromhell-0.6.6-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (196.1 kB 查看哈希值)

上传于 CPython 3.8 manylinux: glibc 2.17+ x86-64

mwparserfromhell-0.6.6-cp38-cp38-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (198.0 kB 查看哈希值)

上传于 CPython 3.8 manylinux: glibc 2.17+ ARM64

mwparserfromhell-0.6.6-cp38-cp38-macosx_11_0_x86_64.whl (99.4 kB 查看哈希值)

上传于 CPython 3.8 macOS 11.0+ x86-64

支持者

AWS AWS 云计算和安全赞助商 Datadog Datadog 监控 Fastly Fastly CDN Google Google 下载分析 Microsoft Microsoft PSF赞助商 Pingdom Pingdom 监控 Sentry Sentry 错误记录 StatusPage StatusPage 状态页面