Browse Source

Change protocol for Node iteration; rename __iternodes__ to __children__.

tags/v0.3.3
Ben Kurtovic 10 years ago
parent
commit
99d433c2d5
7 changed files with 32 additions and 53 deletions
  1. +10
    -10
      mwparserfromhell/nodes/__init__.py
  2. +3
    -6
      mwparserfromhell/nodes/argument.py
  3. +3
    -6
      mwparserfromhell/nodes/external_link.py
  4. +2
    -4
      mwparserfromhell/nodes/heading.py
  5. +7
    -13
      mwparserfromhell/nodes/tag.py
  6. +4
    -8
      mwparserfromhell/nodes/template.py
  7. +3
    -6
      mwparserfromhell/nodes/wikilink.py

+ 10
- 10
mwparserfromhell/nodes/__init__.py View File

@@ -42,21 +42,21 @@ class Node(StringMixIn):

:py:meth:`__unicode__` must be overridden. It should return a ``unicode``
or (``str`` in py3k) representation of the node. If the node contains
:py:class:`~.Wikicode` objects inside of it, :py:meth:`__iternodes__`
should be overridden to yield tuples of (``wikicode``,
``node_in_wikicode``) for each node in each wikicode, as well as the node
itself (``None``, ``self``). If the node is printable, :py:meth:`__strip__`
should be overridden to return the printable version of the node - it does
not have to be a string, but something that can be converted to a string
with ``str()``. Finally, :py:meth:`__showtree__` can be overridden to build
a nice tree representation of the node, if desired, for
:py:class:`~.Wikicode` objects inside of it, :py:meth:`__children__`
should be a generator that iterates over them. If the node is printable
(shown when the page is rendered), :py:meth:`__strip__` should return its
printable version, stripping out any formatting marks. It does not have to
return a string, but something that can be converted to a string with
``str()``. Finally, :py:meth:`__showtree__` can be overridden to build a
nice tree representation of the node, if desired, for
:py:meth:`~.Wikicode.get_tree`.
"""
def __unicode__(self):
raise NotImplementedError()

def __iternodes__(self, getter):
yield None, self
def __children__(self):
return # Funny generator-that-yields-nothing syntax
yield

def __strip__(self, normalize, collapse):
return None


+ 3
- 6
mwparserfromhell/nodes/argument.py View File

@@ -42,13 +42,10 @@ class Argument(Node):
return start + "|" + str(self.default) + "}}}"
return start + "}}}"

def __iternodes__(self, getter):
yield None, self
for child in getter(self.name):
yield self.name, child
def __children__(self):
yield self.name
if self.default is not None:
for child in getter(self.default):
yield self.default, child
yield self.default

def __strip__(self, normalize, collapse):
if self.default is not None:


+ 3
- 6
mwparserfromhell/nodes/external_link.py View File

@@ -44,13 +44,10 @@ class ExternalLink(Node):
return "[" + str(self.url) + "]"
return str(self.url)

def __iternodes__(self, getter):
yield None, self
for child in getter(self.url):
yield self.url, child
def __children__(self):
yield self.url
if self.title is not None:
for child in getter(self.title):
yield self.title, child
yield self.title

def __strip__(self, normalize, collapse):
if self.brackets:


+ 2
- 4
mwparserfromhell/nodes/heading.py View File

@@ -39,10 +39,8 @@ class Heading(Node):
def __unicode__(self):
return ("=" * self.level) + str(self.title) + ("=" * self.level)

def __iternodes__(self, getter):
yield None, self
for child in getter(self.title):
yield self.title, child
def __children__(self):
yield self.title

def __strip__(self, normalize, collapse):
return self.title.strip_code(normalize, collapse)


+ 7
- 13
mwparserfromhell/nodes/tag.py View File

@@ -70,23 +70,17 @@ class Tag(Node):
result += "</" + str(self.closing_tag) + ">"
return result

def __iternodes__(self, getter):
yield None, self
def __children__(self):
if not self.wiki_markup:
for child in getter(self.tag):
yield self.tag, child
yield self.tag
for attr in self.attributes:
for child in getter(attr.name):
yield attr.name, child
if attr.value:
for child in getter(attr.value):
yield attr.value, child
yield attr.name
if attr.value is not None:
yield attr.value
if self.contents:
for child in getter(self.contents):
yield self.contents, child
yield self.contents
if not self.self_closing and not self.wiki_markup and self.closing_tag:
for child in getter(self.closing_tag):
yield self.closing_tag, child
yield self.closing_tag

def __strip__(self, normalize, collapse):
if self.contents and is_visible(self.tag):


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

@@ -51,16 +51,12 @@ class Template(Node):
else:
return "{{" + str(self.name) + "}}"

def __iternodes__(self, getter):
yield None, self
for child in getter(self.name):
yield self.name, child
def __children__(self):
yield self.name
for param in self.params:
if param.showkey:
for child in getter(param.name):
yield param.name, child
for child in getter(param.value):
yield param.value, child
yield param.name
yield param.value

def __showtree__(self, write, get, mark):
write("{{")


+ 3
- 6
mwparserfromhell/nodes/wikilink.py View File

@@ -41,13 +41,10 @@ class Wikilink(Node):
return "[[" + str(self.title) + "|" + str(self.text) + "]]"
return "[[" + str(self.title) + "]]"

def __iternodes__(self, getter):
yield None, self
for child in getter(self.title):
yield self.title, child
def __children__(self):
yield self.title
if self.text is not None:
for child in getter(self.text):
yield self.text, child
yield self.text

def __strip__(self, normalize, collapse):
if self.text is not None:


Loading…
Cancel
Save