Bladeren bron

strip_code() takes a collapse argument to remove extraneous newlines.

tags/v0.1
Ben Kurtovic 12 jaren geleden
bovenliggende
commit
3146a2007f
1 gewijzigde bestanden met toevoegingen van 8 en 2 verwijderingen
  1. +8
    -2
      mwparserfromhell/wikicode.py

+ 8
- 2
mwparserfromhell/wikicode.py Bestand weergeven

@@ -206,7 +206,7 @@ class Wikicode(StringMixIn):
def filter_text(self, recursive=False, matches=None, flags=FLAGS):
return list(self.ifilter_text(recursive, matches, flags))

def strip_code(self, normalize=True):
def strip_code(self, normalize=True, collapse=True):
nodes = []
for node in self.nodes:
if isinstance(node, Text):
@@ -217,7 +217,13 @@ class Wikicode(StringMixIn):
else:
nodes.append(node)

return u" ".join(nodes)
if collapse:
stripped = u"".join(nodes).strip("\n")
while "\n\n\n" in stripped:
stripped = stripped.replace("\n\n\n", "\n\n")
return stripped
else:
return u"".join(nodes)

def get_tree(self):
marker = object() # Random object we can find with certainty in a list


Laden…
Annuleren
Opslaan