A Python parser for MediaWiki wikicode https://mwparserfromhell.readthedocs.io/
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

usage.rst 3.1 KiB

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