A Python parser for MediaWiki wikicode https://mwparserfromhell.readthedocs.io/
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

README.rst 3.7 KiB

12 anos atrás
12 anos atrás
12 anos atrás
12 anos atrás
12 anos atrás
12 anos atrás
12 anos atrás
12 anos atrás
12 anos atrás
12 anos atrás
12 anos atrás
12 anos atrás
12 anos atrás
12 anos atrás
12 anos atrás
12 anos atrás
12 anos atrás
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. mwparserfromhell
  2. ========================
  3. **mwparserfromhell** (the *MediaWiki Parser from Hell*) is a Python package
  4. that provides an easy-to-use and outrageously powerful parser for MediaWiki_
  5. wikicode.
  6. Developed by Earwig_ and named by `Σ`_.
  7. Installation
  8. ------------
  9. The easiest way to install the parser is through the `Python Package Index`_,
  10. so you can install the latest release with ``pip install mwparserfromhell``
  11. (`get pip`_). Alternatively, get the latest development version::
  12. git clone git://github.com/earwig/mwparserfromhell.git mwparserfromhell
  13. cd mwparserfromhell
  14. python setup.py install
  15. You can run the comprehensive unit testing suite with ``python setup.py test``.
  16. Usage
  17. -----
  18. Normal usage is rather straightforward (where ``text`` is page text)::
  19. >>> import mwparserfromhell
  20. >>> parser = mwparserfromhell.Parser()
  21. >>> templates = parser.parse(text)
  22. ``templates`` is a list of ``mwparserfromhell.Template`` objects, which contain
  23. a ``name`` attribute, a ``params`` attribute, and a ``render()`` method. Slices
  24. are supported to get parameters. For example::
  25. >>> templates = parser.parse("{{foo|bar|baz|eggs=spam}}")
  26. >>> print templates
  27. [Template(name="foo", params={"1": "bar", "2": "baz", "eggs": "spam"})]
  28. >>> template = templates[0]
  29. >>> print template.name
  30. foo
  31. >>> print template.params
  32. ['bar', 'baz']
  33. >>> print template[0]
  34. bar
  35. >>> print template["eggs"]
  36. spam
  37. >>> print template.render()
  38. {{foo|bar|baz|eggs=spam}}
  39. If ``get``\ 's argument is a number *n*, it'll return the *n*\ th parameter,
  40. otherwise it will return the parameter with the given name. Unnamed parameters
  41. are given numerical names starting with 1, so ``{{foo|bar}}`` is the same as
  42. ``{{foo|1=bar}}``, and ``templates[0].get(0) is templates[0].get("1")``.
  43. By default, nested templates are supported like so::
  44. >>> templates = parser.parse("{{foo|this {{includes a|template}}}}")
  45. >>> print templates
  46. [Template(name="foo", params={"1": "this {{includes a|template}}"})]
  47. >>> print templates[0].get(0)
  48. this {{includes a|template}}
  49. >>> print templates[0].get(0).templates
  50. [Template(name="includes a", params={"1": "template"})]
  51. >>> print templates[0].get(0).templates[0].params[0]
  52. template
  53. Integration
  54. -----------
  55. ``mwparserfromhell`` is used by and originally developed for EarwigBot_;
  56. ``Page`` objects have a ``parse_templates`` method that essentially calls
  57. ``Parser().parse()`` on ``page.get()``.
  58. If you're using PyWikipedia_, your code might look like this::
  59. import mwparserfromhell
  60. import wikipedia as pywikibot
  61. def parse_templates(title):
  62. site = pywikibot.get_site()
  63. page = pywikibot.Page(site, title)
  64. text = page.get()
  65. parser = mwparserfromhell.Parser()
  66. return parser.parse(text)
  67. If you're not using a library, you can parse templates in any page using the
  68. following code (via the API_)::
  69. import json
  70. import urllib
  71. import mwparserfromhell
  72. API_URL = "http://en.wikipedia.org/w/api.php"
  73. def parse_templates(title):
  74. raw = urllib.urlopen(API_URL, data).read()
  75. res = json.loads(raw)
  76. text = res["query"]["pages"].values()[0]["revisions"][0]["*"]
  77. parser = mwparserfromhell.Parser()
  78. return parser.parse(text)
  79. .. _MediaWiki: http://mediawiki.org
  80. .. _Earwig: http://en.wikipedia.org/wiki/User:The_Earwig
  81. .. _Σ: http://en.wikipedia.org/wiki/User:Σ
  82. .. _Python Package Index: http://pypi.python.org
  83. .. _get pip: http://pypi.python.org/pypi/pip
  84. .. _EarwigBot: https://github.com/earwig/earwigbot
  85. .. _PyWikipedia: http://pywikipediabot.sourceforge.net/
  86. .. _API: http://mediawiki.org/wiki/API