Procházet zdrojové kódy

Allow passing skip_style_tags to parse() (fixes #73)

tags/v0.4
Ben Kurtovic před 10 roky
rodič
revize
d342831af8
2 změnil soubory, kde provedl 9 přidání a 6 odebrání
  1. +1
    -1
      mwparserfromhell/parser/__init__.py
  2. +8
    -5
      mwparserfromhell/utils.py

+ 1
- 1
mwparserfromhell/parser/__init__.py Zobrazit soubor

@@ -57,7 +57,7 @@ class Parser(object):
"""Parse *text*, returning a :py:class:`~.Wikicode` object tree. """Parse *text*, returning a :py:class:`~.Wikicode` object tree.


If *skip_style_tags* is ``True``, then ``''`` and ``'''`` will not be If *skip_style_tags* is ``True``, then ``''`` and ``'''`` will not be
parsed, but instead be treated as plain text.
parsed, but instead will be treated as plain text.
""" """
tokens = self._tokenizer.tokenize(text, context, skip_style_tags) tokens = self._tokenizer.tokenize(text, context, skip_style_tags)
code = self._builder.build(tokens) code = self._builder.build(tokens)


+ 8
- 5
mwparserfromhell/utils.py Zobrazit soubor

@@ -33,7 +33,7 @@ from .smart_list import SmartList


__all__ = ["parse_anything"] __all__ = ["parse_anything"]


def parse_anything(value, context=0):
def parse_anything(value, context=0, skip_style_tags=False):
"""Return a :py:class:`~.Wikicode` for *value*, allowing multiple types. """Return a :py:class:`~.Wikicode` for *value*, allowing multiple types.


This differs from :py:meth:`.Parser.parse` in that we accept more than just This differs from :py:meth:`.Parser.parse` in that we accept more than just
@@ -50,6 +50,9 @@ def parse_anything(value, context=0):
For example, :py:class:`~.ExternalLink`\ 's :py:attr:`~.ExternalLink.url` For example, :py:class:`~.ExternalLink`\ 's :py:attr:`~.ExternalLink.url`
setter sets *context* to :py:mod:`contexts.EXT_LINK_URI <.contexts>` to setter sets *context* to :py:mod:`contexts.EXT_LINK_URI <.contexts>` to
prevent the URL itself from becoming an :py:class:`~.ExternalLink`. prevent the URL itself from becoming an :py:class:`~.ExternalLink`.

If *skip_style_tags* is ``True``, then ``''`` and ``'''`` will not be
parsed, but instead will be treated as plain text.
""" """
from .parser import Parser from .parser import Parser
from .wikicode import Wikicode from .wikicode import Wikicode
@@ -59,17 +62,17 @@ def parse_anything(value, context=0):
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 Parser().parse(value, context)
return Parser().parse(value, context, skip_style_tags)
elif isinstance(value, bytes): elif isinstance(value, bytes):
return Parser().parse(value.decode("utf8"), context)
return Parser().parse(value.decode("utf8"), context, skip_style_tags)
elif isinstance(value, int): elif isinstance(value, int):
return Parser().parse(str(value), context)
return Parser().parse(str(value), context, skip_style_tags)
elif value is None: elif value is None:
return Wikicode(SmartList()) return Wikicode(SmartList())
try: try:
nodelist = SmartList() nodelist = SmartList()
for item in value: for item in value:
nodelist += parse_anything(item, context).nodes
nodelist += parse_anything(item, context, skip_style_tags).nodes
except TypeError: except TypeError:
error = "Needs string, Node, Wikicode, int, None, or iterable of these, but got {0}: {1}" error = "Needs string, Node, Wikicode, int, None, or iterable of these, but got {0}: {1}"
raise ValueError(error.format(type(value).__name__, value)) raise ValueError(error.format(type(value).__name__, value))


Načítá se…
Zrušit
Uložit