瀏覽代碼

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

tags/v0.1
Ben Kurtovic 12 年之前
父節點
當前提交
e6fa7b2b2d
共有 4 個文件被更改,包括 29 次插入2 次删除
  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 查看文件

@@ -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
-----------


+ 4
- 0
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:


+ 22
- 0
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 <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


+ 1
- 0
tests/__init__.py 查看文件

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

Loading…
取消
儲存