Przeglądaj źródła

Implement interface for __showtree__.

Some cleanup and import fixes.
tags/v0.1
Ben Kurtovic 12 lat temu
rodzic
commit
9355f22d22
5 zmienionych plików z 53 dodań i 48 usunięć
  1. +4
    -0
      mwparserfromhell/nodes/__init__.py
  2. +5
    -0
      mwparserfromhell/nodes/heading.py
  3. +28
    -5
      mwparserfromhell/nodes/tag.py
  4. +12
    -0
      mwparserfromhell/nodes/template.py
  5. +4
    -43
      mwparserfromhell/wikicode.py

+ 4
- 0
mwparserfromhell/nodes/__init__.py Wyświetl plik

@@ -31,6 +31,10 @@ class Node(StringMixIn):
def __strip__(self, normalize, collapse):
return None

def __showtree__(self, write, get, mark):
write(unicode(self))


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


+ 5
- 0
mwparserfromhell/nodes/heading.py Wyświetl plik

@@ -40,6 +40,11 @@ class Heading(Node):
def __strip__(self, normalize, collapse):
return self.title

def __showtree__(self, write, get, mark):
write("=" * self.level)
get(self.title)
write("=" * self.level)

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


+ 28
- 5
mwparserfromhell/nodes/tag.py Wyświetl plik

@@ -20,8 +20,7 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

from mwparserfromhell.nodes import Node
from mwparserfromhell.nodes.extras import Attribute
from mwparserfromhell.nodes import Node, Text

__all__ = ["Tag"]

@@ -86,7 +85,7 @@ class Tag(Node):
if not self.showtag:
open_, close = self._translate()
if self.self_closing:
return _open
return open_
else:
return open_ + unicode(self.contents) + close

@@ -104,7 +103,7 @@ class Tag(Node):
yield None, self
if self.showtag:
for child in getter(self.tag):
yield self.tag, tag
yield self.tag, child
for attr in self.attrs:
for child in getter(attr.name):
yield attr.name, child
@@ -119,7 +118,31 @@ class Tag(Node):
return self.contents.strip_code(normalize, collapse)
return None

def translate(self):
def __showtree__(self, write, get, mark):
tagnodes = self.tag.nodes
if (not self.attrs and len(tagnodes) == 1 and
isinstance(tagnodes[0], Text)):
write("<" + unicode(tagnodes[0]) + ">")
else:
write("<")
get(self.tag)
for attr in self.attrs:
get(attr.name)
if not attr.value:
continue
write(" = ")
mark()
get(attr.value)
write(">")
get(self.contents)
if len(tagnodes) == 1 and isinstance(tagnodes[0], Text):
write("</" + unicode(tagnodes[0]) + ">")
else:
write("</")
get(self.tag)
write(">")

def _translate(self):
translations = {
self.TAG_ITALIC: ("''", "''"),
self.TAG_BOLD: ("'''", "'''"),


+ 12
- 0
mwparserfromhell/nodes/template.py Wyświetl plik

@@ -57,6 +57,18 @@ class Template(Node):
for child in getter(param.value):
yield param.value, child

def __showtree__(self, write, get, mark):
write("{{")
get(self.name)
for param in self.params:
write(" | ")
mark()
get(param.name)
write(" = ")
mark()
get(param.value)
write("}}")

def _surface_escape(self, code, char):
replacement = HTMLEntity(value=ord(char))
for node in code.filter_text(recursive=False):


+ 4
- 43
mwparserfromhell/wikicode.py Wyświetl plik

@@ -22,9 +22,7 @@

import re

from mwparserfromhell.nodes import (
Heading, HTMLEntity, Node, Tag, Template, Text
)
from mwparserfromhell.nodes import Node, Template, Text
from mwparserfromhell.string_mixin import StringMixIn
from mwparserfromhell.utils import parse_anything

@@ -95,47 +93,10 @@ class Wikicode(StringMixIn):
else:
lines.append(" " * 6 * indent + " ".join(args))

get = lambda code: self._get_tree(code, lines, marker, indent + 1)
mark = lambda: lines.append(marker)
for node in code.nodes:
if isinstance(node, Heading):
write("=" * node.level)
self._get_tree(node.title, lines, marker, indent + 1)
write("=" * node.level)
elif isinstance(node, Tag):
tagnodes = node.tag.nodes
if (not node.attrs and len(tagnodes) == 1 and
isinstance(tagnodes[0], Text)):
write("<" + unicode(tagnodes[0]) + ">")
else:
write("<")
self._get_tree(node.tag, lines, marker, indent + 1)
for attr in node.attrs:
self._get_tree(attr.name, lines, marker, indent + 1)
if not attr.value:
continue
write(" = ")
lines.append(marker) # Continue from this line
self._get_tree(attr.value, lines, marker, indent + 1)
write(">")
self._get_tree(node.contents, lines, marker, indent + 1)
if len(tagnodes) == 1 and isinstance(tagnodes[0], Text):
write("</" + unicode(tagnodes[0]) + ">")
else:
write("</")
self._get_tree(node.tag, lines, marker, indent + 1)
write(">")
elif isinstance(node, Template):
write("{{")
self._get_tree(node.name, lines, marker, indent + 1)
for param in node.params:
write(" | ")
lines.append(marker) # Continue from this line
self._get_tree(param.name, lines, marker, indent + 1)
write(" = ")
lines.append(marker) # Continue from this line
self._get_tree(param.value, lines, marker, indent + 1)
write("}}")
else:
write(unicode(node))
node.__showtree__(write, get, mark)
return lines

@property


Ładowanie…
Anuluj
Zapisz