Browse Source

Implement __strip__ API for Wikicode.

tags/v0.1
Ben Kurtovic 11 years ago
parent
commit
253c812fb3
6 changed files with 21 additions and 14 deletions
  1. +2
    -1
      mwparserfromhell/nodes/__init__.py
  2. +3
    -0
      mwparserfromhell/nodes/heading.py
  3. +5
    -0
      mwparserfromhell/nodes/html_entity.py
  4. +5
    -0
      mwparserfromhell/nodes/tag.py
  5. +3
    -0
      mwparserfromhell/nodes/text.py
  6. +3
    -13
      mwparserfromhell/wikicode.py

+ 2
- 1
mwparserfromhell/nodes/__init__.py View File

@@ -25,7 +25,8 @@ from mwparserfromhell.string_mixin import StringMixIn
__all__ = ["Node"]

class Node(StringMixIn):
pass
def __strip__(self, normalize=True, collapse=True):
return None

from mwparserfromhell.nodes import extras
from mwparserfromhell.nodes.text import Text


+ 3
- 0
mwparserfromhell/nodes/heading.py View File

@@ -32,6 +32,9 @@ class Heading(Node):
def __unicode__(self):
return ("=" * self.level) + self.title + ("=" * self.level)

def __strip__(self, normalize=True, collapse=True):
return self.title

@property
def title(self):
return self._title


+ 5
- 0
mwparserfromhell/nodes/html_entity.py View File

@@ -53,6 +53,11 @@ class HTMLEntity(Node):
return u"&#x{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):
"""Implement the builtin unichr() with support for non-BMP code points.



+ 5
- 0
mwparserfromhell/nodes/tag.py View File

@@ -100,6 +100,11 @@ class Tag(Node):
result += "</" + unicode(self.tag) + " " * self.close_padding + ">"
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):
translations {
self.TAG_ITALIC: ("''", "''"),


+ 3
- 0
mwparserfromhell/nodes/text.py View File

@@ -31,6 +31,9 @@ class Text(Node):
def __unicode__(self):
return unicode(self.value)

def __strip__(self, normalize=True, collapse=True):
return self

@property
def value(self):
return self._value

+ 3
- 13
mwparserfromhell/wikicode.py View File

@@ -253,20 +253,10 @@ class Wikicode(StringMixIn):
def strip_code(self, normalize=True, collapse=True):
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:
stripped = u"".join(nodes).strip("\n")
while "\n\n\n" in stripped:


Loading…
Cancel
Save