Browse Source

Make mwparserfromhell.parser() be an alias for parse_anything().

Some other changes, including removal of the 'string' import in the tokenizer.
tags/v0.2
Ben Kurtovic 12 years ago
parent
commit
acb7e57904
4 changed files with 12 additions and 17 deletions
  1. +2
    -3
      mwparserfromhell/__init__.py
  2. +1
    -4
      mwparserfromhell/parser/__init__.py
  3. +2
    -3
      mwparserfromhell/parser/tokenizer.py
  4. +7
    -7
      mwparserfromhell/utils.py

+ 2
- 3
mwparserfromhell/__init__.py View File

@@ -34,7 +34,6 @@ __license__ = "MIT License"
__version__ = "0.2.dev"
__email__ = "ben.kurtovic@verizon.net"

from . import nodes, parser, smart_list, string_mixin, wikicode
from . import compat, nodes, parser, smart_list, string_mixin, utils, wikicode

parse = lambda text: parser.Parser(text).parse()
parse.__doc__ = "Short for :py:meth:`.Parser.parse`."
parse = utils.parse_anything

+ 1
- 4
mwparserfromhell/parser/__init__.py View File

@@ -26,10 +26,7 @@ modules: the :py:mod:`~.tokenizer` and the :py:mod:`~.builder`. This module
joins them together under one interface.
"""

try:
from ._builder import CBuilder as Builder
except ImportError:
from .builder import Builder
from .builder import Builder
try:
from ._tokenizer import CTokenizer as Tokenizer
except ImportError:


+ 2
- 3
mwparserfromhell/parser/tokenizer.py View File

@@ -23,7 +23,6 @@
from __future__ import unicode_literals
from math import log
import re
import string

from . import contexts
from . import tokens
@@ -377,9 +376,9 @@ class Tokenizer(object):
else:
numeric = hexadecimal = False

valid = string.hexdigits if hexadecimal else string.digits
valid = "0123456789abcdefABCDEF" if hexadecimal else "0123456789"
if not numeric and not hexadecimal:
valid += string.ascii_letters
valid += "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
if not all([char in valid for char in this]):
self._fail_route()



+ 7
- 7
mwparserfromhell/utils.py View File

@@ -34,16 +34,16 @@ from .smart_list import SmartList
def parse_anything(value):
"""Return a :py:class:`~.Wikicode` for *value*, allowing multiple types.

This differs from :py:func:`mwparserfromhell.parse` in that we accept more
than just a string to be parsed. Unicode objects (strings in py3k), strings
(bytes in py3k), integers (converted to strings), ``None``, existing
This differs from :py:meth:`.Parser.parse` in that we accept more than just
a string to be parsed. Unicode objects (strings in py3k), strings (bytes in
py3k), integers (converted to strings), ``None``, existing
:py:class:`~.Node` or :py:class:`~.Wikicode` objects, as well as an
iterable of these types, are supported. This is used to parse input
on-the-fly by various methods of :py:class:`~.Wikicode` and others like
:py:class:`~.Template`, such as :py:meth:`wikicode.insert()
<.Wikicode.insert>` or setting :py:meth:`template.name <.Template.name>`.
"""
from . import parse
from .parser import Parser
from .wikicode import Wikicode

if isinstance(value, Wikicode):
@@ -51,11 +51,11 @@ def parse_anything(value):
elif isinstance(value, Node):
return Wikicode(SmartList([value]))
elif isinstance(value, str):
return parse(value)
return Parser(value).parse()
elif isinstance(value, bytes):
return parse(value.decode("utf8"))
return Parser(value.decode("utf8")).parse()
elif isinstance(value, int):
return parse(str(value))
return Parser(str(value)).parse()
elif value is None:
return Wikicode(SmartList())
try:


Loading…
Cancel
Save