|
@@ -20,6 +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. |
|
|
|
|
|
|
|
|
|
|
|
""" |
|
|
|
|
|
This package contains the actual wikicode parser, split up into two main |
|
|
|
|
|
modules: the :py:mod:`~mwparserfromhell.parser.tokenizer` and the |
|
|
|
|
|
:py:mod:`~mwparserfromhell.parser.builder`. This module joins them together |
|
|
|
|
|
under one interface. |
|
|
|
|
|
""" |
|
|
|
|
|
|
|
|
try: |
|
|
try: |
|
|
from ._builder import CBuilder as Builder |
|
|
from ._builder import CBuilder as Builder |
|
|
from ._tokenizer import CTokenizer as Tokenizer |
|
|
from ._tokenizer import CTokenizer as Tokenizer |
|
@@ -30,12 +37,24 @@ except ImportError: |
|
|
__all__ = ["Parser"] |
|
|
__all__ = ["Parser"] |
|
|
|
|
|
|
|
|
class Parser(object): |
|
|
class Parser(object): |
|
|
|
|
|
"""Represents a parser for wikicode. |
|
|
|
|
|
|
|
|
|
|
|
Actual parsing is a two-step process: first, the text is split up into a |
|
|
|
|
|
series of tokens by the |
|
|
|
|
|
:py:class:`~mwparserfromhell.parser.tokenizer.Tokenizer`, and then the |
|
|
|
|
|
tokens are converted into trees of |
|
|
|
|
|
:py:class`~mwparserfromhell.wikicode.Wikicode` objects and |
|
|
|
|
|
:py:class:`~mwparserfromhell.nodes.Node`\ nodes by the |
|
|
|
|
|
:py:class:`~mwparserfromhell.parser.builder.Builder`. |
|
|
|
|
|
""" |
|
|
|
|
|
|
|
|
def __init__(self, text): |
|
|
def __init__(self, text): |
|
|
self.text = text |
|
|
self.text = text |
|
|
self._tokenizer = Tokenizer() |
|
|
self._tokenizer = Tokenizer() |
|
|
self._builder = Builder() |
|
|
self._builder = Builder() |
|
|
|
|
|
|
|
|
def parse(self): |
|
|
def parse(self): |
|
|
|
|
|
"""Return a string as a parsed ``Wikicode`` object tree.""" |
|
|
tokens = self._tokenizer.tokenize(self.text) |
|
|
tokens = self._tokenizer.tokenize(self.text) |
|
|
code = self._builder.build(tokens) |
|
|
code = self._builder.build(tokens) |
|
|
return code |
|
|
return code |