Ver código fonte

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

tags/v0.3
Ben Kurtovic 12 anos atrás
pai
commit
eed7c918bf
3 arquivos alterados com 21 adições e 18 exclusões
  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 Ver arquivo

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

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

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

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

@property
def closing_tag(self):
@@ -188,9 +188,9 @@ class Tag(TagDefinitions, Node):
def self_closing(self, 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
def closing_tag(self, value):


+ 3
- 3
mwparserfromhell/parser/builder.py Ver arquivo

@@ -210,19 +210,19 @@ class Builder(object):
if isinstance(token, tokens.TagAttrStart):
attrs.append(self._handle_attribute(token))
elif isinstance(token, tokens.TagCloseOpen):
open_pad = token.padding
padding = token.padding
tag = self._pop()
self._push()
elif isinstance(token, tokens.TagCloseSelfclose):
tag = self._pop()
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):
contents = self._pop()
self._push()
elif isinstance(token, tokens.TagCloseClose):
return Tag(type_, tag, contents, attrs, showtag, False,
open_pad, self._pop())
padding, self._pop())
else:
self._write(self._handle_token(token))



+ 9
- 6
mwparserfromhell/parser/tokenizer.py Ver arquivo

@@ -458,9 +458,9 @@ class Tokenizer(object):
self._context ^= contexts.TAG_OPEN_NAME
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):
if is_new and not self._context & contexts.TAG_OPEN_ATTR_QUOTED:
@@ -538,7 +538,8 @@ class Tokenizer(object):
self._head += 1
reset = self._head
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:
self._head = reset
self._write_text(next)
@@ -654,7 +655,8 @@ class Tokenizer(object):
elif this == "<" and next != "/" and (
not self._context & (contexts.TAG ^ contexts.TAG_BODY)):
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 self._context & contexts.TAG_CLOSE:
self._pop()
@@ -663,7 +665,8 @@ class Tokenizer(object):
self._handle_tag_close_open()
elif this == "/" and next == ">":
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()
elif this == "<" and next == "/" and (
self._context & contexts.TAG_BODY):


Carregando…
Cancelar
Salvar