Explorar el Código

Some node fixes; attributes with empty values now display correctly.

tags/v0.3
Ben Kurtovic hace 11 años
padre
commit
53c26589ee
Se han modificado 5 ficheros con 14 adiciones y 12 borrados
  1. +0
    -2
      mwparserfromhell/compat.py
  2. +2
    -2
      mwparserfromhell/nodes/extras/attribute.py
  3. +6
    -6
      mwparserfromhell/nodes/template.py
  4. +2
    -2
      mwparserfromhell/parser/tokens.py
  5. +4
    -0
      tests/test_attribute.py

+ 0
- 2
mwparserfromhell/compat.py Ver fichero

@@ -15,14 +15,12 @@ py3k = sys.version_info[0] == 3
if py3k: if py3k:
bytes = bytes bytes = bytes
str = str str = str
basestring = str
maxsize = sys.maxsize maxsize = sys.maxsize
import html.entities as htmlentities import html.entities as htmlentities


else: else:
bytes = str bytes = str
str = unicode str = unicode
basestring = basestring
maxsize = sys.maxint maxsize = sys.maxint
import htmlentitydefs as htmlentities import htmlentitydefs as htmlentities




+ 2
- 2
mwparserfromhell/nodes/extras/attribute.py Ver fichero

@@ -48,7 +48,7 @@ class Attribute(StringMixIn):


def __unicode__(self): def __unicode__(self):
base = self.pad_first + str(self.name) + self.pad_before_eq base = self.pad_first + str(self.name) + self.pad_before_eq
if self.value:
if self.value is not None:
if self.quoted: if self.quoted:
return base + '="' + self.pad_after_eq + str(self.value) + '"' return base + '="' + self.pad_after_eq + str(self.value) + '"'
return base + "=" + self.pad_after_eq + str(self.value) return base + "=" + self.pad_after_eq + str(self.value)
@@ -100,7 +100,7 @@ class Attribute(StringMixIn):


@value.setter @value.setter
def value(self, newval): def value(self, newval):
self._value = parse_anything(newval)
self._value = None if newval is None else parse_anything(newval)


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


+ 6
- 6
mwparserfromhell/nodes/template.py Ver fichero

@@ -26,7 +26,7 @@ import re


from . import HTMLEntity, Node, Text from . import HTMLEntity, Node, Text
from .extras import Parameter from .extras import Parameter
from ..compat import basestring, str
from ..compat import str
from ..utils import parse_anything from ..utils import parse_anything


__all__ = ["Template"] __all__ = ["Template"]
@@ -84,7 +84,7 @@ class Template(Node):
replacement = str(HTMLEntity(value=ord(char))) replacement = str(HTMLEntity(value=ord(char)))
for node in code.filter_text(recursive=False): for node in code.filter_text(recursive=False):
if char in node: if char in node:
code.replace(node, node.replace(char, replacement))
code.replace(node, node.replace(char, replacement), False)


def _blank_param_value(self, value): def _blank_param_value(self, value):
"""Remove the content from *value* while keeping its whitespace. """Remove the content from *value* while keeping its whitespace.
@@ -170,9 +170,9 @@ class Template(Node):
With *ignore_empty*, ``False`` will be returned even if the template With *ignore_empty*, ``False`` will be returned even if the template
contains a parameter with the name *name*, if the parameter's value contains a parameter with the name *name*, if the parameter's value
is empty. Note that a template may have multiple parameters with the is empty. Note that a template may have multiple parameters with the
same name.
same name, but only the last one is read by the MediaWiki parser.
""" """
name = name.strip() if isinstance(name, basestring) else str(name)
name = str(name).strip()
for param in self.params: for param in self.params:
if param.name.strip() == name: if param.name.strip() == name:
if ignore_empty and not param.value.strip(): if ignore_empty and not param.value.strip():
@@ -191,7 +191,7 @@ class Template(Node):
parameters can have the same name, we'll return the last match, since parameters can have the same name, we'll return the last match, since
the last parameter is the only one read by the MediaWiki parser. the last parameter is the only one read by the MediaWiki parser.
""" """
name = name.strip() if isinstance(name, basestring) else str(name)
name = str(name).strip()
for param in reversed(self.params): for param in reversed(self.params):
if param.name.strip() == name: if param.name.strip() == name:
return param return param
@@ -294,7 +294,7 @@ class Template(Node):
the first instance if none have dependents, otherwise the one with the first instance if none have dependents, otherwise the one with
dependents will be kept). dependents will be kept).
""" """
name = name.strip() if isinstance(name, basestring) else str(name)
name = str(name).strip()
removed = False removed = False
to_remove = [] to_remove = []
for i, param in enumerate(self.params): for i, param in enumerate(self.params):


+ 2
- 2
mwparserfromhell/parser/tokens.py Ver fichero

@@ -30,7 +30,7 @@ into the :py:class`~.Wikicode` tree by the :py:class:`~.Builder`.


from __future__ import unicode_literals from __future__ import unicode_literals


from ..compat import basestring, py3k
from ..compat import py3k, str


__all__ = ["Token"] __all__ = ["Token"]


@@ -43,7 +43,7 @@ class Token(object):
def __repr__(self): def __repr__(self):
args = [] args = []
for key, value in self._kwargs.items(): for key, value in self._kwargs.items():
if isinstance(value, basestring) and len(value) > 100:
if isinstance(value, str) and len(value) > 100:
args.append(key + "=" + repr(value[:97] + "...")) args.append(key + "=" + repr(value[:97] + "..."))
else: else:
args.append(key + "=" + repr(value)) args.append(key + "=" + repr(value))


+ 4
- 0
tests/test_attribute.py Ver fichero

@@ -40,6 +40,8 @@ class TestAttribute(TreeEqualityTestCase):
self.assertEqual(' foo="bar"', str(node2)) self.assertEqual(' foo="bar"', str(node2))
node3 = Attribute(wraptext("a"), wraptext("b"), False, "", " ", " ") node3 = Attribute(wraptext("a"), wraptext("b"), False, "", " ", " ")
self.assertEqual("a = b", str(node3)) self.assertEqual("a = b", str(node3))
node4 = Attribute(wraptext("a"), wrap([]), False, " ", "", " ")
self.assertEqual(" a= ", str(node4))


def test_name(self): def test_name(self):
"""test getter/setter for the name attribute""" """test getter/setter for the name attribute"""
@@ -56,6 +58,8 @@ class TestAttribute(TreeEqualityTestCase):
self.assertIs(value, node.value) self.assertIs(value, node.value)
node.value = "{{bar}}" node.value = "{{bar}}"
self.assertWikicodeEqual(wrap([Template(wraptext("bar"))]), node.value) self.assertWikicodeEqual(wrap([Template(wraptext("bar"))]), node.value)
node.value = None
self.assertIs(None, node.value)


def test_quoted(self): def test_quoted(self):
"""test getter/setter for the quoted attribute""" """test getter/setter for the quoted attribute"""


Cargando…
Cancelar
Guardar