瀏覽代碼

Wikilinks in builder; some additions; cleanup.

tags/v0.1.1
Ben Kurtovic 12 年之前
父節點
當前提交
aa29a8bd28
共有 5 個文件被更改,包括 91 次插入34 次删除
  1. +16
    -0
      docs/api/mwparserfromhell.nodes.rst
  2. +1
    -0
      mwparserfromhell/nodes/__init__.py
  3. +20
    -1
      mwparserfromhell/parser/builder.py
  4. +37
    -32
      mwparserfromhell/parser/tokens.py
  5. +17
    -1
      mwparserfromhell/wikicode.py

+ 16
- 0
docs/api/mwparserfromhell.nodes.rst 查看文件

@@ -17,6 +17,14 @@ nodes Package
:undoc-members: :undoc-members:
:show-inheritance: :show-inheritance:


:mod:`comment` Module
---------------------

.. automodule:: mwparserfromhell.nodes.comment
:members:
:undoc-members:
:show-inheritance:

:mod:`heading` Module :mod:`heading` Module
--------------------- ---------------------


@@ -56,6 +64,14 @@ nodes Package
:undoc-members: :undoc-members:
:show-inheritance: :show-inheritance:


:mod:`wikilink` Module
----------------------

.. automodule:: mwparserfromhell.nodes.wikilink
:members:
:undoc-members:
:show-inheritance:

Subpackages Subpackages
----------- -----------




+ 1
- 0
mwparserfromhell/nodes/__init__.py 查看文件

@@ -73,3 +73,4 @@ from .heading import Heading
from .html_entity import HTMLEntity from .html_entity import HTMLEntity
from .tag import Tag from .tag import Tag
from .template import Template from .template import Template
from .wikilink import Wikilink

+ 20
- 1
mwparserfromhell/parser/builder.py 查看文件

@@ -24,7 +24,8 @@ from __future__ import unicode_literals


from . import tokens from . import tokens
from ..compat import str from ..compat import str
from ..nodes import Argument, Comment, Heading, HTMLEntity, Tag, Template, Text
from ..nodes import (Argument, Comment, Heading, HTMLEntity, Tag, Template,
Text, Wikilink)
from ..nodes.extras import Attribute, Parameter from ..nodes.extras import Attribute, Parameter
from ..smart_list import SmartList from ..smart_list import SmartList
from ..wikicode import Wikicode from ..wikicode import Wikicode
@@ -125,6 +126,22 @@ class Builder(object):
else: else:
self._write(self._handle_token(token)) self._write(self._handle_token(token))


def _handle_wikilink(self):
"""Handle a case where a wikilink is at the head of the tokens."""
title = None
self._push()
while self._tokens:
token = self._tokens.pop()
if isinstance(token, tokens.WikilinkSeparator):
title = self._pop()
self._push()
elif isinstance(token, tokens.WikilinkClose):
if title is not None:
return Wikilink(title, self._pop())
return Wikilink(self._pop())
else:
self._write(self._handle_token(token))

def _handle_entity(self): def _handle_entity(self):
"""Handle a case where an HTML entity is at the head of the tokens.""" """Handle a case where an HTML entity is at the head of the tokens."""
token = self._tokens.pop() token = self._tokens.pop()
@@ -216,6 +233,8 @@ class Builder(object):
return self._handle_template() return self._handle_template()
elif isinstance(token, tokens.ArgumentOpen): elif isinstance(token, tokens.ArgumentOpen):
return self._handle_argument() return self._handle_argument()
elif isinstance(token, tokens.WikilinkOpen):
return self._handle_wikilink()
elif isinstance(token, tokens.HTMLEntityStart): elif isinstance(token, tokens.HTMLEntityStart):
return self._handle_entity() return self._handle_entity()
elif isinstance(token, tokens.HeadingStart): elif isinstance(token, tokens.HeadingStart):


+ 37
- 32
mwparserfromhell/parser/tokens.py 查看文件

@@ -65,38 +65,43 @@ class Token(object):


def make(name): def make(name):
"""Create a new Token class using ``type()`` and add it to ``__all__``.""" """Create a new Token class using ``type()`` and add it to ``__all__``."""
token = type(name if py3k else name.encode("utf8"), (Token,), {})
globals()[name] = token
__all__.append(name) __all__.append(name)
return type(name if py3k else name.encode("utf8"), (Token,), {})

