diff --git a/mwparserfromhell/nodes/tag.py b/mwparserfromhell/nodes/tag.py index 08d5204..25c0708 100644 --- a/mwparserfromhell/nodes/tag.py +++ b/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 ````.""" - 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): diff --git a/mwparserfromhell/parser/builder.py b/mwparserfromhell/parser/builder.py index 9366742..ef55776 100644 --- a/mwparserfromhell/parser/builder.py +++ b/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)) diff --git a/mwparserfromhell/parser/tokens.py b/mwparserfromhell/parser/tokens.py index f3d89fc..6dd3446 100644 --- a/mwparserfromhell/parser/tokens.py +++ b/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__``.""" diff --git a/mwparserfromhell/tag_defs.py b/mwparserfromhell/tag_defs.py index 73493d3..c918b4d 100644 --- a/mwparserfromhell/tag_defs.py +++ b/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): diff --git a/tests/_test_tree_equality.py b/tests/_test_tree_equality.py index cfda97b..3267b45 100644 --- a/tests/_test_tree_equality.py +++ b/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) diff --git a/tests/test_tag.py b/tests/test_tag.py index 6755270..a0fbcf1 100644 --- a/tests/test_tag.py +++ b/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("", str(node1)) self.assertEqual('foo', str(node2)) @@ -72,7 +72,7 @@ class TestTag(TreeEqualityTestCase): # foobar 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) # 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("italic text", node) def test_self_closing(self): diff --git a/tests/test_tokens.py b/tests/test_tokens.py index 4620982..2048bb9 100644 --- a/tests/test_tokens.py +++ b/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):