A Python parser for MediaWiki wikicode https://mwparserfromhell.readthedocs.io/
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 

85 řádky
3.2 KiB

  1. Usage
  2. =====
  3. Normal usage is rather straightforward (where ``text`` is page text)::
  4. >>> import mwparserfromhell
  5. >>> wikicode = mwparserfromhell.parse(text)
  6. ``wikicode`` is a :py:class:`mwparserfromhell.Wikicode <.Wikicode>` object,
  7. which acts like an ordinary ``unicode`` object (or ``str`` in Python 3) with
  8. some extra methods. For example::
  9. >>> text = "I has a template! {{foo|bar|baz|eggs=spam}} See it?"
  10. >>> wikicode = mwparserfromhell.parse(text)
  11. >>> print wikicode
  12. I has a template! {{foo|bar|baz|eggs=spam}} See it?
  13. >>> templates = wikicode.filter_templates()
  14. >>> print templates
  15. ['{{foo|bar|baz|eggs=spam}}']
  16. >>> template = templates[0]
  17. >>> print template.name
  18. foo
  19. >>> print template.params
  20. ['bar', 'baz', 'eggs=spam']
  21. >>> print template.get(1).value
  22. bar
  23. >>> print template.get("eggs").value
  24. spam
  25. Since nodes can contain other nodes, getting nested templates is trivial::
  26. >>> text = "{{foo|{{bar}}={{baz|{{spam}}}}}}"
  27. >>> mwparserfromhell.parse(text).filter_templates()
  28. ['{{foo|{{bar}}={{baz|{{spam}}}}}}', '{{bar}}', '{{baz|{{spam}}}}', '{{spam}}']
  29. You can also pass *recursive=False* to :py:meth:`~.filter_templates` and
  30. explore templates manually. This is possible because nodes can contain
  31. additional :py:class:`~.Wikicode` objects::
  32. >>> code = mwparserfromhell.parse("{{foo|this {{includes a|template}}}}")
  33. >>> print code.filter_templates(recursive=False)
  34. ['{{foo|this {{includes a|template}}}}']
  35. >>> foo = code.filter_templates(recursive=False)[0]
  36. >>> print foo.get(1).value
  37. this {{includes a|template}}
  38. >>> print foo.get(1).value.filter_templates()[0]
  39. {{includes a|template}}
  40. >>> print foo.get(1).value.filter_templates()[0].get(1).value
  41. template
  42. Templates can be easily modified to add, remove, or alter params.
  43. :py:class:`~.Wikicode` objects can be treated like lists, with
  44. :py:meth:`~.Wikicode.append`, :py:meth:`~.Wikicode.insert`,
  45. :py:meth:`~.Wikicode.remove`, :py:meth:`~.Wikicode.replace`, and more. They
  46. also have a :py:meth:`~.Wikicode.matches` method for comparing page or template
  47. names, which takes care of capitalization and whitespace::
  48. >>> text = "{{cleanup}} '''Foo''' is a [[bar]]. {{uncategorized}}"
  49. >>> code = mwparserfromhell.parse(text)
  50. >>> for template in code.filter_templates():
  51. ... if template.name.matches("Cleanup") and not template.has_param("date"):
  52. ... template.add("date", "July 2012")
  53. ...
  54. >>> print code
  55. {{cleanup|date=July 2012}} '''Foo''' is a [[bar]]. {{uncategorized}}
  56. >>> code.replace("{{uncategorized}}", "{{bar-stub}}")
  57. >>> print code
  58. {{cleanup|date=July 2012}} '''Foo''' is a [[bar]]. {{bar-stub}}
  59. >>> print code.filter_templates()
  60. ['{{cleanup|date=July 2012}}', '{{bar-stub}}']
  61. You can then convert ``code`` back into a regular :py:class:`unicode` object
  62. (for saving the page!) by calling :py:func:`unicode` on it::
  63. >>> text = unicode(code)
  64. >>> print text
  65. {{cleanup|date=July 2012}} '''Foo''' is a [[bar]]. {{bar-stub}}
  66. >>> text == code
  67. True
  68. (Likewise, use :py:func:`str(code) <str>` in Python 3.)
  69. For more tips, check out :py:class:`Wikicode's full method list <.Wikicode>`
  70. and the :py:mod:`list of Nodes <.nodes>`.