Bladeren bron

Implement ExternalLinks as tokens and in the builder.

tags/v0.3
Ben Kurtovic 11 jaren geleden
bovenliggende
commit
8fe8b1fef5
3 gewijzigde bestanden met toevoegingen van 25 en 2 verwijderingen
  1. +1
    -0
      mwparserfromhell/nodes/__init__.py
  2. +20
    -2
      mwparserfromhell/parser/builder.py
  3. +4
    -0
      mwparserfromhell/parser/tokens.py

+ 1
- 0
mwparserfromhell/nodes/__init__.py Bestand weergeven

@@ -69,6 +69,7 @@ from . import extras
from .text import Text
from .argument import Argument
from .comment import Comment
from .external_link import ExternalLink
from .heading import Heading
from .html_entity import HTMLEntity
from .tag import Tag


+ 20
- 2
mwparserfromhell/parser/builder.py Bestand weergeven

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

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

def _handle_external_link(self, token):
"""Handle when an external link is at the head of the tokens."""
brackets, url = token.brackets, None
self._push()
while self._tokens:
token = self._tokens.pop()
if isinstance(token, tokens.ExternalLinkSeparator):
url = self._pop()
self._push()
elif isinstance(token, tokens.ExternalLinkClose):
if url is not None:
return ExternalLink(url, self._pop(), brackets)
return ExternalLink(self._pop(), brackets=brackets)
else:
self._write(self._handle_token(token))

def _handle_token(self, token):
"""Handle a single token."""
if isinstance(token, tokens.Text):
@@ -252,6 +268,8 @@ class Builder(object):
return self._handle_comment()
elif isinstance(token, tokens.TagOpenOpen):
return self._handle_tag(token)
elif isinstance(token, tokens.ExternalLinkOpen):
return self._handle_external_link(token)

def build(self, tokenlist):
"""Build a Wikicode object from a list tokens and return it."""


+ 4
- 0
mwparserfromhell/parser/tokens.py Bestand weergeven

@@ -104,4 +104,8 @@ TagCloseSelfclose = make("TagCloseSelfclose") # />
TagOpenClose = make("TagOpenClose") # </
TagCloseClose = make("TagCloseClose") # >

ExternalLinkOpen = make("ExternalLinkOpen") # [
ExternalLinkSeparator = make("ExternalLinkSeparator") #
ExternalLinkClose = make("ExternalLinkClose") # ]

del make

Laden…
Annuleren
Opslaan