Bläddra i källkod

wiki_markup stores the markup itself instead of a bool

tags/v0.3
Ben Kurtovic 11 år sedan
förälder
incheckning
4f52887e17
5 ändrade filer med 24 tillägg och 35 borttagningar
  1. +11
    -7
      mwparserfromhell/nodes/tag.py
  2. +4
    -3
      mwparserfromhell/parser/builder.py
  3. +1
    -1
      mwparserfromhell/parser/tokens.py
  4. +1
    -17
      mwparserfromhell/tag_defs.py
  5. +7
    -7
      tests/test_tag.py

+ 11
- 7
mwparserfromhell/nodes/tag.py Visa fil

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

from . import Node, Text
from ..compat import str
from ..tag_defs import get_wiki_markup, is_visible
from ..tag_defs import 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, wiki_markup=False,
def __init__(self, tag, contents=None, attrs=None, wiki_markup=None,
self_closing=False, invalid=False, implicit=False, padding="",
closing_tag=None):
super(Tag, self).__init__()
@@ -54,11 +54,10 @@ class Tag(Node):

def __unicode__(self):
if self.wiki_markup:
open_, close = get_wiki_markup(self.tag)
if self.self_closing:
return open_
return self.wiki_markup
else:
return open_ + str(self.contents) + close
return self.wiki_markup + str(self.contents) + self.wiki_markup

result = ("</" if self.invalid else "<") + str(self.tag)
if self.attributes:
@@ -132,7 +131,12 @@ class Tag(Node):

@property
def wiki_markup(self):
"""Whether to show the wiki version of a tag instead of the HTML."""
"""The wikified version of a tag to show instead of HTML.

If set to a value, this will be displayed instead of the brackets.
For example, set to ``''`` to replace ``<i>`` or ``----`` to replace
``<hr>``.
"""
return self._wiki_markup

@property
@@ -185,7 +189,7 @@ class Tag(Node):

@wiki_markup.setter
def wiki_markup(self, value):
self._wiki_markup = bool(value)
self._wiki_markup = str(value) if value else None

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


+ 4
- 3
mwparserfromhell/parser/builder.py Visa fil

@@ -207,14 +207,14 @@ 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
wiki_markup, invalid = token.wiki_markup, token.invalid
wiki_markup, invalid = token.wiki_markup, token.invalid or False
self._push()
while self._tokens:
token = self._tokens.pop()
if isinstance(token, tokens.TagAttrStart):
attrs.append(self._handle_attribute(token))
elif isinstance(token, tokens.TagCloseOpen):
padding = token.padding
padding = token.padding or ""
tag = self._pop()
self._push()
elif isinstance(token, tokens.TagOpenClose):
@@ -224,7 +224,8 @@ class Builder(object):
if isinstance(token, tokens.TagCloseSelfclose):
tag = self._pop()
self_closing = True
padding, implicit = token.padding, token.implicit
padding = token.padding or ""
implicit = token.implicit or False
else:
self_closing = False
closing_tag = self._pop()


+ 1
- 1
mwparserfromhell/parser/tokens.py Visa fil

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

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

def __setattr__(self, key, value):
self._kwargs[key] = value


+ 1
- 17
mwparserfromhell/tag_defs.py Visa fil

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

from __future__ import unicode_literals

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

PARSER_BLACKLIST = [
# enwiki extensions @ 2013-06-28
@@ -44,21 +43,6 @@ INVISIBLE_TAGS = [
SINGLE_ONLY = ["br", "hr", "meta", "link", "img"]
SINGLE = SINGLE_ONLY + ["li", "dt", "dd"]

WIKI_MARKUP = {
"i": {"open": "''", "close": "''"},
"b": {"open": "'''", "close": "'''"},
"ul": {"open": "*"},
"ol": {"open": "#"},
"dt": {"open": ";"},
"dd": {"open": ":"},
"hr": {"open": "----"},
}

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):
"""Return if the given *tag*'s contents should be passed to the parser."""
return tag.lower() not in PARSER_BLACKLIST


+ 7
- 7
tests/test_tag.py Visa fil

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

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]), wiki_markup=True)
node2 = Tag(wraptext("b"), wrap([node2n1]), wiki_markup="'''")
# <img id="foo" class="bar" />
node3 = Tag(wrap([node3n1]),
attrs=[Attribute(wrap([node3n2]), wrap([node3n3])),
@@ -159,11 +159,11 @@ class TestTag(TreeEqualityTestCase):
def test_wiki_markup(self):
"""test getter/setter for the wiki_markup attribute"""
node = Tag(wraptext("i"), wraptext("italic text"))
self.assertFalse(node.wiki_markup)
node.wiki_markup = True
self.assertTrue(node.wiki_markup)
self.assertIs(None, node.wiki_markup)
node.wiki_markup = "''"
self.assertEqual("''", node.wiki_markup)
self.assertEqual("''italic text''", node)
node.wiki_markup = 0
node.wiki_markup = False
self.assertFalse(node.wiki_markup)
self.assertEqual("<i>italic text</i>", node)



Laddar…
Avbryt
Spara