Skip to content

Commit

Permalink
Simplify docs massively.
Browse files Browse the repository at this point in the history
  • Loading branch information
earwig committed Aug 22, 2012
1 parent bdf5608 commit 816207b
Show file tree
Hide file tree
Showing 17 changed files with 129 additions and 164 deletions.
1 change: 1 addition & 0 deletions docs/api/mwparserfromhell.nodes.rst
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ nodes Package
.. automodule:: mwparserfromhell.nodes .. automodule:: mwparserfromhell.nodes


.. autoclass:: mwparserfromhell.nodes.Node .. autoclass:: mwparserfromhell.nodes.Node
:special-members:


:mod:`heading` Module :mod:`heading` Module
--------------------- ---------------------
Expand Down
2 changes: 1 addition & 1 deletion mwparserfromhell/__init__.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -37,4 +37,4 @@
from . import nodes, parser, smart_list, string_mixin, wikicode from . import nodes, parser, smart_list, string_mixin, wikicode


parse = lambda text: parser.Parser(text).parse() parse = lambda text: parser.Parser(text).parse()
parse.__doc__ = "Short for ``mwparserfromhell.parser.Parser(text).parse()``." parse.__doc__ = "Short for :py:meth:`.Parser.parse`."
32 changes: 15 additions & 17 deletions mwparserfromhell/nodes/__init__.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -21,14 +21,12 @@
# SOFTWARE. # SOFTWARE.


""" """
This package contains :py:class:`~mwparserfromhell.wikicode.Wikicode` "nodes", This package contains :py:class:`~.Wikicode` "nodes", which represent a single
which represent a single unit of wikitext, such as a Template, an HTML tag, unit of wikitext, such as a Template, an HTML tag, a Heading, or plain text.
a Heading, or plain text. The node "tree" is far from flat, as most types can The node "tree" is far from flat, as most types can contain additional
contain additional :py:class:`~mwparserfromhell.wikicode.Wikicode` types within :py:class:`~.Wikicode` types within them - and with that, more nodes. For
them - and with that, more nodes. For example, the name of a example, the name of a :py:class:`~.Template` is a :py:class:`~.Wikicode`
:py:class:`~mwparserfromhell.nodes.template.Template` is a object that can contain text or more templates.
:py:class:`~mwparserfromhell.wikicode.Wikicode` object that can contain text or
more templates.
""" """


from __future__ import unicode_literals from __future__ import unicode_literals
Expand All @@ -43,15 +41,15 @@ class Node(StringMixIn):
:py:meth:`__unicode__` must be overridden. It should return a ``unicode`` :py:meth:`__unicode__` must be overridden. It should return a ``unicode``
or (``str`` in py3k) representation of the node. If the node contains or (``str`` in py3k) representation of the node. If the node contains
:py:class:`~mwparserfromhell.wikicode.Wikicode` objects inside of it, :py:class:`~.Wikicode` objects inside of it, :py:meth:`__iternodes__`
:py:meth:`__iternodes__` should be overridden to yield tuples of should be overridden to yield tuples of (``wikicode``,
(``wikicode``, ``node_in_wikicode``) for each node in each wikicode, as ``node_in_wikicode``) for each node in each wikicode, as well as the node
well as the node itself (``None``, ``self``). If the node is printable, itself (``None``, ``self``). If the node is printable, :py:meth:`__strip__`
:py:meth:`__strip__` should be overridden to return the printable version should be overridden to return the printable version of the node - it does
of the node - it does not have to be a string, but something that can be not have to be a string, but something that can be converted to a string
converted to a string with ``str()``. Finally, :py:meth:`__showtree__` can with ``str()``. Finally, :py:meth:`__showtree__` can be overridden to build
be overridden to build a nice tree representation of the node, if desired, a nice tree representation of the node, if desired, for
for :py:meth:`~mwparserfromhell.wikicode.Wikicode.get_tree`. :py:meth:`~.Wikicode.get_tree`.
""" """
def __unicode__(self): def __unicode__(self):
raise NotImplementedError() raise NotImplementedError()
Expand Down
4 changes: 2 additions & 2 deletions mwparserfromhell/nodes/extras/__init__.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@


