Sfoglia il codice sorgente

Refactor out _really_parse_style()

tags/v0.3
Ben Kurtovic 11 anni fa
parent
commit
120f53deec
1 ha cambiato i file con 15 aggiunte e 21 eliminazioni
  1. +15
    -21
      mwparserfromhell/parser/tokenizer.py

+ 15
- 21
mwparserfromhell/parser/tokenizer.py Vedi File

@@ -644,37 +644,27 @@ class Tokenizer(object):
self._emit_text(tag) self._emit_text(tag)
self._emit(tokens.TagCloseClose()) self._emit(tokens.TagCloseClose())


def _really_parse_style(self, context, reset, markup, tag):
"""Parse wiki-style bold or italics. Raises :py:exc:`BadRoute`."""
if context & contexts.STYLE_ITALICS:
try:
stack = self._parse(context)
except BadRoute as route:
if not route.context & contexts.STYLE_PASS_AGAIN:
raise
self._head = reset
stack = self._parse(context | contexts.STYLE_SECOND_PASS)
else:
stack = self._parse(context)

self._emit_tag_open(tag, markup)
self._emit_all(stack)
self._emit_tag_close(tag)

def _parse_italics(self): def _parse_italics(self):
"""Parse wiki-style italics.""" """Parse wiki-style italics."""
reset = self._head reset = self._head
try: try:
self._really_parse_style(contexts.STYLE_ITALICS, reset, "''", "i")
except BadRoute:
stack = self._parse(contexts.STYLE_ITALICS)
except BadRoute as route:
self._head = reset self._head = reset
self._emit_text("''")
if route.context & contexts.STYLE_PASS_AGAIN:
stack = self._parse(route.context | contexts.STYLE_SECOND_PASS)
else:
return self._emit_text("''")

self._emit_tag_open("i", "''")
self._emit_all(stack)
self._emit_tag_close("i")


def _parse_bold(self): def _parse_bold(self):
"""Parse wiki-style bold.""" """Parse wiki-style bold."""
reset = self._head reset = self._head
try: try:
self._really_parse_style(contexts.STYLE_BOLD, reset, "'''", "b")
stack = self._parse(contexts.STYLE_BOLD)
except BadRoute: except BadRoute:
self._head = reset self._head = reset
if self._context & contexts.STYLE_SECOND_PASS: if self._context & contexts.STYLE_SECOND_PASS:
@@ -686,6 +676,10 @@ class Tokenizer(object):
else: else:
self._emit_text("'") self._emit_text("'")
self._parse_italics() self._parse_italics()
else:
self._emit_tag_open("b", "'''")
self._emit_all(stack)
self._emit_tag_close("b")


def _parse_italics_and_bold(self): def _parse_italics_and_bold(self):
"""Parse wiki-style italics and bold together (i.e., five ticks).""" """Parse wiki-style italics and bold together (i.e., five ticks)."""


Caricamento…
Annulla
Salva