Browse Source

Should correctly handle closing tags with strange spacing.

tags/v0.3
Ben Kurtovic 12 years ago
parent
commit
827c544721
3 changed files with 22 additions and 16 deletions
  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 View File

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


def __init__(self, type_, tag, contents=None, attrs=None, showtag=True, 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__() super(Tag, self).__init__()
self._type = type_ self._type = type_
self._tag = tag self._tag = tag
@@ -123,7 +123,10 @@ class Tag(Node):
self._showtag = showtag self._showtag = showtag
self._self_closing = self_closing self._self_closing = self_closing
self._open_padding = open_padding 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): def __unicode__(self):
if not self.showtag: if not self.showtag:
@@ -140,7 +143,7 @@ class Tag(Node):
result += self.open_padding + "/>" result += self.open_padding + "/>"
else: else:
result += self.open_padding + ">" + str(self.contents) result += self.open_padding + ">" + str(self.contents)
result += "</" + str(self.tag) + self.close_padding + ">"
result += "</" + self.closing_tag + ">"
return result return result


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


@property @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 @type.setter
def type(self, value): def type(self, value):
@@ -254,11 +261,11 @@ class Tag(Node):
self._type = value self._type = value
for key in self.TRANSLATIONS: for key in self.TRANSLATIONS:
if self.TRANSLATIONS[key] == value: if self.TRANSLATIONS[key] == value:
self._tag = parse_anything(key)
self._tag = self._closing_tag = parse_anything(key)


@tag.setter @tag.setter
def tag(self, value): def tag(self, value):
self._tag = parse_anything(value)
self._tag = self._closing_tag = parse_anything(value)
try: try:
self._type = self.TRANSLATIONS[text] self._type = self.TRANSLATIONS[text]
except KeyError: except KeyError:
@@ -280,6 +287,6 @@ class Tag(Node):
def open_padding(self, value): def open_padding(self, value):
self._open_padding = str(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 View File

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




+ 3
- 3
mwparserfromhell/parser/tokenizer.py View File

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


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


Loading…
Cancel
Save