""" """
This package contains objects used by This package contains objects used by
:py:class:`~mwparserfromhell.nodes.Node`\ s, but are not nodes themselves. :py:class:`~.Node`\ s, but are not nodes themselves. This includes the
This includes the parameters of Templates or the attributes of HTML tags. parameters of Templates or the attributes of HTML tags.
""" """


from .attribute import Attribute from .attribute import Attribute
Expand Down
10 changes: 5 additions & 5 deletions mwparserfromhell/nodes/extras/attribute.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@
class Attribute(StringMixIn): class Attribute(StringMixIn):
"""Represents an attribute of an HTML tag. """Represents an attribute of an HTML tag.
This is used by :py:class:`~mwparserfromhell.nodes.tag.Tag` objects. For This is used by :py:class:`~.Tag` objects. For example, the tag
example, the tag ``<ref name="foo">`` contains an Attribute whose name is ``<ref name="foo">`` contains an Attribute whose name is ``"name"`` and
``"name"`` and whose value is ``"foo"``. whose value is ``"foo"``.
""" """


def __init__(self, name, value=None, quoted=True): def __init__(self, name, value=None, quoted=True):
Expand All @@ -50,12 +50,12 @@ def __unicode__(self):


@property @property
def name(self): def name(self):
"""The name of the attribute as a ``Wikicode`` object.""" """The name of the attribute as a :py:class:`~.Wikicode` object."""
return self._name return self._name


@property @property
def value(self): def value(self):
"""The value of the attribute as a ``Wikicode`` object.""" """The value of the attribute as a :py:class:`~.Wikicode` object."""
return self._value return self._value


@property @property
Expand Down
4 changes: 2 additions & 2 deletions mwparserfromhell/nodes/extras/parameter.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -50,12 +50,12 @@ def __unicode__(self):


@property @property
def name(self): def name(self):
"""The name of the parameter as a ``Wikicode`` object.""" """The name of the parameter as a :py:class:`~.Wikicode` object."""
return self._name return self._name


@property @property
def value(self): def value(self):
"""The value of the parameter as a ``Wikicode`` object.""" """The value of the parameter as a :py:class:`~.Wikicode` object."""
return self._value return self._value


@property @property
Expand Down
2 changes: 1 addition & 1 deletion mwparserfromhell/nodes/heading.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def __showtree__(self, write, get, mark):


@property @property
def title(self): def title(self):
"""The title of the heading itself, as a ``Wikicode`` object.""" """The title of the heading, as a :py:class:`~.Wikicode` object."""
return self._title return self._title


@property @property
Expand Down
7 changes: 3 additions & 4 deletions mwparserfromhell/nodes/tag.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -172,20 +172,19 @@ def type(self):


@property @property
def tag(self): def tag(self):
"""The tag itself, as a ``Wikicode`` object.""" """The tag itself, as a :py:class:`~.Wikicode` object."""
return self._tag return self._tag


@property @property
def contents(self): def contents(self):
"""The contents of the tag, as a ``Wikicode`` object.""" """The contents of the tag, as a :py:class:`~.Wikicode` object."""
return self._contents return self._contents


@property @property
def attrs(self): def attrs(self):
"""The list of attributes affecting the tag. """The list of attributes affecting the tag.
Each attribute is an instance of Each attribute is an instance of :py:class:`~.Attribute`.
:py:class:`~mwparserfromhell.nodes.extras.attribute.Attribute`.
""" """
return self._attrs return self._attrs


Expand Down
27 changes: 13 additions & 14 deletions mwparserfromhell/nodes/template.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ def _remove_without_field(self, param, i, force_no_field):


@property @property
def name(self): def name(self):
"""The name of the template, as a ``Wikicode`` object.""" """The name of the template, as a :py:class:`~.Wikicode` object."""
return self._name return self._name


