Browse Source

Support changing a template's name, plus a couple of typos and docs.

tags/v0.1
Ben Kurtovic 12 years ago
parent
commit
e6fa7b2b2d
4 changed files with 29 additions and 2 deletions
  1. +2
    -2
      README.rst
  2. +4
    -0
      mwparserfromhell/nodes/template.py
  3. +22
    -0
      mwparserfromhell/wikicode.py
  4. +1
    -0
      tests/__init__.py

+ 2
- 2
README.rst View File

@@ -29,7 +29,7 @@ Normal usage is rather straightforward (where ``text`` is page text)::
>>> wikicode = mwparserfromhell.parse(text) >>> wikicode = mwparserfromhell.parse(text)


``wikicode`` is a ``mwparserfromhell.wikicode.Wikicode`` object, which acts ``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:: methods. For example::


>>> text = "I has a template! {{foo|bar|baz|eggs=spam}} See it?" >>> 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 >>> text == code
True True


Likewise, in Python 3, use ``str(code)``.
Likewise, use ``str(code)`` in Python 3.


Integration Integration
----------- -----------


+ 4
- 0
mwparserfromhell/nodes/template.py View File

@@ -123,6 +123,10 @@ class Template(Node):
def params(self): def params(self):
return self._params return self._params


@name.setter
def name(self, value):
self._name = parse_anything(value)

def has_param(self, name, ignore_empty=True): def has_param(self, name, ignore_empty=True):
name = name.strip() if isinstance(name, basestring) else unicode(name) name = name.strip() if isinstance(name, basestring) else unicode(name)
for param in self.params: for param in self.params:


+ 22
- 0
mwparserfromhell/wikicode.py View File

@@ -32,6 +32,9 @@ __all__ = ["Wikicode"]
FLAGS = re.IGNORECASE | re.DOTALL | re.UNICODE FLAGS = re.IGNORECASE | re.DOTALL | re.UNICODE


class Wikicode(StringMixIn): class Wikicode(StringMixIn):
"""A ``Wikicode`` is a container for nodes that functions like a string.
"""

def __init__(self, nodes): def __init__(self, nodes):
super(Wikicode, self).__init__() super(Wikicode, self).__init__()
self._nodes = nodes self._nodes = nodes
@@ -40,21 +43,40 @@ class Wikicode(StringMixIn):
return "".join([unicode(node) for node in self.nodes]) return "".join([unicode(node) for node in self.nodes])


def _get_children(self, node): 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): for context, child in node.__iternodes__(self._get_all_nodes):
yield child yield child


def _get_context(self, node, obj): 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): for context, child in node.__iternodes__(self._get_all_nodes):
if child is obj: if child is obj:
return context return context
raise ValueError(obj) raise ValueError(obj)


def _get_all_nodes(self, code): 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 <nodes>`).
"""
for node in code.nodes: for node in code.nodes:
for child in self._get_children(node): for child in self._get_children(node):
yield child yield child


def _is_equivalent(self, obj, node): def _is_equivalent(self, obj, node):
"""Return ``True`` if obj and node are equivalent, otherwise ``False``.
"""
if isinstance(obj, Node): if isinstance(obj, Node):
if node is obj: if node is obj:
return True return True


+ 1
- 0
tests/__init__.py View File

@@ -0,0 +1 @@
# -*- coding: utf-8 -*-

Loading…
Cancel
Save