diff --git a/README.rst b/README.rst index d1eb3b3..94833bc 100644 --- a/README.rst +++ b/README.rst @@ -29,7 +29,7 @@ Normal usage is rather straightforward (where ``text`` is page text):: >>> wikicode = mwparserfromhell.parse(text) ``wikicode`` is a ``mwparserfromhell.wikicode.Wikicode`` object, which acts -like an ordinary ``unicode`` object (or ``str`` on Python 3) with some extra +like an ordinary ``unicode`` object (or ``str`` in Python 3) with some extra methods. For example:: >>> text = "I has a template! {{foo|bar|baz|eggs=spam}} See it?" @@ -97,7 +97,7 @@ saving the page!) by calling ``unicode()`` on it:: >>> text == code True -Likewise, in Python 3, use ``str(code)``. +Likewise, use ``str(code)`` in Python 3. Integration ----------- diff --git a/mwparserfromhell/nodes/template.py b/mwparserfromhell/nodes/template.py index 581e8ce..714d12c 100644 --- a/mwparserfromhell/nodes/template.py +++ b/mwparserfromhell/nodes/template.py @@ -123,6 +123,10 @@ class Template(Node): def params(self): return self._params + @name.setter + def name(self, value): + self._name = parse_anything(value) + def has_param(self, name, ignore_empty=True): name = name.strip() if isinstance(name, basestring) else unicode(name) for param in self.params: diff --git a/mwparserfromhell/wikicode.py b/mwparserfromhell/wikicode.py index 1199e4b..d462e9b 100644 --- a/mwparserfromhell/wikicode.py +++ b/mwparserfromhell/wikicode.py @@ -32,6 +32,9 @@ __all__ = ["Wikicode"] FLAGS = re.IGNORECASE | re.DOTALL | re.UNICODE class Wikicode(StringMixIn): + """A ``Wikicode`` is a container for nodes that functions like a string. + """ + def __init__(self, nodes): super(Wikicode, self).__init__() self._nodes = nodes @@ -40,21 +43,40 @@ class Wikicode(StringMixIn): return "".join([unicode(node) for node in self.nodes]) def _get_children(self, node): + """Iterate over all descendants of a given node, including itself. + + This is implemented by the __iternodes__() generator of Node classes, + which by default yields itself and nothing more. + """ for context, child in node.__iternodes__(self._get_all_nodes): yield child def _get_context(self, node, obj): + """Return a ``Wikicode`` that contains ``obj`` in its descendants. + + The closest (shortest distance from ``node``) suitable ``Wikicode`` + will be returned, or ``None`` if the ``obj`` is the ``node`` itself. + + Raises ``ValueError`` if ``obj`` is not within ``node``. + """ for context, child in node.__iternodes__(self._get_all_nodes): if child is obj: return context raise ValueError(obj) def _get_all_nodes(self, code): + """Iterate over all of our descendant nodes. + + This is implemented by calling :py:meth:`_get_children` on every node + in our node list (:py:attr:`self.nodes `). + """ for node in code.nodes: for child in self._get_children(node): yield child def _is_equivalent(self, obj, node): + """Return ``True`` if obj and node are equivalent, otherwise ``False``. + """ if isinstance(obj, Node): if node is obj: return True diff --git a/tests/__init__.py b/tests/__init__.py index e69de29..89907bf 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -0,0 +1 @@ +# -*- coding: utf-8 -*-