Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
@param wikicode: the parent from which to remove the element
@type wikicode: mwparserfromhell.wikicode.Wikicode
@param el: the element to remove
@type el: mwparserfromhell.nodes.Node
"""
index = wikicode.index(el)
try:
before = wikicode.get(index - 1)
if isinstance(before, mwparserfromhell.nodes.text.Text) and \
before.rstrip(' ').endswith('\n'):
wikicode.replace(before, before.rstrip(' '))
except IndexError:
pass
try:
after = wikicode.get(index + 1)
if isinstance(after, mwparserfromhell.nodes.text.Text):
lstripped = after.lstrip(' ')
if lstripped.startswith('\n\n'):
wikicode.replace(after, lstripped[2:])
elif lstripped.startswith('\n'):
wikicode.replace(after, lstripped[1:])
except IndexError:
pass
wikicode.remove(el)
def _split_text_node_on_endline(text_node):
text = text_node.value
lines = _split_text_and_leave_delimiter(text, "\n")
results = []
for line in lines:
if line != "":
results.append(mwp.nodes.text.Text(line))
return results
# look at our top-level nodes we have to handle
prev_node = None
for n in wikicode.nodes:
if type(n) in ignore_nodes:
continue
#yield mwparserfromhell.nodes.text.Text("\n")
elif type(n) == mwparserfromhell.nodes.tag.Tag and n.tag == "translate":
for n2 in mwparserfromhell.parse(n.contents).nodes:
if type(n2) in ignore_nodes:
continue
else:
yield n2
elif type(n) == mwparserfromhell.nodes.text.Text:
for bad_word in ignore_magic_words:
n = n.replace(bad_word, "")
n = mwparserfromhell.nodes.text.Text(n)
yield text_tokenize(n, prev_node)
else:
yield n
prev_node = n
def _get_text(index):
# the first node has no previous node, especially not the last node
if index < 0:
return None, None
try:
node = parent.get(index)
# don't EVER remove whitespace from non-Text nodes (it would
# modify the objects by converting to str, making the operation
# and replacing the object with str, but we keep references to
# the old nodes)
if not isinstance(node, mwparserfromhell.nodes.text.Text):
return None, mwparserfromhell.nodes.text.Text
return node, mwparserfromhell.nodes.text.Text
except IndexError:
return None, None