generic text markup tools
Revisión | a108d1a6634b0764d75500470ed232ab98be1698 (tree) |
---|---|
Tiempo | 2013-04-04 20:04:40 |
Autor | hylom <hylom@hylo...> |
Commiter | hylom |
rewriting textparser...
@@ -3,71 +3,53 @@ | ||
3 | 3 | |
4 | 4 | import re |
5 | 5 | |
6 | +def CreateMode(lexi, mode_name): | |
7 | + return Mode(lexi, mode_name) | |
6 | 8 | |
7 | -class Mode(dict): | |
8 | - def __init__(self, modes, defs): | |
9 | - self.update(defs) | |
10 | - self.modes = modes | |
11 | 9 | |
12 | - def check_to_exit(self, text): | |
13 | - rex = self.get("end", False) | |
10 | +class ModeBase(dict): | |
11 | + def __init__(self, lexi, mode_name): | |
12 | + self.lexi = lexi | |
13 | + modes = lexi.get("modes") | |
14 | + self.update(modes[mode_name]) | |
15 | + | |
16 | + def match(self, key, text): | |
17 | + rex = self.get(key, False) | |
14 | 18 | if rex and re.search(rex, text): |
15 | 19 | return True |
16 | 20 | else: |
17 | 21 | return False |
18 | 22 | |
19 | - def exit(self): | |
20 | - if "onFinished" in self: | |
21 | - m = self["onFinished"] | |
22 | - if "insert" in m: | |
23 | - self._write(m["insert"]) | |
24 | - if "replace" in m: | |
25 | - text = m["replace"] | |
26 | - if len(self.mode_stack) == 0: | |
27 | - return text | |
28 | - mode = self.mode_stack[-1] | |
23 | + def has_element(self, key): | |
24 | + return (key in self) | |
29 | 25 | |
26 | + def write(self, text): | |
27 | + self.writer.write(text) | |
30 | 28 | |
31 | - def apply(self, text): | |
32 | - if self._check_to_exit(mode, text): | |
33 | - if "onFinished" in self: | |
34 | - m = self["onFinished"] | |
35 | - if "insert" in m: | |
36 | - self._write(m["insert"]) | |
37 | - if "replace" in m: | |
38 | - text = m["replace"] | |
39 | - if len(self.mode_stack) == 0: | |
40 | - return text | |
41 | - mode = self.mode_stack[-1] | |
29 | +class Mode(dict): | |
30 | + def __init__(self, lexi, writer, defs): | |
31 | + ModeBase.__init__(self, lexi, writer) | |
32 | + self.update(defs) | |
42 | 33 | |
43 | - next_mode = self._transition_check(mode, text) | |
44 | - if next_mode: | |
45 | - self._transition(next_mode) | |
46 | - mode = next_mode | |
47 | - if "onStart" in self.modeOf(mode): | |
48 | - m = self.modeOf(mode)["onStart"] | |
49 | - if "insert" in m: | |
50 | - self._write(m["insert"]) | |
34 | + def mode_end_check(self, text): | |
35 | + return self.match("end", text) | |
51 | 36 | |
52 | - rules = self.rulesOf(mode) | |
53 | - for key in self._sorted_keys(rules): | |
54 | - rule = rules[key] | |
55 | - if rule.get('pass', False): | |
56 | - continue | |
57 | - if 'regexp' in rule: | |
58 | - rex = re.compile(rule['regexp']) | |
59 | - match = rex.search(text) | |
60 | - if match: | |
61 | - sub_rule = rule.get('apply', False) | |
62 | - if sub_rule: | |
63 | - text = self._apply_rules(sub_rule, text) | |
64 | - text = self._do_action(mode, text, rule, rex, match) | |
65 | - if not rule.get('continue', True): | |
66 | - break | |
67 | 37 | |
68 | - return text | |
38 | +class ModeStack(object): | |
39 | + def __init__(self): | |
40 | + self.stack = [] | |
41 | + | |
42 | + def push(self, mode): | |
43 | + self.stack.append(mode) | |
69 | 44 | |
45 | + def pop(self): | |
46 | + return self.stack.pop() | |
70 | 47 | |
48 | + def current(self): | |
49 | + try: | |
50 | + return self.mode_stack[-1] | |
51 | + except IndexError: | |
52 | + return False | |
71 | 53 | |
72 | 54 | class Store(dict): |
73 | 55 | def __init__(self): |
@@ -109,17 +91,18 @@ class Parser(object): | ||
109 | 91 | @return TextWriter object |
110 | 92 | """ |
111 | 93 | self.lexi = lexi |
112 | - self.mode_stack = ["global", ] | |
94 | +# self.mode_stack = ["global", ] | |
95 | + self.mode_stack = ModeStack() | |
96 | + mode_global = CreateMode(self.lexi, "global") | |
97 | + self.mode_stack.push(mode_global) | |
98 | + | |
113 | 99 | self.store = Store() |
114 | 100 | self.functions = { |
115 | 101 | # "getImageGeom": getImageGeom, |
116 | 102 | } |
117 | 103 | |
118 | - def currentMode(self): | |
119 | - try: | |
120 | - return self.mode_stack[-1] | |
121 | - except IndexError: | |
122 | - return False | |
104 | + def current_mode(self): | |
105 | + return self.mode_stack.current() | |
123 | 106 | |
124 | 107 | def markup(self, iter_in, stream_out): |
125 | 108 | """read from iter_in and output to stream_out |
@@ -225,7 +208,7 @@ class Parser(object): | ||
225 | 208 | text = self._expand_variable(mode, text) |
226 | 209 | return text |
227 | 210 | |
228 | - def _apply_rules2(self, mode, text): | |
211 | + def _apply_rules(self, mode, text): | |
229 | 212 | # check global rule |
230 | 213 | gi = self.lexi.get("globalIdentifier", False) |
231 | 214 | if gi: |
@@ -234,13 +217,17 @@ class Parser(object): | ||
234 | 217 | self.store.save_global(m_gvi.group(1), m_gvi.group(2)) |
235 | 218 | return '' |
236 | 219 | |
237 | - # extends | |
238 | - if "extends" in self.modeOf(mode): | |
239 | - pass | |
240 | - | |
241 | - if mode.check_to_exit(mode, text): | |
242 | - mode.exit() | |
220 | + if self._mode_end_check(mode, text): | |
243 | 221 | self._mode_exit() |
222 | + if "onFinished" in self.modeOf(mode): | |
223 | + m = self.modeOf(mode)["onFinished"] | |
224 | + if "insert" in m: | |
225 | + self._write(m["insert"]) | |
226 | + if "replace" in m: | |
227 | + text = m["replace"] | |
228 | + if len(self.mode_stack) == 0: | |
229 | + return text | |
230 | + mode = self.mode_stack[-1] | |
244 | 231 | |
245 | 232 | next_mode = self._transition_check(mode, text) |
246 | 233 | if next_mode: |
@@ -269,8 +256,7 @@ class Parser(object): | ||
269 | 256 | |
270 | 257 | return text |
271 | 258 | |
272 | - | |
273 | - def _apply_rules(self, mode, text): | |
259 | + def _apply_rules2(self, text): | |
274 | 260 | # check global rule |
275 | 261 | gi = self.lexi.get("globalIdentifier", False) |
276 | 262 | if gi: |
@@ -279,26 +265,24 @@ class Parser(object): | ||
279 | 265 | self.store.save_global(m_gvi.group(1), m_gvi.group(2)) |
280 | 266 | return '' |
281 | 267 | |
282 | - if self._mode_end_check(mode, text): | |
283 | - self._mode_exit() | |
284 | - if "onFinished" in self.modeOf(mode): | |
285 | - m = self.modeOf(mode)["onFinished"] | |
268 | + mode = self.current_mode() | |
269 | + if mode.mode_end_check(text): | |
270 | + if mode.has_element("onFinished"): | |
271 | + m = mode.get("onFinished") | |
286 | 272 | if "insert" in m: |
287 | 273 | self._write(m["insert"]) |
288 | 274 | if "replace" in m: |
289 | 275 | text = m["replace"] |
290 | - if len(self.mode_stack) == 0: | |
291 | - return text | |
292 | - mode = self.mode_stack[-1] | |
293 | 276 | |
294 | - next_mode = self._transition_check(mode, text) | |
295 | - if next_mode: | |
296 | - self._transition(next_mode) | |
297 | - mode = next_mode | |
298 | - if "onStart" in self.modeOf(mode): | |
299 | - m = self.modeOf(mode)["onStart"] | |
300 | - if "insert" in m: | |
301 | - self._write(m["insert"]) | |
277 | + self.mode_stack.pop() | |
278 | + if len(self.mode_stack) > 0: | |
279 | + # start next mode | |
280 | + if self.current().hasElement("onStart"): | |
281 | + m = self.current()["onStart"] | |
282 | + if "insert" in m: | |
283 | + self._write(m["insert"]) | |
284 | + text = self._apply_rules2(text) | |
285 | + return text | |
302 | 286 | |
303 | 287 | rules = self.rulesOf(mode) |
304 | 288 | for key in self._sorted_keys(rules): |
@@ -319,5 +303,7 @@ class Parser(object): | ||
319 | 303 | return text |
320 | 304 | |
321 | 305 | |
306 | + | |
307 | + | |
322 | 308 | def getImageGeom(context, filename): |
323 | 309 | return [('width', str(128)), ('height', str(256))] |