diff options
author | Sergey Poznyakoff <gray@gnu.org.ua> | 2015-07-15 17:11:51 +0300 |
---|---|---|
committer | Sergey Poznyakoff <gray@gnu.org.ua> | 2015-07-15 17:11:51 +0300 |
commit | b3aa1433e6cd41cdb3a6212ad60d5468d6f6d649 (patch) | |
tree | 0cddac3febd3c9d5a0de1dc4681d0836ce3ff93d | |
parent | 288d3c09c06af73ca6413b9692c06d379de319b1 (diff) | |
download | wikitrans-b3aa1433e6cd41cdb3a6212ad60d5468d6f6d649.tar.gz wikitrans-b3aa1433e6cd41cdb3a6212ad60d5468d6f6d649.tar.bz2 |
Get rid of globals
* wikimarkup.py (delim,otag,ctag)
(refstart): Now class attributes
-rw-r--r-- | wikimarkup.py | 73 |
1 files changed, 31 insertions, 42 deletions
diff --git a/wikimarkup.py b/wikimarkup.py index b765594..18e9a21 100644 --- a/wikimarkup.py +++ b/wikimarkup.py @@ -19,28 +19,27 @@ import sys import re from types import * -__all__ = [ "BaseWikiMarkup", "WikiMarkup", - "envtypes" ] +__all__ = [ "BaseWikiMarkup", "WikiMarkup" ] -delim = re.compile("^==+|==+[ \\t]*$|(^----$)|^\\*+|^#+|^[;:]+|(\\[\\[)|\\[|(\\{\\{)|(\\]\\])|\\]|(\\}\\})|\\||(\\'\\'\\'?)|<") -otag = re.compile("<(?P<tag>[a-zA-Z0-9_]+)(?:\s+(?P<args>.+))?\s*(?P<closed>/)?>") -ctag = re.compile("</(?P<tag>[a-zA-Z0-9_]+)\s*>") -refstart = re.compile("^https?://") +class BaseWikiMarkup(object): + + delim = re.compile("^==+|==+[ \\t]*$|(^----$)|^\\*+|^#+|^[;:]+|(\\[\\[)|\\[|(\\{\\{)|(\\]\\])|\\]|(\\}\\})|\\||(\\'\\'\\'?)|<") + otag = re.compile("<(?P<tag>[a-zA-Z0-9_]+)(?:\s+(?P<args>.+))?\s*(?P<closed>/)?>") + ctag = re.compile("</(?P<tag>[a-zA-Z0-9_]+)\s*>") + refstart = re.compile("^https?://") -close_delim = { - '[': ']', - '[[': ']]', - '{{': '}}' -} - -# Environment types: -envtypes = { "*": [ "unnumbered", 0 ], - "#": [ "numbered", 0 ], - ";": [ "defn", 0 ], - ":": [ "defn", 1 ] - } + close_delim = { + '[': ']', + '[[': ']]', + '{{': '}}' + } -class BaseWikiMarkup(object): + # Environment types: + envtypes = { "*": [ "unnumbered", 0 ], + "#": [ "numbered", 0 ], + ";": [ "defn", 0 ], + ":": [ "defn", 1 ] + } toklist = None tokind = 0 @@ -156,16 +155,6 @@ class BaseWikiMarkup(object): for node in tree: self.dump_node(node, level, file) - def rettext(self, text): - if text[-1] == '\n': - if text[0:-1] != '': - yield({ 'type': 'TEXT', - 'content': text[0:-1] }) - yield({ 'type': 'NL', - 'content': '\n' }) - else: - yield({ 'type': 'TEXT', 'content': text }) - def tokread(self): line = None pos = 0 @@ -187,7 +176,7 @@ class BaseWikiMarkup(object): continue self.dprint(100, "LINE: %s", line[pos:]) - m = delim.search(line, pos) + m = self.delim.search(line, pos) if m: if (pos < m.start(0)): @@ -196,14 +185,14 @@ class BaseWikiMarkup(object): t = None if line[m.start(0)] == '<': - m = otag.match(line, pos) + m = self.otag.match(line, pos) if m: pos = m.end(0) if m.group('tag') == 'nowiki': if not m.group('closed'): while 1: try: - m = ctag.match(line) + m = self.ctag.match(line) if m and m.group('tag') == 'nowiki': yield({ 'type': 'TEXT', 'content': line[pos:m.start(0)] }) @@ -228,7 +217,7 @@ class BaseWikiMarkup(object): yield(t) continue else: - m = ctag.match(line, pos) + m = self.ctag.match(line, pos) if m: if m.group('tag') in self.tags: yield( { 'type': 'CTAG', @@ -243,7 +232,7 @@ class BaseWikiMarkup(object): else: pos = m.end(0) content = m.group(0) - if content[0] in envtypes: + if content[0] in self.envtypes: t = { 'type': 'DELIM', 'content': content, 'continuation': pos < len(line) and line[pos] == ":" } @@ -424,7 +413,7 @@ class BaseWikiMarkup(object): def parse_ref(self): tok = self.getkn() self.dprint(80, "ENTER parse_ref, tok %s", tok) - if not (tok['type'] == 'TEXT' and refstart.match(tok['content'])): + if not (tok['type'] == 'TEXT' and self.refstart.match(tok['content'])): self.dprint(80, "LEAVE parse_ref=None") return None @@ -487,8 +476,8 @@ class BaseWikiMarkup(object): tok['type'] = 'TEXT' self.dprint(80, "BEGIN DELIMITER RECOVERY: %s", tok) od = tok['content'] - if od in close_delim: - cd = close_delim[od] + if od in self.close_delim: + cd = self.close_delim[od] lev = 0 for tok in self.toklist[self.tokind+1:]: if tok['type'] == 'NIL': @@ -631,8 +620,8 @@ class BaseWikiMarkup(object): while 1: tok = self.getkn() if tok['type'] == 'DELIM' \ - and tok['content'][0] in envtypes \ - and type == envtypes[tok['content'][0]][0]: + and tok['content'][0] in self.envtypes \ + and type == self.envtypes[tok['content'][0]][0]: if len(tok['content']) < lev: self.ungetkn() break @@ -643,7 +632,7 @@ class BaseWikiMarkup(object): elt = self.parse_line() if not tok['continuation']: list.append({ 'type': 'ELT', - 'subtype': envtypes[tok['content'][0]][1], + 'subtype': self.envtypes[tok['content'][0]][1], 'content': elt }) continue @@ -715,8 +704,8 @@ class BaseWikiMarkup(object): return { 'type': 'BAR' } elif tok['content'][0:2] == "==": return self.parse_header(tok['content']) - elif tok['content'][0] in envtypes: - type = envtypes[tok['content'][0]][0] + elif tok['content'][0] in self.envtypes: + type = self.envtypes[tok['content'][0]][0] lev = len(tok['content']) if tok['content'][0] == ':': t = self.peektkn(2) |