From 99d433c2d55bbf5d12d9c76ae324b65de825d17e Mon Sep 17 00:00:00 2001 From: Ben Kurtovic Date: Wed, 27 Nov 2013 21:07:42 -0500 Subject: [PATCH] Change protocol for Node iteration; rename __iternodes__ to __children__. --- mwparserfromhell/nodes/__init__.py | 20 ++++++++++---------- mwparserfromhell/nodes/argument.py | 9 +++------ mwparserfromhell/nodes/external_link.py | 9 +++------ mwparserfromhell/nodes/heading.py | 6 ++---- mwparserfromhell/nodes/tag.py | 20 +++++++------------- mwparserfromhell/nodes/template.py | 12 ++++-------- mwparserfromhell/nodes/wikilink.py | 9 +++------ 7 files changed, 32 insertions(+), 53 deletions(-) diff --git a/mwparserfromhell/nodes/__init__.py b/mwparserfromhell/nodes/__init__.py index 0b3b326..91afb23 100644 --- a/mwparserfromhell/nodes/__init__.py +++ b/mwparserfromhell/nodes/__init__.py @@ -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 diff --git a/mwparserfromhell/nodes/argument.py b/mwparserfromhell/nodes/argument.py index f30cddb..d28d979 100644 --- a/mwparserfromhell/nodes/argument.py +++ b/mwparserfromhell/nodes/argument.py @@ -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: diff --git a/mwparserfromhell/nodes/external_link.py b/mwparserfromhell/nodes/external_link.py index 1678eb1..89eab1f 100644 --- a/mwparserfromhell/nodes/external_link.py +++ b/mwparserfromhell/nodes/external_link.py @@ -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: diff --git a/mwparserfromhell/nodes/heading.py b/mwparserfromhell/nodes/heading.py index eecc65d..e3a0ae5 100644 --- a/mwparserfromhell/nodes/heading.py +++ b/mwparserfromhell/nodes/heading.py @@ -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) diff --git a/mwparserfromhell/nodes/tag.py b/mwparserfromhell/nodes/tag.py index 775abbd..6869c72 100644 --- a/mwparserfromhell/nodes/tag.py +++ b/mwparserfromhell/nodes/tag.py @@ -70,23 +70,17 @@ class Tag(Node): result += "" 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): diff --git a/mwparserfromhell/nodes/template.py b/mwparserfromhell/nodes/template.py index e7b4cd3..c9f99bf 100644 --- a/mwparserfromhell/nodes/template.py +++ b/mwparserfromhell/nodes/template.py @@ -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("{{") diff --git a/mwparserfromhell/nodes/wikilink.py b/mwparserfromhell/nodes/wikilink.py index 6ac092c..f730697 100644 --- a/mwparserfromhell/nodes/wikilink.py +++ b/mwparserfromhell/nodes/wikilink.py @@ -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: