Browse Source

Move get_children() out of utils.

tags/v0.3.3
Ben Kurtovic 11 years ago
parent
commit
f0a591b323
2 changed files with 16 additions and 14 deletions
  1. +1
    -10
      mwparserfromhell/utils.py
  2. +15
    -4
      mwparserfromhell/wikicode.py

+ 1
- 10
mwparserfromhell/utils.py View File

@@ -31,16 +31,7 @@ from .compat import bytes, str
from .nodes import Node from .nodes import Node
from .smart_list import SmartList from .smart_list import SmartList


__all__ = ["get_children", "parse_anything"]

def get_children(node, contexts=False, parent=None):
"""Iterate over all child :py:class:`.Node`\ s of a given *node*."""
## DON'T MAKE THIS RECURSIVE, USE A STACK!
yield (parent, node) if contexts else node
for code in node.__children__():
for descendant in code.nodes:
for child in get_children(descendant, contexts, code):
yield child
__all__ = ["parse_anything"]


def parse_anything(value, context=0): def parse_anything(value, context=0):
"""Return a :py:class:`~.Wikicode` for *value*, allowing multiple types. """Return a :py:class:`~.Wikicode` for *value*, allowing multiple types.


+ 15
- 4
mwparserfromhell/wikicode.py View File

@@ -21,6 +21,7 @@
# SOFTWARE. # SOFTWARE.


from __future__ import unicode_literals from __future__ import unicode_literals
from collections import deque
from itertools import chain from itertools import chain
import re import re


@@ -28,7 +29,7 @@ from .compat import py3k, range, str
from .nodes import (Argument, Comment, ExternalLink, Heading, HTMLEntity, from .nodes import (Argument, Comment, ExternalLink, Heading, HTMLEntity,
Node, Tag, Template, Text, Wikilink) Node, Tag, Template, Text, Wikilink)
from .string_mixin import StringMixIn from .string_mixin import StringMixIn
from .utils import get_children, parse_anything
from .utils import parse_anything


__all__ = ["Wikicode"] __all__ = ["Wikicode"]


@@ -53,6 +54,15 @@ class Wikicode(StringMixIn):
return "".join([str(node) for node in self.nodes]) return "".join([str(node) for node in self.nodes])


@staticmethod @staticmethod
def _get_children(node, contexts=False, parent=None):
"""Iterate over all child :py:class:`.Node`\ s of a given *node*."""
yield (parent, node) if contexts else node
for code in node.__children__():
for child in code.nodes:
for result in Wikicode._get_children(child, contexts, code):
yield result

@staticmethod
def _slice_replace(code, index, old, new): def _slice_replace(code, index, old, new):
"""Replace the string *old* with *new* across *index* in *code*.""" """Replace the string *old* with *new* across *index* in *code*."""
nodes = [str(node) for node in code.get(index)] nodes = [str(node) for node in code.get(index)]
@@ -76,7 +86,7 @@ class Wikicode(StringMixIn):
if not recursive: if not recursive:
return self, mkslice(self.index(obj)) return self, mkslice(self.index(obj))
for i, node in enumerate(self.nodes): for i, node in enumerate(self.nodes):
for context, child in get_children(node, contexts=True):
for context, child in self._get_children(node, contexts=True):
if obj is child: if obj is child:
if not context: if not context:
context = self context = self
@@ -240,7 +250,7 @@ class Wikicode(StringMixIn):
equivalent = (lambda o, n: o is n) if strict else (lambda o, n: o == n) equivalent = (lambda o, n: o is n) if strict else (lambda o, n: o == n)
for i, node in enumerate(self.nodes): for i, node in enumerate(self.nodes):
if recursive: if recursive:
for child in get_children(node):
for child in self._get_children(node):
if equivalent(obj, child): if equivalent(obj, child):
return i return i
elif equivalent(obj, node): elif equivalent(obj, node):
@@ -405,7 +415,8 @@ class Wikicode(StringMixIn):
if matches and not callable(matches): if matches and not callable(matches):
pat, matches = matches, lambda obj: re.search(pat, str(obj), flags) pat, matches = matches, lambda obj: re.search(pat, str(obj), flags)
if recursive: if recursive:
nodes = chain.from_iterable(get_children(n) for n in self.nodes)
getter = self._get_children
nodes = chain.from_iterable(getter(n) for n in self.nodes)
else: else:
nodes = self.nodes nodes = self.nodes
for node in nodes: for node in nodes:


Loading…
Cancel
Save