@property @property
Expand Down Expand Up @@ -184,11 +184,10 @@ def get(self, name):
"""Get the parameter whose name is *name*. """Get the parameter whose name is *name*.
The returned object is a The returned object is a
:py:class:`~mwparserfromhell.nodes.extras.parameter.Parameter` :py:class:`~.Parameter` instance. Raises :py:exc:`ValueError` if no
instance. Raises :py:exc:`ValueError` if no parameter has this name. parameter has this name. Since multiple parameters can have the same
Since multiple parameters can have the same name, we'll return the last name, we'll return the last match, since the last parameter is the only
match, since the last parameter is the only one read by the MediaWiki one read by the MediaWiki parser.
parser.
""" """
name = name.strip() if isinstance(name, basestring) else str(name) name = name.strip() if isinstance(name, basestring) else str(name)
for param in reversed(self.params): for param in reversed(self.params):
Expand All @@ -200,14 +199,14 @@ def add(self, name, value, showkey=None, force_nonconformity=False):
"""Add a parameter to the template with a given *name* and *value*. """Add a parameter to the template with a given *name* and *value*.
*name* and *value* can be anything parasable by *name* and *value* can be anything parasable by
:py:func:`mwparserfromhell.utils.parse_anything`; pipes (and equal :py:func:`.utils.parse_anything`; pipes (and equal signs, if
signs, if appropriate) are automatically escaped from *value* where appropriate) are automatically escaped from *value* where applicable.
applicable. If *showkey* is given, this will determine whether or not If *showkey* is given, this will determine whether or not to show the
to show the parameter's name (e.g., ``{{foo|bar}}``'s parameter has a parameter's name (e.g., ``{{foo|bar}}``'s parameter has a name of
name of ``"1"`` but it is hidden); otherwise, we'll make a safe and ``"1"`` but it is hidden); otherwise, we'll make a safe and intelligent
intelligent guess. If *name* is already a parameter, we'll replace its guess. If *name* is already a parameter, we'll replace its value while
value while keeping the same spacing rules unless *force_nonconformity* keeping the same spacing rules unless *force_nonconformity* is
is ``True``. We will also try to guess the dominant spacing convention ``True``. We will also try to guess the dominant spacing convention
when adding a new parameter using :py:meth:`_get_spacing_conventions` when adding a new parameter using :py:meth:`_get_spacing_conventions`
unless *force_nonconformity* is ``True``. unless *force_nonconformity* is ``True``.
""" """
Expand Down
16 changes: 6 additions & 10 deletions mwparserfromhell/parser/__init__.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -22,9 +22,8 @@


""" """
This package contains the actual wikicode parser, split up into two main This package contains the actual wikicode parser, split up into two main
modules: the :py:mod:`~mwparserfromhell.parser.tokenizer` and the modules: the :py:mod:`~.tokenizer` and the :py:mod:`~.builder`. This module
:py:mod:`~mwparserfromhell.parser.builder`. This module joins them together joins them together under one interface.
under one interface.
""" """


try: try:
Expand All @@ -40,12 +39,9 @@ class Parser(object):
"""Represents a parser for wikicode. """Represents a parser for wikicode.
Actual parsing is a two-step process: first, the text is split up into a Actual parsing is a two-step process: first, the text is split up into a
series of tokens by the series of tokens by the :py:class:`~.Tokenizer`, and then the tokens are
:py:class:`~mwparserfromhell.parser.tokenizer.Tokenizer`, and then the converted into trees of :py:class:`~.Wikicode` objects and
tokens are converted into trees of :py:class:`~.Node`\ s by the :py:class:`~.Builder`.
:py:class:`~mwparserfromhell.wikicode.Wikicode` objects and
:py:class:`~mwparserfromhell.nodes.Node`\ s by the
:py:class:`~mwparserfromhell.parser.builder.Builder`.
""" """


def __init__(self, text): def __init__(self, text):
Expand All @@ -54,7 +50,7 @@ def __init__(self, text):
self._builder = Builder() self._builder = Builder()


def parse(self): def parse(self):
"""Return a string as a parsed ``Wikicode`` object tree.""" """Return a string as a parsed :py:class:`~.Wikicode` object tree."""
tokens = self._tokenizer.tokenize(self.text) tokens = self._tokenizer.tokenize(self.text)
code = self._builder.build(tokens) code = self._builder.build(tokens)
return code return code
7 changes: 3 additions & 4 deletions mwparserfromhell/parser/builder.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -34,10 +34,9 @@
class Builder(object): class Builder(object):
"""Combines a sequence of tokens into a tree of ``Wikicode`` objects. """Combines a sequence of tokens into a tree of ``Wikicode`` objects.
To use, pass a list of :py:class:`~mwparserfromhell.parser.tokens.Token`\ s To use, pass a list of :py:class:`~.Token`\ s to the :py:meth:`build`
to the :py:meth:`build` method. The list will be exhausted as it is parsed method. The list will be exhausted as it is parsed and a
and a :py:class:`~mwparserfromhell.wikicode.Wikicode` object will be :py:class:`~.Wikicode` object will be returned.
returned.
""" """


def __init__(self): def __init__(self):
Expand Down
2 changes: 1 addition & 1 deletion mwparserfromhell/parser/tokenizer.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def _fail_route(self):
"""Fail the current tokenization route. """Fail the current tokenization route.
Discards the current stack/context/textbuffer and raises Discards the current stack/context/textbuffer and raises
:py:exc:`~mwparserfromhell.parser.tokenizer.BadRoute`. :py:exc:`~.BadRoute`.
""" """
self._pop() self._pop()
raise BadRoute() raise BadRoute()
Expand Down
6 changes: 2 additions & 4 deletions mwparserfromhell/parser/tokens.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -24,10 +24,8 @@
This module contains the token definitions that are used as an intermediate This module contains the token definitions that are used as an intermediate
parsing data type - they are stored in a flat list, with each token being parsing data type - they are stored in a flat list, with each token being
identified by its type and optional attributes. The token list is generated in identified by its type and optional attributes. The token list is generated in
a syntactically valid form by the a syntactically valid form by the :py:class:`~.Tokenizer`, and then converted
:py:class:`~mwparserfromhell.parser.tokenizer.Tokenizer`, and then converted into the :py:class`~.Wikicode` tree by the :py:class:`~.Builder`.
into the :py:class`~mwparserfromhell.wikicode.Wikicode` tree by the
:py:class:`~mwparserfromhell.parser.builder.Builder`.
""" """


from __future__ import unicode_literals from __future__ import unicode_literals
Expand Down
26 changes: 11 additions & 15 deletions mwparserfromhell/smart_list.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -21,10 +21,9 @@
# SOFTWARE. # SOFTWARE.


""" """
This module contains the :py:class:`~mwparserfromhell.smart_list.SmartList` This module contains the :py:class:`~.SmartList` type, as well as its
type, as well as its :py:class:`~mwparserfromhell.smart_list._ListProxy` child, :py:class:`~._ListProxy` child, which together implement a list whose sublists
which together implement a list whose sublists reflect changes made to the main reflect changes made to the main list, and vice-versa.
list, and vice-versa.
""" """


from __future__ import unicode_literals from __future__ import unicode_literals
Expand All @@ -36,9 +35,8 @@
def inheritdoc(method): def inheritdoc(method):
"""Set __doc__ of *method* to __doc__ of *method* in its parent class. """Set __doc__ of *method* to __doc__ of *method* in its parent class.
Since this is used on Since this is used on :py:class:`~.SmartList`, the "parent class" used is
:py:class:`~mwparserfromhell.smart_list.SmartList`, the "parent class" used ``list``. This function can be used as a decorator.
is ``list``. This function can be used as a decorator.
""" """
method.__doc__ = getattr(list, method.__name__).__doc__ method.__doc__ = getattr(list, method.__name__).__doc__
return method return method
Expand All @@ -51,10 +49,9 @@ class SmartList(list):
list (such as the addition, removal, or replacement of elements) will be list (such as the addition, removal, or replacement of elements) will be
reflected in the sublist, or vice-versa, to the greatest degree possible. reflected in the sublist, or vice-versa, to the greatest degree possible.
This is implemented by having sublists - instances of the This is implemented by having sublists - instances of the
:py:class:`~mwparserfromhell.smart_list._ListProxy` type - dynamically :py:class:`~._ListProxy` type - dynamically determine their elements by
determine their elements by storing their slice info and retrieving that storing their slice info and retrieving that slice from the parent. Methods
slice from the parent. Methods that change the size of the list also change that change the size of the list also change the slice info. For example::
the slice info. For example::
>>> parent = SmartList([0, 1, 2, 3]) >>> parent = SmartList([0, 1, 2, 3])
>>> parent >>> parent
Expand Down Expand Up @@ -183,10 +180,9 @@ def sort(self, cmp=None, key=None, reverse=None):
class _ListProxy(list): class _ListProxy(list):
"""Implement the ``list`` interface by getting elements from a parent. """Implement the ``list`` interface by getting elements from a parent.
This is created by a :py:class:`~mwparserfromhell.smart_list.SmartList` This is created by a :py:class:`~.SmartList` object when slicing. It does
object when slicing. It does not actually store the list at any time; not actually store the list at any time; instead, whenever the list is
instead, whenever the list is needed, it builds it dynamically using the needed, it builds it dynamically using the :py:meth:`_render` method.
:py:meth:`_render` method.
""" """


