A Python parser for MediaWiki wikicode https://mwparserfromhell.readthedocs.io/
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 

83 líneas
3.0 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 every node you reach is also a :py:class:`~.Wikicode` object, it's
  26. trivial to get nested templates::
  27. >>> code = mwparserfromhell.parse("{{foo|this {{includes a|template}}}}")
  28. >>> print code.filter_templates()
  29. ['{{foo|this {{includes a|template}}}}']
  30. >>> foo = code.filter_templates()[0]
  31. >>> print foo.get(1).value
  32. this {{includes a|template}}
  33. >>> print foo.get(1).value.filter_templates()[0]
  34. {{includes a|template}}
  35. >>> print foo.get(1).value.filter_templates()[0].get(1).value
  36. template
  37. Additionally, you can include nested templates in :py:meth:`~.filter_templates`
  38. by passing *recursive=True*::
  39. >>> text = "{{foo|{{bar}}={{baz|{{spam}}}}}}"
  40. >>> mwparserfromhell.parse(text).filter_templates(recursive=True)
  41. ['{{foo|{{bar}}={{baz|{{spam}}}}}}', '{{bar}}', '{{baz|{{spam}}}}', '{{spam}}']
  42. Templates can be easily modified to add, remove, or alter params.
  43. :py:class:`~.Wikicode` can also be treated like a list with
  44. :py:meth:`~.Wikicode.append`, :py:meth:`~.Wikicode.insert`,
  45. :py:meth:`~.Wikicode.remove`, :py:meth:`~.Wikicode.replace`, and more::
  46. >>> text = "{{cleanup}} '''Foo''' is a [[bar]]. {{uncategorized}}"
  47. >>> code = mwparserfromhell.parse(text)
  48. >>> for template in code.filter_templates():
  49. ... if template.name == "cleanup" and not template.has_param("date"):
  50. ... template.add("date", "July 2012")
  51. ...
  52. >>> print code
  53. {{cleanup|date=July 2012}} '''Foo''' is a [[bar]]. {{uncategorized}}
  54. >>> code.replace("{{uncategorized}}", "{{bar-stub}}")
  55. >>> print code
  56. {{cleanup|date=July 2012}} '''Foo''' is a [[bar]]. {{bar-stub}}
  57. >>> print code.filter_templates()
  58. ['{{cleanup|date=July 2012}}', '{{bar-stub}}']
  59. You can then convert ``code`` back into a regular :py:class:`unicode` object
  60. (for saving the page!) by calling :py:func:`unicode` on it::
  61. >>> text = unicode(code)
  62. >>> print text
  63. {{cleanup|date=July 2012}} '''Foo''' is a [[bar]]. {{bar-stub}}
  64. >>> text == code
  65. True
  66. (Likewise, use :py:func:`str(code) <str>` in Python 3.)
  67. For more tips, check out :py:class:`Wikicode's full method list <.Wikicode>`
  68. and the :py:mod:`list of Nodes <.nodes>`.