@@ -25,7 +25,8 @@ from mwparserfromhell.string_mixin import StringMixIn | |||||
__all__ = ["Node"] | __all__ = ["Node"] | ||||
class Node(StringMixIn): | class Node(StringMixIn): | ||||
pass | |||||
def __strip__(self, normalize=True, collapse=True): | |||||
return None | |||||
from mwparserfromhell.nodes import extras | from mwparserfromhell.nodes import extras | ||||
from mwparserfromhell.nodes.text import Text | from mwparserfromhell.nodes.text import Text | ||||
@@ -32,6 +32,9 @@ class Heading(Node): | |||||
def __unicode__(self): | def __unicode__(self): | ||||
return ("=" * self.level) + self.title + ("=" * self.level) | return ("=" * self.level) + self.title + ("=" * self.level) | ||||
def __strip__(self, normalize=True, collapse=True): | |||||
return self.title | |||||
@property | @property | ||||
def title(self): | def title(self): | ||||
return self._title | return self._title | ||||
@@ -53,6 +53,11 @@ class HTMLEntity(Node): | |||||
return u"&#x{0};".format(self.value) | return u"&#x{0};".format(self.value) | ||||
return u"&#{0};".format(self.value) | return u"&#{0};".format(self.value) | ||||
def __strip__(self, normalize=True, collapse=True): | |||||
if normalize: | |||||
return self.normalize() | |||||
return self | |||||
def _unichr(self, value): | def _unichr(self, value): | ||||
"""Implement the builtin unichr() with support for non-BMP code points. | """Implement the builtin unichr() with support for non-BMP code points. | ||||
@@ -100,6 +100,11 @@ class Tag(Node): | |||||
result += "</" + unicode(self.tag) + " " * self.close_padding + ">" | result += "</" + unicode(self.tag) + " " * self.close_padding + ">" | ||||
return result | return result | ||||
def __strip__(self, normalize=True, collapse=True): | |||||
if self.type in self.TAGS_VISIBLE: | |||||
return self.contents.strip_code(normalize, collapse) | |||||
return None | |||||
def translate(self): | def translate(self): | ||||
translations { | translations { | ||||
self.TAG_ITALIC: ("''", "''"), | self.TAG_ITALIC: ("''", "''"), | ||||
@@ -31,6 +31,9 @@ class Text(Node): | |||||
def __unicode__(self): | def __unicode__(self): | ||||
return unicode(self.value) | return unicode(self.value) | ||||
def __strip__(self, normalize=True, collapse=True): | |||||
return self | |||||
@property | @property | ||||
def value(self): | def value(self): | ||||
return self._value | return self._value |
@@ -253,20 +253,10 @@ class Wikicode(StringMixIn): | |||||
def strip_code(self, normalize=True, collapse=True): | def strip_code(self, normalize=True, collapse=True): | ||||
nodes = [] | nodes = [] | ||||
for node in self.nodes: | for node in self.nodes: | ||||
if isinstance(node, Heading): | |||||
nodes.append(child.title) | |||||
elif isinstance(node, HTMLEntity): | |||||
if normalize: | |||||
nodes.append(node.normalize()) | |||||
else: | |||||
nodes.append(node) | |||||
elif isinstance(node, Tag): | |||||
if node.type in node.TAGS_VISIBLE: | |||||
nodes.append(node.contents.strip_code(normalize, collapse)) | |||||
elif isinstance(node, Text): | |||||
nodes.append(node) | |||||
stripped = node.__strip__(normalize) | |||||
if stripped: | |||||
nodes.append(unicode(stripped)) | |||||
nodes = map(unicode, nodes) | |||||
if collapse: | if collapse: | ||||
stripped = u"".join(nodes).strip("\n") | stripped = u"".join(nodes).strip("\n") | ||||
while "\n\n\n" in stripped: | while "\n\n\n" in stripped: | ||||