ソースを参照

Should correctly handle closing tags with strange spacing.

tags/v0.3
Ben Kurtovic 12年前
コミット
827c544721
3個のファイルの変更22行の追加16行の削除
  1. +18
    -11
      mwparserfromhell/nodes/tag.py
  2. +1
    -2
      mwparserfromhell/parser/builder.py
  3. +3
    -3
      mwparserfromhell/parser/tokenizer.py

+ 18
- 11
mwparserfromhell/nodes/tag.py ファイルの表示

@@ -111,7 +111,7 @@ class Tag(Node):
}

def __init__(self, type_, tag, contents=None, attrs=None, showtag=True,
self_closing=False, open_padding="", close_padding=""):
self_closing=False, open_padding="", closing_tag=None):
super(Tag, self).__init__()
self._type = type_
self._tag = tag
@@ -123,7 +123,10 @@ class Tag(Node):
self._showtag = showtag
self._self_closing = self_closing
self._open_padding = open_padding
self._close_padding = close_padding
if closing_tag:
self._closing_tag = closing_tag
else:
self._closing_tag = tag

def __unicode__(self):
if not self.showtag:
@@ -140,7 +143,7 @@ class Tag(Node):
result += self.open_padding + "/>"
else:
result += self.open_padding + ">" + str(self.contents)
result += "</" + str(self.tag) + self.close_padding + ">"
result += "</" + self.closing_tag + ">"
return result

def __iternodes__(self, getter):
@@ -242,9 +245,13 @@ class Tag(Node):
return self._open_padding

@property
def close_padding(self):
"""Spacing to insert before the last closing > (excl. self-closing)."""
return self._close_padding
def closing_tag(self):
"""The closing tag, as a :py:class:`~.Wikicode` object.

This will usually equal :py:attr:`tag`, unless there is additional
spacing, comments, or the like.
"""
return self._closing_tag

@type.setter
def type(self, value):
@@ -254,11 +261,11 @@ class Tag(Node):
self._type = value
for key in self.TRANSLATIONS:
if self.TRANSLATIONS[key] == value:
self._tag = parse_anything(key)
self._tag = self._closing_tag = parse_anything(key)

@tag.setter
def tag(self, value):
self._tag = parse_anything(value)
self._tag = self._closing_tag = parse_anything(value)
try:
self._type = self.TRANSLATIONS[text]
except KeyError:
@@ -280,6 +287,6 @@ class Tag(Node):
def open_padding(self, value):
self._open_padding = str(value)

@close_padding.setter
def close_padding(self, value):
self._close_padding = str(value)
@closing_tag.setter
def closing_tag(self, value):
self._closing_tag = parse_anything(value)

+ 1
- 2
mwparserfromhell/parser/builder.py ファイルの表示

@@ -221,9 +221,8 @@ class Builder(object):
contents = self._pop()
self._push()
elif isinstance(token, tokens.TagCloseClose):
self._pop()
return Tag(type_, tag, contents, attrs, showtag, False,
open_pad, token.padding)
open_pad, self._pop())
else:
self._write(self._handle_token(token))



+ 3
- 3
mwparserfromhell/parser/tokenizer.py ファイルの表示

@@ -496,8 +496,7 @@ class Tokenizer(object):
# Closing and opening tags are not the same, so fail this route:
self._fail_route()
self._write_all(closing)
padding = "" # TODO
self._write(tokens.TagCloseClose(padding=padding))
self._write(tokens.TagCloseClose())
return self._pop()

def _parse(self, context=0):
@@ -589,7 +588,8 @@ class Tokenizer(object):
self._context & contexts.TAG_OPEN and not
self._context & contexts.TAG_ATTR_BODY_QUOTED):
return self._handle_tag_selfclose()
elif this == "<" and next == "/" and self._context & contexts.TAG_BODY:
elif this == "<" and next == "/" and (
self._context & contexts.TAG_BODY):
self._handle_tag_open_close()
elif this == ">" and self._context & contexts.TAG_CLOSE:
return self._handle_tag_close_close()


読み込み中…
キャンセル
保存