瀏覽代碼

Tag.showtag -> Tag.wiki_markup

tags/v0.3
Ben Kurtovic 11 年之前
父節點
當前提交
de6d5074fa
共有 7 個檔案被更改,包括 36 行新增42 行删除
  1. +13
    -13
      mwparserfromhell/nodes/tag.py
  2. +3
    -5
      mwparserfromhell/parser/builder.py
  3. +1
    -5
      mwparserfromhell/parser/tokens.py
  4. +5
    -5
      mwparserfromhell/tag_defs.py
  5. +1
    -1
      tests/_test_tree_equality.py
  6. +10
    -10
      tests/test_tag.py
  7. +3
    -3
      tests/test_tokens.py

+ 13
- 13
mwparserfromhell/nodes/tag.py 查看文件

@@ -24,7 +24,7 @@ from __future__ import unicode_literals

from . import Node, Text
from ..compat import str
from ..tag_defs import get_wikicode, is_visible
from ..tag_defs import get_wiki_markup, is_visible
from ..utils import parse_anything

__all__ = ["Tag"]
@@ -32,7 +32,7 @@ __all__ = ["Tag"]
class Tag(Node):
"""Represents an HTML-style tag in wikicode, like ``<ref>``."""

def __init__(self, tag, contents=None, attrs=None, showtag=True,
def __init__(self, tag, contents=None, attrs=None, wiki_markup=False,
self_closing=False, invalid=False, implicit=False, padding="",
closing_tag=None):
super(Tag, self).__init__()
@@ -42,7 +42,7 @@ class Tag(Node):
else:
self._contents = contents
self._attrs = attrs if attrs else []
self._showtag = showtag
self._wiki_markup = wiki_markup
self._self_closing = self_closing
self._invalid = invalid
self._implicit = implicit
@@ -53,8 +53,8 @@ class Tag(Node):
self._closing_tag = tag

def __unicode__(self):
if not self.showtag:
open_, close = get_wikicode(self.tag)
if self.wiki_markup:
open_, close = get_wiki_markup(self.tag)
if self.self_closing:
return open_
else:
@@ -72,7 +72,7 @@ class Tag(Node):

def __iternodes__(self, getter):
yield None, self
if self.showtag:
if not self.wiki_markup:
for child in getter(self.tag):
yield self.tag, child
for attr in self.attributes:
@@ -84,7 +84,7 @@ class Tag(Node):
if self.contents:
for child in getter(self.contents):
yield self.contents, child
if not self.self_closing and self.showtag and self.closing_tag:
if not self.self_closing and not self.wiki_markup and self.closing_tag:
for child in getter(self.closing_tag):
yield self.closing_tag, child

@@ -131,9 +131,9 @@ class Tag(Node):
return self._attrs

@property
def showtag(self):
"""Whether to show the tag itself instead of a wikicode version."""
return self._showtag
def wiki_markup(self):
"""Whether to show the wiki version of a tag instead of the HTML."""
return self._wiki_markup

@property
def self_closing(self):
@@ -183,9 +183,9 @@ class Tag(Node):
def contents(self, value):
self._contents = parse_anything(value)

@showtag.setter
def showtag(self, value):
self._showtag = bool(value)
@wiki_markup.setter
def wiki_markup(self, value):
self._wiki_markup = bool(value)

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


+ 3
- 5
mwparserfromhell/parser/builder.py 查看文件

@@ -207,8 +207,7 @@ class Builder(object):
"""Handle a case where a tag is at the head of the tokens."""
close_tokens = (tokens.TagCloseSelfclose, tokens.TagCloseClose)
implicit, attrs, contents, closing_tag = False, [], None, None
showtag = token.get("showtag", True)
invalid = token.get("invalid", False)
wiki_markup, invalid = token.wiki_markup, token.invalid
self._push()
while self._tokens:
token = self._tokens.pop()
@@ -225,12 +224,11 @@ class Builder(object):
if isinstance(token, tokens.TagCloseSelfclose):
tag = self._pop()
self_closing = True
padding = token.padding
implicit = token.get("implicit", False)
padding, implicit = token.padding, token.implicit
else:
self_closing = False
closing_tag = self._pop()
return Tag(tag, contents, attrs, showtag, self_closing,
return Tag(tag, contents, attrs, wiki_markup, self_closing,
invalid, implicit, padding, closing_tag)
else:
self._write(self._handle_token(token))


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

@@ -55,7 +55,7 @@ class Token(object):
return False

def __getattr__(self, key):
return self._kwargs[key]
return self._kwargs.get(key, False)

def __setattr__(self, key, value):
self._kwargs[key] = value
@@ -63,10 +63,6 @@ class Token(object):
def __delattr__(self, key):
del self._kwargs[key]

def get(self, key, default=None):
"""Same as :py:meth:`__getattr__`, but has a *default* if missing."""
return self._kwargs.get(key, default)


def make(name):
"""Create a new Token class using ``type()`` and add it to ``__all__``."""


+ 5
- 5
mwparserfromhell/tag_defs.py 查看文件

@@ -24,7 +24,7 @@

from __future__ import unicode_literals

__all__ = ["get_wikicode", "is_parsable", "is_visible", "is_single",
__all__ = ["get_wiki_markup", "is_parsable", "is_visible", "is_single",
"is_single_only"]

PARSER_BLACKLIST = [
@@ -44,7 +44,7 @@ INVISIBLE_TAGS = [
SINGLE_ONLY = ["br", "hr", "meta", "link", "img"]
SINGLE = SINGLE_ONLY + ["li", "dt", "dd"]

WIKICODE = {
WIKI_MARKUP = {
"i": {"open": "''", "close": "''"},
"b": {"open": "'''", "close": "'''"},
"ul": {"open": "*"},
@@ -54,9 +54,9 @@ WIKICODE = {
"hr": {"open": "----"},
}

def get_wikicode(tag):
"""Return the appropriate wikicode before and after the given *tag*."""
data = WIKICODE[tag.lower()]
def get_wiki_markup(tag):
"""Return the appropriate wiki markup before and after the given *tag*."""
data = WIKI_MARKUP[tag.lower()]
return (data.get("open"), data.get("close"))

def is_parsable(tag):


+ 1
- 1
tests/_test_tree_equality.py 查看文件

@@ -106,7 +106,7 @@ class TreeEqualityTestCase(TestCase):
self.assertEqual(exp_attr.pad_first, act_attr.pad_first)
self.assertEqual(exp_attr.pad_before_eq, act_attr.pad_before_eq)
self.assertEqual(exp_attr.pad_after_eq, act_attr.pad_after_eq)
self.assertIs(expected.showtag, actual.showtag)
self.assertIs(expected.wiki_markup, actual.wiki_markup)
self.assertIs(expected.self_closing, actual.self_closing)
self.assertIs(expected.invalid, actual.invalid)
self.assertIs(expected.implicit, actual.implicit)


+ 10
- 10
tests/test_tag.py 查看文件

@@ -50,8 +50,8 @@ class TestTag(TreeEqualityTestCase):
implicit=True)
node7 = Tag(wraptext("br"), self_closing=True, invalid=True,
padding=" ")
node8 = Tag(wraptext("hr"), showtag=False, self_closing=True)
node9 = Tag(wraptext("i"), wraptext("italics!"), showtag=False)
node8 = Tag(wraptext("hr"), wiki_markup=True, self_closing=True)
node9 = Tag(wraptext("i"), wraptext("italics!"), wiki_markup=True)

self.assertEqual("<ref></ref>", str(node1))
self.assertEqual('<span style="color: red;">foo</span>', str(node2))
@@ -72,7 +72,7 @@ class TestTag(TreeEqualityTestCase):
# <ref>foobar</ref>
node1 = Tag(wrap([node1n1]), wrap([node1n2]))
# '''bold text'''
node2 = Tag(wraptext("i"), wrap([node2n1]), showtag=False)
node2 = Tag(wraptext("i"), wrap([node2n1]), wiki_markup=True)
# <img id="foo" class="bar" />
node3 = Tag(wrap([node3n1]),
attrs=[Attribute(wrap([node3n2]), wrap([node3n3])),
@@ -156,15 +156,15 @@ class TestTag(TreeEqualityTestCase):
self.assertEqual([], node1.attributes)
self.assertIs(attrs, node2.attributes)

def test_showtag(self):
"""test getter/setter for the showtag attribute"""
def test_wiki_markup(self):
"""test getter/setter for the wiki_markup attribute"""
node = Tag(wraptext("i"), wraptext("italic text"))
self.assertTrue(node.showtag)
node.showtag = False
self.assertFalse(node.showtag)
self.assertFalse(node.wiki_markup)
node.wiki_markup = True
self.assertTrue(node.wiki_markup)
self.assertEqual("''italic text''", node)
node.showtag = 1
self.assertTrue(node.showtag)
node.wiki_markup = 0
self.assertFalse(node.wiki_markup)
self.assertEqual("<i>italic text</i>", node)

def test_self_closing(self):


+ 3
- 3
tests/test_tokens.py 查看文件

@@ -44,8 +44,8 @@ class TestTokens(unittest.TestCase):

self.assertEqual("bar", token2.foo)
self.assertEqual(123, token2.baz)
self.assertRaises(KeyError, lambda: token1.foo)
self.assertRaises(KeyError, lambda: token2.bar)
self.assertFalse(token1.foo)
self.assertFalse(token2.bar)

token1.spam = "eggs"
token2.foo = "ham"
@@ -53,7 +53,7 @@ class TestTokens(unittest.TestCase):

self.assertEqual("eggs", token1.spam)
self.assertEqual("ham", token2.foo)
self.assertRaises(KeyError, lambda: token2.baz)
self.assertFalse(token2.baz)
self.assertRaises(KeyError, delattr, token2, "baz")

def test_repr(self):


Loading…
取消
儲存