Bladeren bron

Implement padding support for Tags completely; open_padding->padding.

tags/v0.3
Ben Kurtovic 11 jaren geleden
bovenliggende
commit
eed7c918bf
3 gewijzigde bestanden met toevoegingen van 21 en 18 verwijderingen
  1. +9
    -9
      mwparserfromhell/nodes/tag.py
  2. +3
    -3
      mwparserfromhell/parser/builder.py
  3. +9
    -6
      mwparserfromhell/parser/tokenizer.py

+ 9
- 9
mwparserfromhell/nodes/tag.py Bestand weergeven

@@ -33,7 +33,7 @@ class Tag(TagDefinitions, Node):
"""Represents an HTML-style tag in wikicode, like ``<ref>``.""" """Represents an HTML-style tag in wikicode, like ``<ref>``."""


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="", closing_tag=None):
self_closing=False, padding="", closing_tag=None):
super(Tag, self).__init__() super(Tag, self).__init__()
self._type = type_ self._type = type_
self._tag = tag self._tag = tag
@@ -44,7 +44,7 @@ class Tag(TagDefinitions, Node):
self._attrs = [] self._attrs = []
self._showtag = showtag self._showtag = showtag
self._self_closing = self_closing self._self_closing = self_closing
self._open_padding = open_padding
self._padding = padding
if closing_tag: if closing_tag:
self._closing_tag = closing_tag self._closing_tag = closing_tag
else: else:
@@ -62,9 +62,9 @@ class Tag(TagDefinitions, Node):
if self.attributes: if self.attributes:
result += " " + " ".join([str(attr) for attr in self.attributes]) result += " " + " ".join([str(attr) for attr in self.attributes])
if self.self_closing: if self.self_closing:
result += self.open_padding + "/>"
result += self.padding + "/>"
else: else:
result += self.open_padding + ">" + str(self.contents)
result += self.padding + ">" + str(self.contents)
result += "</" + str(self.closing_tag) + ">" result += "</" + str(self.closing_tag) + ">"
return result return result


@@ -145,9 +145,9 @@ class Tag(TagDefinitions, Node):
return self._self_closing return self._self_closing


@property @property
def open_padding(self):
def padding(self):
"""Spacing to insert before the first closing ``>``.""" """Spacing to insert before the first closing ``>``."""
return self._open_padding
return self._padding


@property @property
def closing_tag(self): def closing_tag(self):
@@ -188,9 +188,9 @@ class Tag(TagDefinitions, Node):
def self_closing(self, value): def self_closing(self, value):
self._self_closing = bool(value) self._self_closing = bool(value)


@open_padding.setter
def open_padding(self, value):
self._open_padding = str(value)
@padding.setter
def padding(self, value):
self._padding = str(value)


@closing_tag.setter @closing_tag.setter
def closing_tag(self, value): def closing_tag(self, value):


+ 3
- 3
mwparserfromhell/parser/builder.py Bestand weergeven

@@ -210,19 +210,19 @@ class Builder(object):
if isinstance(token, tokens.TagAttrStart): if isinstance(token, tokens.TagAttrStart):
attrs.append(self._handle_attribute(token)) attrs.append(self._handle_attribute(token))
elif isinstance(token, tokens.TagCloseOpen): elif isinstance(token, tokens.TagCloseOpen):
open_pad = token.padding
padding = token.padding
tag = self._pop() tag = self._pop()
self._push() self._push()
elif isinstance(token, tokens.TagCloseSelfclose): elif isinstance(token, tokens.TagCloseSelfclose):
tag = self._pop() tag = self._pop()
return Tag(type_, tag, attrs=attrs, showtag=showtag, return Tag(type_, tag, attrs=attrs, showtag=showtag,
self_closing=True, open_padding=token.padding)
self_closing=True, padding=token.padding)
elif isinstance(token, tokens.TagOpenClose): elif isinstance(token, tokens.TagOpenClose):
contents = self._pop() contents = self._pop()
self._push() self._push()
elif isinstance(token, tokens.TagCloseClose): elif isinstance(token, tokens.TagCloseClose):
return Tag(type_, tag, contents, attrs, showtag, False, return Tag(type_, tag, contents, attrs, showtag, False,
open_pad, self._pop())
padding, self._pop())
else: else:
self._write(self._handle_token(token)) self._write(self._handle_token(token))




+ 9
- 6
mwparserfromhell/parser/tokenizer.py Bestand weergeven

@@ -458,9 +458,9 @@ class Tokenizer(object):
self._context ^= contexts.TAG_OPEN_NAME self._context ^= contexts.TAG_OPEN_NAME
self._context |= contexts.TAG_BODY self._context |= contexts.TAG_BODY


## If the last element was TagAttrStart, remove it, add " " to its padding, then return that
padding = ""
return padding
if isinstance(self._stack[-1], tokens.TagAttrStart):
return self._stack.pop().padding
return ""


def _actually_handle_chunk(self, chunks, is_new): def _actually_handle_chunk(self, chunks, is_new):
if is_new and not self._context & contexts.TAG_OPEN_ATTR_QUOTED: if is_new and not self._context & contexts.TAG_OPEN_ATTR_QUOTED:
@@ -538,7 +538,8 @@ class Tokenizer(object):
self._head += 1 self._head += 1
reset = self._head reset = self._head
try: try:
attr = self._parse(contexts.TAG_OPEN_ATTR_QUOTED | contexts.TAG_OPEN_ATTR_IGNORE)
attr = self._parse(contexts.TAG_OPEN_ATTR_QUOTED |
contexts.TAG_OPEN_ATTR_IGNORE)
except BadRoute: except BadRoute:
self._head = reset self._head = reset
self._write_text(next) self._write_text(next)
@@ -654,7 +655,8 @@ class Tokenizer(object):
elif this == "<" and next != "/" and ( elif this == "<" and next != "/" and (
not self._context & (contexts.TAG ^ contexts.TAG_BODY)): not self._context & (contexts.TAG ^ contexts.TAG_BODY)):
self._parse_tag() self._parse_tag()
elif self._context & (contexts.TAG_OPEN ^ contexts.TAG_OPEN_ATTR_QUOTED):
elif self._context & (
contexts.TAG_OPEN ^ contexts.TAG_OPEN_ATTR_QUOTED):
if this == "\n": if this == "\n":
if self._context & contexts.TAG_CLOSE: if self._context & contexts.TAG_CLOSE:
self._pop() self._pop()
@@ -663,7 +665,8 @@ class Tokenizer(object):
self._handle_tag_close_open() self._handle_tag_close_open()
elif this == "/" and next == ">": elif this == "/" and next == ">":
return self._handle_tag_selfclose() return self._handle_tag_selfclose()
elif this == "=" and self._context & contexts.TAG_OPEN_ATTR_NAME:
elif this == "=" and (
self._context & contexts.TAG_OPEN_ATTR_NAME):
self._handle_tag_attribute_body() self._handle_tag_attribute_body()
elif this == "<" and next == "/" and ( elif this == "<" and next == "/" and (
self._context & contexts.TAG_BODY): self._context & contexts.TAG_BODY):


Laden…
Annuleren
Opslaan