@@ -24,7 +24,7 @@ from __future__ import unicode_literals | |||||
from . import Node, Text | from . import Node, Text | ||||
from ..compat import str | 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 | from ..utils import parse_anything | ||||
__all__ = ["Tag"] | __all__ = ["Tag"] | ||||
@@ -32,7 +32,7 @@ __all__ = ["Tag"] | |||||
class Tag(Node): | class Tag(Node): | ||||
"""Represents an HTML-style tag in wikicode, like ``<ref>``.""" | """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="", | self_closing=False, invalid=False, implicit=False, padding="", | ||||
closing_tag=None): | closing_tag=None): | ||||
super(Tag, self).__init__() | super(Tag, self).__init__() | ||||
@@ -42,7 +42,7 @@ class Tag(Node): | |||||
else: | else: | ||||
self._contents = contents | self._contents = contents | ||||
self._attrs = attrs if attrs else [] | self._attrs = attrs if attrs else [] | ||||
self._showtag = showtag | |||||
self._wiki_markup = wiki_markup | |||||
self._self_closing = self_closing | self._self_closing = self_closing | ||||
self._invalid = invalid | self._invalid = invalid | ||||
self._implicit = implicit | self._implicit = implicit | ||||
@@ -53,8 +53,8 @@ class Tag(Node): | |||||
self._closing_tag = tag | self._closing_tag = tag | ||||
def __unicode__(self): | 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: | if self.self_closing: | ||||
return open_ | return open_ | ||||
else: | else: | ||||
@@ -72,7 +72,7 @@ class Tag(Node): | |||||
def __iternodes__(self, getter): | def __iternodes__(self, getter): | ||||
yield None, self | yield None, self | ||||
if self.showtag: | |||||
if not self.wiki_markup: | |||||
for child in getter(self.tag): | for child in getter(self.tag): | ||||
yield self.tag, child | yield self.tag, child | ||||
for attr in self.attributes: | for attr in self.attributes: | ||||
@@ -84,7 +84,7 @@ class Tag(Node): | |||||
if self.contents: | if self.contents: | ||||
for child in getter(self.contents): | for child in getter(self.contents): | ||||
yield self.contents, child | 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): | for child in getter(self.closing_tag): | ||||
yield self.closing_tag, child | yield self.closing_tag, child | ||||
@@ -131,9 +131,9 @@ class Tag(Node): | |||||
return self._attrs | return self._attrs | ||||
@property | @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 | @property | ||||
def self_closing(self): | def self_closing(self): | ||||
@@ -183,9 +183,9 @@ class Tag(Node): | |||||
def contents(self, value): | def contents(self, value): | ||||
self._contents = parse_anything(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 | @self_closing.setter | ||||
def self_closing(self, value): | def self_closing(self, value): | ||||
@@ -207,8 +207,7 @@ class Builder(object): | |||||
"""Handle a case where a tag is at the head of the tokens.""" | """Handle a case where a tag is at the head of the tokens.""" | ||||
close_tokens = (tokens.TagCloseSelfclose, tokens.TagCloseClose) | close_tokens = (tokens.TagCloseSelfclose, tokens.TagCloseClose) | ||||
implicit, attrs, contents, closing_tag = False, [], None, None | 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() | self._push() | ||||
while self._tokens: | while self._tokens: | ||||
token = self._tokens.pop() | token = self._tokens.pop() | ||||
@@ -225,12 +224,11 @@ class Builder(object): | |||||
if isinstance(token, tokens.TagCloseSelfclose): | if isinstance(token, tokens.TagCloseSelfclose): | ||||
tag = self._pop() | tag = self._pop() | ||||
self_closing = True | self_closing = True | ||||
padding = token.padding | |||||
implicit = token.get("implicit", False) | |||||
padding, implicit = token.padding, token.implicit | |||||
else: | else: | ||||
self_closing = False | self_closing = False | ||||
closing_tag = self._pop() | 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) | invalid, implicit, padding, closing_tag) | ||||
else: | else: | ||||
self._write(self._handle_token(token)) | self._write(self._handle_token(token)) | ||||
@@ -55,7 +55,7 @@ class Token(object): | |||||
return False | return False | ||||
def __getattr__(self, key): | def __getattr__(self, key): | ||||
return self._kwargs[key] | |||||
return self._kwargs.get(key, False) | |||||
def __setattr__(self, key, value): | def __setattr__(self, key, value): | ||||
self._kwargs[key] = value | self._kwargs[key] = value | ||||
@@ -63,10 +63,6 @@ class Token(object): | |||||
def __delattr__(self, key): | def __delattr__(self, key): | ||||
del self._kwargs[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): | def make(name): | ||||
"""Create a new Token class using ``type()`` and add it to ``__all__``.""" | """Create a new Token class using ``type()`` and add it to ``__all__``.""" | ||||
@@ -24,7 +24,7 @@ | |||||
from __future__ import unicode_literals | 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"] | "is_single_only"] | ||||
PARSER_BLACKLIST = [ | PARSER_BLACKLIST = [ | ||||
@@ -44,7 +44,7 @@ INVISIBLE_TAGS = [ | |||||
SINGLE_ONLY = ["br", "hr", "meta", "link", "img"] | SINGLE_ONLY = ["br", "hr", "meta", "link", "img"] | ||||
SINGLE = SINGLE_ONLY + ["li", "dt", "dd"] | SINGLE = SINGLE_ONLY + ["li", "dt", "dd"] | ||||
WIKICODE = { | |||||
WIKI_MARKUP = { | |||||
"i": {"open": "''", "close": "''"}, | "i": {"open": "''", "close": "''"}, | ||||
"b": {"open": "'''", "close": "'''"}, | "b": {"open": "'''", "close": "'''"}, | ||||
"ul": {"open": "*"}, | "ul": {"open": "*"}, | ||||
@@ -54,9 +54,9 @@ WIKICODE = { | |||||
"hr": {"open": "----"}, | "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")) | return (data.get("open"), data.get("close")) | ||||
def is_parsable(tag): | def is_parsable(tag): | ||||
@@ -106,7 +106,7 @@ class TreeEqualityTestCase(TestCase): | |||||
self.assertEqual(exp_attr.pad_first, act_attr.pad_first) | 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_before_eq, act_attr.pad_before_eq) | ||||
self.assertEqual(exp_attr.pad_after_eq, act_attr.pad_after_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.self_closing, actual.self_closing) | ||||
self.assertIs(expected.invalid, actual.invalid) | self.assertIs(expected.invalid, actual.invalid) | ||||
self.assertIs(expected.implicit, actual.implicit) | self.assertIs(expected.implicit, actual.implicit) | ||||
@@ -50,8 +50,8 @@ class TestTag(TreeEqualityTestCase): | |||||
implicit=True) | implicit=True) | ||||
node7 = Tag(wraptext("br"), self_closing=True, invalid=True, | node7 = Tag(wraptext("br"), self_closing=True, invalid=True, | ||||
padding=" ") | 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("<ref></ref>", str(node1)) | ||||
self.assertEqual('<span style="color: red;">foo</span>', str(node2)) | self.assertEqual('<span style="color: red;">foo</span>', str(node2)) | ||||
@@ -72,7 +72,7 @@ class TestTag(TreeEqualityTestCase): | |||||
# <ref>foobar</ref> | # <ref>foobar</ref> | ||||
node1 = Tag(wrap([node1n1]), wrap([node1n2])) | node1 = Tag(wrap([node1n1]), wrap([node1n2])) | ||||
# '''bold text''' | # '''bold text''' | ||||
node2 = Tag(wraptext("i"), wrap([node2n1]), showtag=False) | |||||
node2 = Tag(wraptext("i"), wrap([node2n1]), wiki_markup=True) | |||||
# <img id="foo" class="bar" /> | # <img id="foo" class="bar" /> | ||||
node3 = Tag(wrap([node3n1]), | node3 = Tag(wrap([node3n1]), | ||||
attrs=[Attribute(wrap([node3n2]), wrap([node3n3])), | attrs=[Attribute(wrap([node3n2]), wrap([node3n3])), | ||||
@@ -156,15 +156,15 @@ class TestTag(TreeEqualityTestCase): | |||||
self.assertEqual([], node1.attributes) | self.assertEqual([], node1.attributes) | ||||
self.assertIs(attrs, node2.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")) | 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) | 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) | self.assertEqual("<i>italic text</i>", node) | ||||
def test_self_closing(self): | def test_self_closing(self): | ||||
@@ -44,8 +44,8 @@ class TestTokens(unittest.TestCase): | |||||
self.assertEqual("bar", token2.foo) | self.assertEqual("bar", token2.foo) | ||||
self.assertEqual(123, token2.baz) | 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" | token1.spam = "eggs" | ||||
token2.foo = "ham" | token2.foo = "ham" | ||||
@@ -53,7 +53,7 @@ class TestTokens(unittest.TestCase): | |||||
self.assertEqual("eggs", token1.spam) | self.assertEqual("eggs", token1.spam) | ||||
self.assertEqual("ham", token2.foo) | self.assertEqual("ham", token2.foo) | ||||
self.assertRaises(KeyError, lambda: token2.baz) | |||||
self.assertFalse(token2.baz) | |||||
self.assertRaises(KeyError, delattr, token2, "baz") | self.assertRaises(KeyError, delattr, token2, "baz") | ||||
def test_repr(self): | def test_repr(self): | ||||