瀏覽代碼

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

tags/v0.3
Ben Kurtovic 11 年之前
父節點
當前提交
53c26589ee
共有 5 個檔案被更改,包括 14 行新增12 行删除
  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 查看文件

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

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



+ 2
- 2
mwparserfromhell/nodes/extras/attribute.py 查看文件

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

def __unicode__(self):
base = self.pad_first + str(self.name) + self.pad_before_eq
if self.value:
if self.value is not None:
if self.quoted:
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
def value(self, newval):
self._value = parse_anything(newval)
self._value = None if newval is None else parse_anything(newval)

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


+ 6
- 6
mwparserfromhell/nodes/template.py 查看文件

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

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

__all__ = ["Template"]
@@ -84,7 +84,7 @@ class Template(Node):
replacement = str(HTMLEntity(value=ord(char)))
for node in code.filter_text(recursive=False):
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):
"""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
contains a parameter with the name *name*, if the parameter's value
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:
if param.name.strip() == name:
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
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):
if param.name.strip() == name:
return param
@@ -294,7 +294,7 @@ class Template(Node):
the first instance if none have dependents, otherwise the one with
dependents will be kept).
"""
name = name.strip() if isinstance(name, basestring) else str(name)
name = str(name).strip()
removed = False
to_remove = []
for i, param in enumerate(self.params):


+ 2
- 2
mwparserfromhell/parser/tokens.py 查看文件

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

from __future__ import unicode_literals

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

__all__ = ["Token"]

@@ -43,7 +43,7 @@ class Token(object):
def __repr__(self):
args = []
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] + "..."))
else:
args.append(key + "=" + repr(value))


+ 4
- 0
tests/test_attribute.py 查看文件

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

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

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


Loading…
取消
儲存