@@ -26,15 +26,19 @@ Usage | |||||
Normal usage is rather straightforward (where ``text`` is page text):: | Normal usage is rather straightforward (where ``text`` is page text):: | ||||
>>> import mwparserfromhell | >>> import mwparserfromhell | ||||
>>> parser = mwparserfromhell.Parser() | |||||
>>> templates = parser.parse(text) | |||||
>>> wikicode = mwparserfromhell.parse(text) | |||||
``wikicode`` is a ``mwparserfromhell.Wikicode`` object, which acts like an | |||||
ordinary unicode object. It also contains a list of nodes representing the | |||||
components of the wikicode, including ordinary text nodes, templates, and | |||||
links. For example:: | |||||
>>> wikicode = mwparserfromhell.parse(u"{{foo|bar|baz|eggs=spam}}") | |||||
>>> print wikicode | |||||
u"{{foo|bar|baz|eggs=spam}}" | |||||
>>> | |||||
``templates`` is a list of ``mwparserfromhell.Template`` objects, which contain | |||||
a ``name`` attribute, a ``params`` attribute, and a ``render()`` method. Slices | |||||
are supported to get parameters. For example:: | |||||
>>> templates = parser.parse("{{foo|bar|baz|eggs=spam}}") | |||||
>>> print templates | |||||
[Template(name="foo", params={"1": "bar", "2": "baz", "eggs": "spam"})] | [Template(name="foo", params={"1": "bar", "2": "baz", "eggs": "spam"})] | ||||
>>> template = templates[0] | >>> template = templates[0] | ||||
>>> print template.name | >>> print template.name | ||||
@@ -32,12 +32,6 @@ __license__ = "MIT License" | |||||
__version__ = "0.1.dev" | __version__ = "0.1.dev" | ||||
__email__ = "ben.kurtovic@verizon.net" | __email__ = "ben.kurtovic@verizon.net" | ||||
from mwparserfromhell.node import Node | |||||
from mwparserfromhell.parameter import Parameter | |||||
from mwparserfromhell.parser import Parser | |||||
from mwparserfromhell.string_mixin import StringMixIn | |||||
from mwparserfromhell.template import Template | |||||
from mwparserfromhell.text import Text | |||||
from mwparserfromhell.wikicode import Wikicode | |||||
from mwparserfromhell import nodes, parser, string_mixin, wikicode | |||||
parse = Parser().parse | |||||
parse = lambda text: parser.Parser().parse(text) |
@@ -0,0 +1,32 @@ | |||||
# -*- coding: utf-8 -*- | |||||
# | |||||
# Copyright (C) 2012 Ben Kurtovic <ben.kurtovic@verizon.net> | |||||
# | |||||
# Permission is hereby granted, free of charge, to any person obtaining a copy | |||||
# of this software and associated documentation files (the "Software"), to deal | |||||
# in the Software without restriction, including without limitation the rights | |||||
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |||||
# copies of the Software, and to permit persons to whom the Software is | |||||
# furnished to do so, subject to the following conditions: | |||||
# | |||||
# The above copyright notice and this permission notice shall be included in | |||||
# all copies or substantial portions of the Software. | |||||
# | |||||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |||||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |||||
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |||||
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |||||
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |||||
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |||||
# SOFTWARE. | |||||
from mwparserfromhell.string_mixin import StringMixIn | |||||
__all__ = ["Node"] | |||||
class Node(StringMixIn): | |||||
pass | |||||
from mwparserfromhell.nodes import extras | |||||
from mwparserfromhell.nodes.template import Template | |||||
from mwparserfromhell.nodes.text import Text |
@@ -20,7 +20,4 @@ | |||||
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||||
# SOFTWARE. | # SOFTWARE. | ||||
__all__ = ["Node"] | |||||
class Node(object): | |||||
pass | |||||
from mwparserfromhell.nodes.extras.parameter import Parameter |
@@ -20,12 +20,11 @@ | |||||
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||||
# SOFTWARE. | # SOFTWARE. | ||||
from mwparserfromhell.node import Node | |||||
from mwparserfromhell.string_mixin import StringMixIn | |||||
from mwparserfromhell.nodes import Node | |||||
__all__ = ["Template"] | __all__ = ["Template"] | ||||
class Template(Node, StringMixIn): | |||||
class Template(Node): | |||||
def __init__(self, name, params=None): | def __init__(self, name, params=None): | ||||
self._name = name | self._name = name | ||||
if params: | if params: |
@@ -20,12 +20,11 @@ | |||||
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||||
# SOFTWARE. | # SOFTWARE. | ||||
from mwparserfromhell.node import Node | |||||
from mwparserfromhell.string_mixin import StringMixIn | |||||
from mwparserfromhell.nodes import Node | |||||
__all__ = ["Text"] | __all__ = ["Text"] | ||||
class Text(Node, StringMixIn): | |||||
class Text(Node): | |||||
def __init__(self, value): | def __init__(self, value): | ||||
self._value = value | self._value = value | ||||
@@ -0,0 +1,23 @@ | |||||
# -*- coding: utf-8 -*- | |||||
# | |||||
# Copyright (C) 2012 Ben Kurtovic <ben.kurtovic@verizon.net> | |||||
# | |||||
# Permission is hereby granted, free of charge, to any person obtaining a copy | |||||
# of this software and associated documentation files (the "Software"), to deal | |||||
# in the Software without restriction, including without limitation the rights | |||||
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |||||
# copies of the Software, and to permit persons to whom the Software is | |||||
# furnished to do so, subject to the following conditions: | |||||
# | |||||
# The above copyright notice and this permission notice shall be included in | |||||
# all copies or substantial portions of the Software. | |||||
# | |||||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |||||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |||||
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |||||
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |||||
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |||||
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |||||
# SOFTWARE. | |||||
from mwparserfromhell.parser.demo import DemoParser as Parser |
@@ -20,14 +20,13 @@ | |||||
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||||
# SOFTWARE. | # SOFTWARE. | ||||
from mwparserfromhell.parameter import Parameter | |||||
from mwparserfromhell.template import Template | |||||
from mwparserfromhell.text import Text | |||||
from mwparserfromhell.nodes import Template, Text | |||||
from mwparserfromhell.nodes.extras import Parameter | |||||
from mwparserfromhell.wikicode import Wikicode | from mwparserfromhell.wikicode import Wikicode | ||||
__all__ = ["Parser"] | |||||
__all__ = ["DemoParser"] | |||||
class Parser(object): | |||||
class DemoParser(object): | |||||
def _tokenize(self, text): | def _tokenize(self, text): | ||||
return text | return text | ||||
@@ -22,7 +22,7 @@ | |||||
__all__ = ["StringMixIn"] | __all__ = ["StringMixIn"] | ||||
class StringMixIn(object): | |||||
class StringMixIn(object): # UnicodeMixIn? | |||||
def __str__(self): | def __str__(self): | ||||
return unicode(self).encode("utf8") | return unicode(self).encode("utf8") | ||||
@@ -24,10 +24,8 @@ import htmlentitydefs | |||||
import re | import re | ||||
import mwparserfromhell | import mwparserfromhell | ||||
from mwparserfromhell.node import Node | |||||
from mwparserfromhell.nodes import Node, Template, Text | |||||
from mwparserfromhell.string_mixin import StringMixIn | from mwparserfromhell.string_mixin import StringMixIn | ||||
from mwparserfromhell.template import Template | |||||
from mwparserfromhell.text import Text | |||||
__all__ = ["Wikicode"] | __all__ = ["Wikicode"] | ||||
@@ -187,6 +185,6 @@ class Wikicode(StringMixIn): | |||||
# Magic with htmlentitydefs if normalize | # Magic with htmlentitydefs if normalize | ||||
return normalized(u" ".join(self.ifilter_text())) | return normalized(u" ".join(self.ifilter_text())) | ||||
def show_tree(self): | |||||
def get_tree(self): | |||||
marker = object() # Random object we can find with certainty in a list | marker = object() # Random object we can find with certainty in a list | ||||
print "\n".join(self._show_tree(self, [], marker)) | |||||
return "\n".join(self._show_tree(self, [], marker)) |