Browse Source

Handle Wikicode contexts correctly.

tags/v0.1
Ben Kurtovic 12 years ago
parent
commit
62b997d2fd
1 changed files with 10 additions and 8 deletions
  1. +10
    -8
      mwparserfromhell/wikicode.py

+ 10
- 8
mwparserfromhell/wikicode.py View File

@@ -84,16 +84,18 @@ class Wikicode(StringMixIn):
return True return True
return False return False


def _do_search(self, obj, value, recursive, callback):
def _do_search(self, obj, value, recursive, callback, context=None):
if recursive: if recursive:
for i, node in enumerate(self.nodes):
nodes = context.nodes if context else self.nodes
for i, node in enumerate(nodes):
if self._is_equivalent(obj, node): if self._is_equivalent(obj, node):
return callback(value, i)
return callback(self, value, i)
if self._contains(self._get_children(node), obj): if self._contains(self._get_children(node), obj):
return self._do_search(obj, value, recursive, callback)
return self._do_search(obj, value, recursive, callback,
context=obj)
raise ValueError(obj) raise ValueError(obj)


callback(value, self.index(obj, recursive=False))
callback(self, value, self.index(obj, recursive=False))


def _get_tree(self, code, lines, marker=None, indent=0): def _get_tree(self, code, lines, marker=None, indent=0):
def write(*args): def write(*args):
@@ -157,15 +159,15 @@ class Wikicode(StringMixIn):
self.nodes.insert(index, node) self.nodes.insert(index, node)


def insert_before(self, obj, value, recursive=True): def insert_before(self, obj, value, recursive=True):
callback = lambda value, i: self.insert(i, value)
callback = lambda self, value, i: self.insert(i, value)
self._do_search(obj, value, recursive, callback) self._do_search(obj, value, recursive, callback)


def insert_after(self, obj, value, recursive=True): def insert_after(self, obj, value, recursive=True):
callback = lambda value, i: self.insert(i + 1, value)
callback = lambda self, value, i: self.insert(i + 1, value)
self._do_search(obj, value, recursive, callback) self._do_search(obj, value, recursive, callback)


def replace(self, obj, value, recursive=True): def replace(self, obj, value, recursive=True):
def callback(value, i):
def callback(self, value, i):
self.nodes.pop(i) self.nodes.pop(i)
self.insert(i, value) self.insert(i, value)




Loading…
Cancel
Save