Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,25 @@ saving the page!) by calling ``str()`` on it:
>>> text == code
True

To get section headings, use ``filter_headings()``. Each ``Heading`` has a
``level`` and a ``title`` (a ``Wikicode`` object). Use ``recursive=False`` to
exclude headings nested inside other nodes, such as templates. This still
includes subheadings at the top level of the parsed text:

.. code-block:: python

>>> text = "== History ==\nText.\n=== [[Early history|Early years]] ===\nMore text."
>>> code = mwparserfromhell.parse(text)
>>> headings = code.filter_headings(recursive=False)
>>> [(heading.level, str(heading.title).strip()) for heading in headings]
[(2, 'History'), (3, '[[Early history|Early years]]')]
>>> [heading.title.strip_code().strip() for heading in headings]
['History', 'Early years']

Calling ``str()`` on the title preserves its wikitext; ``strip_code()`` removes
markup, and ``strip()`` removes surrounding whitespace. To get section content
as well as headings, use ``get_sections()``.

Limitations
-----------

Expand Down
19 changes: 19 additions & 0 deletions docs/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -78,5 +78,24 @@ saving the page!) by calling :func:`str` on it::
True


To get section headings, use ``filter_headings()``. Each ``Heading`` has a
``level`` and a ``title`` (a ``Wikicode`` object). Use ``recursive=False`` to
exclude headings nested inside other nodes, such as templates. This still
includes subheadings at the top level of the parsed text:

.. code-block:: python

>>> text = "== History ==\nText.\n=== [[Early history|Early years]] ===\nMore text."
>>> code = mwparserfromhell.parse(text)
>>> headings = code.filter_headings(recursive=False)
>>> [(heading.level, str(heading.title).strip()) for heading in headings]
[(2, 'History'), (3, '[[Early history|Early years]]')]
>>> [heading.title.strip_code().strip() for heading in headings]
['History', 'Early years']

Calling ``str()`` on the title preserves its wikitext; ``strip_code()`` removes
markup, and ``strip()`` removes surrounding whitespace. To get section content
as well as headings, use ``get_sections()``.

For more tips, check out :class:`Wikicode's full method list <.Wikicode>` and
the :mod:`list of Nodes <.nodes>`.