def __init__(self, parent, sliceinfo): def __init__(self, parent, sliceinfo):
Expand Down
10 changes: 4 additions & 6 deletions mwparserfromhell/string_mixin.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -21,9 +21,8 @@
# SOFTWARE. # SOFTWARE.


""" """
This module contains the :py:class:`~mwparserfromhell.string_mixin.StringMixIn` This module contains the :py:class:`~.StringMixIn` type, which implements the
type, which implements the interface for the ``unicode`` type (``str`` on py3k) interface for the ``unicode`` type (``str`` on py3k) in a dynamic manner.
in a dynamic manner.
""" """


from __future__ import unicode_literals from __future__ import unicode_literals
Expand All @@ -35,9 +34,8 @@
def inheritdoc(method): def inheritdoc(method):
"""Set __doc__ of *method* to __doc__ of *method* in its parent class. """Set __doc__ of *method* to __doc__ of *method* in its parent class.
Since this is used on Since this is used on :py:class:`~.StringMixIn`, the "parent class" used is
:py:class:`~mwparserfromhell.string_mixin.StringMixIn`, the "parent class" ``str``. This function can be used as a decorator.
used is ``str``. This function can be used as a decorator.
""" """
method.__doc__ = getattr(str, method.__name__).__doc__ method.__doc__ = getattr(str, method.__name__).__doc__
return method return method
Expand Down
14 changes: 5 additions & 9 deletions mwparserfromhell/utils.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -33,20 +33,16 @@
from .smart_list import SmartList from .smart_list import SmartList


def parse_anything(value): def parse_anything(value):
"""Return a :py:class:`~mwparserfromhell.wikicode.Wikicode` for *value*. """Return a :py:class:`~.Wikicode` for *value*, allowing multiple types.
This differs from :py:func:`mwparserfromhell.parse` in that we accept more This differs from :py:func:`mwparserfromhell.parse` in that we accept more
than just a string to be parsed. Unicode objects (strings in py3k), strings than just a string to be parsed. Unicode objects (strings in py3k), strings
(bytes in py3k), integers (converted to strings), ``None``, existing (bytes in py3k), integers (converted to strings), ``None``, existing
:py:class:`~mwparserfromhell.nodes.Node` or :py:class:`~.Node` or :py:class:`~.Wikicode` objects, as well as an
:py:class:`~mwparserfromhell.wikicode.Wikicode` objects, as well as an
iterable of these types, are supported. This is used to parse input iterable of these types, are supported. This is used to parse input
on-the-fly by various methods of on-the-fly by various methods of :py:class:`~.Wikicode` and others like
:py:class:`~mwparserfromhell.wikicode.Wikicode` and others like :py:class:`~.Template`, such as :py:meth:`wikicode.insert()
:py:class:`~mwparserfromhell.nodes.template.Template`, such as <.Wikicode.insert>` or setting :py:meth:`template.name <.Template.name>`.
:py:meth:`wikicode.insert() <mwparserfromhell.wikicode.Wikicode.insert>`
or setting :py:meth:`template.name
<mwparserfromhell.nodes.template.Template.name>`.
""" """
wikicode = mwparserfromhell.wikicode.Wikicode wikicode = mwparserfromhell.wikicode.Wikicode
if isinstance(value, wikicode): if isinstance(value, wikicode):
Expand Down
Loading

0 comments on commit 816207b

Please sign in to comment.