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 | wit-b3aa1433e6cd41cdb3a6212ad60d5468d6f6d649.tar.gz wit-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 | |||
19 | import re | 19 | import re |
20 | from types import * | 20 | from types import * |
21 | 21 | ||
22 | __all__ = [ "BaseWikiMarkup", "WikiMarkup", | 22 | __all__ = [ "BaseWikiMarkup", "WikiMarkup" ] |
23 | "envtypes" ] | ||
24 | 23 | ||
25 | delim = re.compile("^==+|==+[ \\t]*$|(^----$)|^\\*+|^#+|^[;:]+|(\\[\\[)|\\[|(\\{\\{)|(\\]\\])|\\]|(\\}\\})|\\||(\\'\\'\\'?)|<") | 24 | class BaseWikiMarkup(object): |
26 | otag = re.compile("<(?P<tag>[a-zA-Z0-9_]+)(?:\s+(?P<args>.+))?\s*(?P<closed>/)?>") | 25 | |
27 | ctag = re.compile("</(?P<tag>[a-zA-Z0-9_]+)\s*>") | 26 | delim = re.compile("^==+|==+[ \\t]*$|(^----$)|^\\*+|^#+|^[;:]+|(\\[\\[)|\\[|(\\{\\{)|(\\]\\])|\\]|(\\}\\})|\\||(\\'\\'\\'?)|<") |
28 | refstart = re.compile("^https?://") | 27 | otag = re.compile("<(?P<tag>[a-zA-Z0-9_]+)(?:\s+(?P<args>.+))?\s*(?P<closed>/)?>") |
28 | ctag = re.compile("</(?P<tag>[a-zA-Z0-9_]+)\s*>") | ||
29 | refstart = re.compile("^https?://") | ||
29 | 30 | ||
30 | close_delim = { | 31 | close_delim = { |
31 | '[': ']', | 32 | '[': ']', |
32 | '[[': ']]', | 33 | '[[': ']]', |
33 | '{{': '}}' | 34 | '{{': '}}' |
34 | } | 35 | } |
35 | |||
36 | # Environment types: | ||
37 | envtypes = { "*": [ "unnumbered", 0 ], | ||
38 | "#": [ "numbered", 0 ], | ||
39 | ";": [ "defn", 0 ], | ||
40 | ":": [ "defn", 1 ] | ||
41 | } | ||
42 | 36 | ||
43 | class BaseWikiMarkup(object): | 37 | # Environment types: |
38 | envtypes = { "*": [ "unnumbered", 0 ], | ||
39 | "#": [ "numbered", 0 ], | ||
40 | ";": [ "defn", 0 ], | ||
41 | ":": [ "defn", 1 ] | ||
42 | } | ||
44 | 43 | ||
45 | toklist = None | 44 | toklist = None |
46 | tokind = 0 | 45 | tokind = 0 |
@@ -156,16 +155,6 @@ class BaseWikiMarkup(object): | |||
156 | for node in tree: | 155 | for node in tree: |
157 | self.dump_node(node, level, file) | 156 | self.dump_node(node, level, file) |
158 | 157 | ||
159 | def rettext(self, text): | ||
160 | if text[-1] == '\n': | ||
161 | if text[0:-1] != '': | ||
162 | yield({ 'type': 'TEXT', | ||
163 | 'content': text[0:-1] }) | ||
164 | yield({ 'type': 'NL', | ||
165 | 'content': '\n' }) | ||
166 | else: | ||
167 | yield({ 'type': 'TEXT', 'content': text }) | ||
168 | |||
169 | def tokread(self): | 158 | def tokread(self): |
170 | line = None | 159 | line = None |
171 | pos = 0 | 160 | pos = 0 |
@@ -187,7 +176,7 @@ class BaseWikiMarkup(object): | |||
187 | continue | 176 | continue |
188 | 177 | ||
189 | self.dprint(100, "LINE: %s", line[pos:]) | 178 | self.dprint(100, "LINE: %s", line[pos:]) |
190 | m = delim.search(line, pos) | 179 | m = self.delim.search(line, pos) |
191 | 180 | ||
192 | if m: | 181 | if m: |
193 | if (pos < m.start(0)): | 182 | if (pos < m.start(0)): |
@@ -196,14 +185,14 @@ class BaseWikiMarkup(object): | |||
196 | t = None | 185 | t = None |
197 | 186 | ||
198 | if line[m.start(0)] == '<': | 187 | if line[m.start(0)] == '<': |
199 | m = otag.match(line, pos) | 188 | m = self.otag.match(line, pos) |
200 | if m: | 189 | if m: |
201 | pos = m.end(0) | 190 | pos = m.end(0) |
202 | if m.group('tag') == 'nowiki': | 191 | if m.group('tag') == 'nowiki': |
203 | if not m.group('closed'): | 192 | if not m.group('closed'): |
204 | while 1: | 193 | while 1: |
205 | try: | 194 | try: |
206 | m = ctag.match(line) | 195 | m = self.ctag.match(line) |
207 | if m and m.group('tag') == 'nowiki': | 196 | if m and m.group('tag') == 'nowiki': |
208 | yield({ 'type': 'TEXT', | 197 | yield({ 'type': 'TEXT', |
209 | 'content': line[pos:m.start(0)] }) | 198 | 'content': line[pos:m.start(0)] }) |
@@ -228,7 +217,7 @@ class BaseWikiMarkup(object): | |||
228 | yield(t) | 217 | yield(t) |
229 | continue | 218 | continue |
230 | else: | 219 | else: |
231 | m = ctag.match(line, pos) | 220 | m = self.ctag.match(line, pos) |
232 | if m: | 221 | if m: |
233 | if m.group('tag') in self.tags: | 222 | if m.group('tag') in self.tags: |
234 | yield( { 'type': 'CTAG', | 223 | yield( { 'type': 'CTAG', |
@@ -243,7 +232,7 @@ class BaseWikiMarkup(object): | |||
243 | else: | 232 | else: |
244 | pos = m.end(0) | 233 | pos = m.end(0) |
245 | content = m.group(0) | 234 | content = m.group(0) |
246 | if content[0] in envtypes: | 235 | if content[0] in self.envtypes: |
247 | t = { 'type': 'DELIM', | 236 | t = { 'type': 'DELIM', |
248 | 'content': content, | 237 | 'content': content, |
249 | 'continuation': pos < len(line) and line[pos] == ":" } | 238 | 'continuation': pos < len(line) and line[pos] == ":" } |
@@ -424,7 +413,7 @@ class BaseWikiMarkup(object): | |||
424 | def parse_ref(self): | 413 | def parse_ref(self): |
425 | tok = self.getkn() | 414 | tok = self.getkn() |
426 | self.dprint(80, "ENTER parse_ref, tok %s", tok) | 415 | self.dprint(80, "ENTER parse_ref, tok %s", tok) |
427 | if not (tok['type'] == 'TEXT' and refstart.match(tok['content'])): | 416 | if not (tok['type'] == 'TEXT' and self.refstart.match(tok['content'])): |
428 | self.dprint(80, "LEAVE parse_ref=None") | 417 | self.dprint(80, "LEAVE parse_ref=None") |
429 | return None | 418 | return None |
430 | 419 | ||
@@ -487,8 +476,8 @@ class BaseWikiMarkup(object): | |||
487 | tok['type'] = 'TEXT' | 476 | tok['type'] = 'TEXT' |
488 | self.dprint(80, "BEGIN DELIMITER RECOVERY: %s", tok) | 477 | self.dprint(80, "BEGIN DELIMITER RECOVERY: %s", tok) |
489 | od = tok['content'] | 478 | od = tok['content'] |
490 | if od in close_delim: | 479 | if od in self.close_delim: |
491 | cd = close_delim[od] | 480 | cd = self.close_delim[od] |
492 | lev = 0 | 481 | lev = 0 |
493 | for tok in self.toklist[self.tokind+1:]: | 482 | for tok in self.toklist[self.tokind+1:]: |
494 | if tok['type'] == 'NIL': | 483 | if tok['type'] == 'NIL': |
@@ -631,8 +620,8 @@ class BaseWikiMarkup(object): | |||
631 | while 1: | 620 | while 1: |
632 | tok = self.getkn() | 621 | tok = self.getkn() |
633 | if tok['type'] == 'DELIM' \ | 622 | if tok['type'] == 'DELIM' \ |
634 | and tok['content'][0] in envtypes \ | 623 | and tok['content'][0] in self.envtypes \ |
635 | and type == envtypes[tok['content'][0]][0]: | 624 | and type == self.envtypes[tok['content'][0]][0]: |
636 | if len(tok['content']) < lev: | 625 | if len(tok['content']) < lev: |
637 | self.ungetkn() | 626 | self.ungetkn() |
638 | break | 627 | break |
@@ -643,7 +632,7 @@ class BaseWikiMarkup(object): | |||
643 | elt = self.parse_line() | 632 | elt = self.parse_line() |
644 | if not tok['continuation']: | 633 | if not tok['continuation']: |
645 | list.append({ 'type': 'ELT', | 634 | list.append({ 'type': 'ELT', |
646 | 'subtype': envtypes[tok['content'][0]][1], | 635 | 'subtype': self.envtypes[tok['content'][0]][1], |
647 | 'content': elt }) | 636 | 'content': elt }) |
648 | continue | 637 | continue |
649 | 638 | ||
@@ -715,8 +704,8 @@ class BaseWikiMarkup(object): | |||
715 | return { 'type': 'BAR' } | 704 | return { 'type': 'BAR' } |
716 | elif tok['content'][0:2] == "==": | 705 | elif tok['content'][0:2] == "==": |
717 | return self.parse_header(tok['content']) | 706 | return self.parse_header(tok['content']) |
718 | elif tok['content'][0] in envtypes: | 707 | elif tok['content'][0] in self.envtypes: |
719 | type = envtypes[tok['content'][0]][0] | 708 | type = self.envtypes[tok['content'][0]][0] |
720 | lev = len(tok['content']) | 709 | lev = len(tok['content']) |
721 | if tok['content'][0] == ':': | 710 | if tok['content'][0] == ':': |
722 | t = self.peektkn(2) | 711 | t = self.peektkn(2) |