Text = make("Text")

TemplateOpen = make("TemplateOpen") # {{
TemplateParamSeparator = make("TemplateParamSeparator") # |
TemplateParamEquals = make("TemplateParamEquals") # =
TemplateClose = make("TemplateClose") # }}

ArgumentOpen = make("ArgumentOpen") # {{{
ArgumentSeparator = make("ArgumentSeparator") # |
ArgumentClose = make("ArgumentClose") # }}}

HTMLEntityStart = make("HTMLEntityStart") # &
HTMLEntityNumeric = make("HTMLEntityNumeric") # #
HTMLEntityHex = make("HTMLEntityHex") # x
HTMLEntityEnd = make("HTMLEntityEnd") # ;

HeadingStart = make("HeadingStart") # =...
HeadingEnd = make("HeadingEnd") # =...

CommentStart = make("CommentStart") # <!--
CommentEnd = make("CommentEnd") # -->

TagOpenOpen = make("TagOpenOpen") # <
TagAttrStart = make("TagAttrStart")
TagAttrEquals = make("TagAttrEquals") # =
TagAttrQuote = make("TagAttrQuote") # "
TagCloseOpen = make("TagCloseOpen") # >
TagCloseSelfclose = make("TagCloseSelfclose") # />
TagOpenClose = make("TagOpenClose") # </
TagCloseClose = make("TagCloseClose") # >

make("Text")

make("TemplateOpen") # {{
make("TemplateParamSeparator") # |
make("TemplateParamEquals") # =
make("TemplateClose") # }}

make("ArgumentOpen") # {{{
make("ArgumentSeparator") # |
make("ArgumentClose") # }}}

make("WikilinkOpen") # [[
make("WikilinkSeparator") # |
make("WikilinkClose") # ]]

make("HTMLEntityStart") # &
make("HTMLEntityNumeric") # #
make("HTMLEntityHex") # x
make("HTMLEntityEnd") # ;

make("HeadingStart") # =...
make("HeadingEnd") # =...

make("CommentStart") # <!--
make("CommentEnd") # -->

make("TagOpenOpen") # <
make("TagAttrStart")
make("TagAttrEquals") # =
make("TagAttrQuote") # "
make("TagCloseOpen") # >
make("TagCloseSelfclose") # />
make("TagOpenClose") # </
make("TagCloseClose") # >


del make del make

+ 17
- 1
mwparserfromhell/wikicode.py 查看文件

@@ -24,7 +24,7 @@ from __future__ import unicode_literals
import re import re


from .compat import maxsize, str from .compat import maxsize, str
from .nodes import Heading, Node, Tag, Template, Text
from .nodes import Heading, Node, Tag, Template, Text, Wikilink
from .string_mixin import StringMixIn from .string_mixin import StringMixIn
from .utils import parse_anything from .utils import parse_anything


@@ -303,6 +303,14 @@ class Wikicode(StringMixIn):
if not matches or re.search(matches, str(node), flags): if not matches or re.search(matches, str(node), flags):
yield node yield node


def ifilter_links(self, recursive=False, matches=None, flags=FLAGS):
"""Iterate over wikilink nodes.

This is equivalent to :py:meth:`ifilter` with *forcetype* set to
:py:class:`~.Wikilink`.
"""
return self.ifilter(recursive, matches, flags, forcetype=Wikilink)

def ifilter_templates(self, recursive=False, matches=None, flags=FLAGS): def ifilter_templates(self, recursive=False, matches=None, flags=FLAGS):
"""Iterate over template nodes. """Iterate over template nodes.


@@ -335,6 +343,14 @@ class Wikicode(StringMixIn):
""" """
return list(self.ifilter(recursive, matches, flags, forcetype)) return list(self.ifilter(recursive, matches, flags, forcetype))


def filter_links(self, recursive=False, matches=None, flags=FLAGS):
"""Return a list of wikilink nodes.

This is equivalent to calling :py:func:`list` on
:py:meth:`ifilter_links`.
"""
return list(self.ifilter_links(recursive, matches, flags))

def filter_templates(self, recursive=False, matches=None, flags=FLAGS): def filter_templates(self, recursive=False, matches=None, flags=FLAGS):
"""Return a list of template nodes. """Return a list of template nodes.




Loading…
取消
儲存