浏览代码

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 11 年前
父节点
当前提交
acb7e57904
共有 4 个文件被更改,包括 12 次插入17 次删除
  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 查看文件

@@ -34,7 +34,6 @@ __license__ = "MIT License"
__version__ = "0.2.dev" __version__ = "0.2.dev"
__email__ = "ben.kurtovic@verizon.net" __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 查看文件

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


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


+ 2
- 3
mwparserfromhell/parser/tokenizer.py 查看文件

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


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


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




+ 7
- 7
mwparserfromhell/utils.py 查看文件

@@ -34,16 +34,16 @@ from .smart_list import SmartList
def parse_anything(value): def parse_anything(value):
"""Return a :py:class:`~.Wikicode` for *value*, allowing multiple types. """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 :py:class:`~.Node` or :py:class:`~.Wikicode` objects, as well as an
iterable of these types, are supported. This is used to parse input 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 on-the-fly by various methods of :py:class:`~.Wikicode` and others like
:py:class:`~.Template`, such as :py:meth:`wikicode.insert() :py:class:`~.Template`, such as :py:meth:`wikicode.insert()
<.Wikicode.insert>` or setting :py:meth:`template.name <.Template.name>`. <.Wikicode.insert>` or setting :py:meth:`template.name <.Template.name>`.
""" """
from . import parse
from .parser import Parser
from .wikicode import Wikicode from .wikicode import Wikicode


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


正在加载...
取消
保存