Преглед изворни кода

Move get_children() out of utils.

tags/v0.3.3
Ben Kurtovic пре 11 година
родитељ
комит
f0a591b323
2 измењених фајлова са 16 додато и 14 уклоњено
  1. +1
    -10
      mwparserfromhell/utils.py
  2. +15
    -4
      mwparserfromhell/wikicode.py

+ 1
- 10
mwparserfromhell/utils.py Прегледај датотеку

@@ -31,16 +31,7 @@ from .compat import bytes, str
from .nodes import Node
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):
"""Return a :py:class:`~.Wikicode` for *value*, allowing multiple types.


+ 15
- 4
mwparserfromhell/wikicode.py Прегледај датотеку

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

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

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

__all__ = ["Wikicode"]

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

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


Loading…
Откажи
Сачувај