generic text markup tools
修訂 | 53662205ab30b86aaad5ec3e64797e3ba7e7f032 (tree) |
---|---|
時間 | 2013-04-18 19:50:25 |
作者 | hylom <hylom@hylo...> |
Commiter | hylom |
update jarkup
@@ -22,7 +22,7 @@ | ||
22 | 22 | "red1": { |
23 | 23 | "priority": 102, |
24 | 24 | "regexp": "\\*g\\[(.*?)]", |
25 | - "replace": "<strong class=\"red-text\">\\1</strong>" | |
25 | + "replace": "<strong class=\"color-red\">\\1</strong>" | |
26 | 26 | }, |
27 | 27 | "bold2": { |
28 | 28 | "priority": 103, |
@@ -32,7 +32,7 @@ | ||
32 | 32 | "red2": { |
33 | 33 | "priority": 104, |
34 | 34 | "regexp": "\\*g\\{(.*?)}", |
35 | - "replace": "<strong class=\"red-text\">\\1</strong>" | |
35 | + "replace": "<strong class=\"color-red\">\\1</strong>" | |
36 | 36 | }, |
37 | 37 | "bold1": { |
38 | 38 | "priority": 105, |
@@ -246,7 +246,7 @@ | ||
246 | 246 | }, |
247 | 247 | "blockDevider": "\n", |
248 | 248 | "globalVariables": [ |
249 | - "image_dir", | |
250 | - "." | |
249 | + "image_dir", ".", | |
250 | + "defaultFigureWidth", "480" | |
251 | 251 | ] |
252 | 252 | } |
@@ -134,38 +134,42 @@ class ModeStack(object): | ||
134 | 134 | return None |
135 | 135 | |
136 | 136 | class Store(dict): |
137 | - def __init__(self): | |
137 | + def __init__(self, mode_stack): | |
138 | 138 | self.mode_stores = {} |
139 | 139 | self.global_store = {} |
140 | + self._mode_stack = mode_stack; | |
141 | + | |
142 | + def _mode(self): | |
143 | + return self._mode_stack.current() | |
140 | 144 | |
141 | 145 | def save_global(self, key, value): |
142 | 146 | self.global_store[key] = value |
143 | 147 | |
144 | - def save(self, mode, key, value): | |
145 | - modekey = mode.name | |
146 | - if not modekey in self.mode_stores: | |
147 | - self.mode_stores[modekey] = {} | |
148 | - self.mode_stores[modekey][key] = value | |
148 | + def save(self, key, value): | |
149 | + mode = self._mode() | |
150 | + if not mode in self.mode_stores: | |
151 | + self.mode_stores[mode] = {} | |
152 | + self.mode_stores[mode][key] = value | |
149 | 153 | |
150 | - def load(self, mode, key, default=None): | |
151 | - modekey = mode.name | |
152 | - if (modekey in self.mode_stores) and (key in self.mode_stores[modekey]): | |
153 | - return self.mode_stores[modekey][key] | |
154 | + def load(self, key, default=None): | |
155 | + mode = self._mode() | |
156 | + if (mode in self.mode_stores) and (key in self.mode_stores[mode]): | |
157 | + return self.mode_stores[mode][key] | |
154 | 158 | else: |
155 | 159 | return self.global_store.get(key, default) |
156 | 160 | |
157 | - def delete(self, mode, key): | |
158 | - modekey = mode.name | |
159 | - if not modekey in self.mode_stores: | |
161 | + def delete(self, key): | |
162 | + mode = self._mode() | |
163 | + if not mode in self.mode_stores: | |
160 | 164 | return |
161 | - if not key in self.mode_stores[modekey]: | |
165 | + if not key in self.mode_stores[mode]: | |
162 | 166 | return |
163 | - del self.mode_stores[modekey][key] | |
167 | + del self.mode_stores[mode][key] | |
164 | 168 | |
165 | - def clear(self, mode): | |
166 | - modekey = mode.name | |
167 | - if modekey in self.mode_stores: | |
168 | - del self.mode_stores[modekey] | |
169 | + def clear(self): | |
170 | + mode = self._mode() | |
171 | + if mode in self.mode_stores: | |
172 | + del self.mode_stores[mode] | |
169 | 173 | |
170 | 174 | |
171 | 175 | class Parser(object): |
@@ -180,7 +184,7 @@ class Parser(object): | ||
180 | 184 | self.mode_stack = ModeStack() |
181 | 185 | self.mode_stack.push("global") |
182 | 186 | |
183 | - self.store = Store() | |
187 | + self.store = Store(self.mode_stack) | |
184 | 188 | self.functions = { |
185 | 189 | "getImageGeometry": getImageGeometry, |
186 | 190 | } |
@@ -212,7 +216,6 @@ class Parser(object): | ||
212 | 216 | self.stream_out.write(text) |
213 | 217 | |
214 | 218 | def _expand_variable(self, text, match=None): |
215 | - mode = self.current_mode() | |
216 | 219 | if text.find(u'$') < 0: |
217 | 220 | return text |
218 | 221 | # expand $[0-9]+ |
@@ -225,7 +228,7 @@ class Parser(object): | ||
225 | 228 | # expand vars |
226 | 229 | rex = re.compile('\${?([A-Za-z0-9_]+)}?') |
227 | 230 | m = rex.search(text) |
228 | - sub_func = lambda x:self.store.load(mode, x.group(1), '') | |
231 | + sub_func = lambda x:self.store.load(x.group(1), '') | |
229 | 232 | text = rex.sub(sub_func, text) |
230 | 233 | return text |
231 | 234 |
@@ -242,6 +245,7 @@ class Parser(object): | ||
242 | 245 | #_debug_print(mode.name) |
243 | 246 | if mode.test("end", text): |
244 | 247 | text = mode.on_exit(self, text) |
248 | + self.store.clear() | |
245 | 249 | self.mode_stack.pop() |
246 | 250 | next_mode = self.current_mode() |
247 | 251 | if next_mode: |
@@ -275,7 +279,7 @@ class Parser(object): | ||
275 | 279 | args = [self._expand_variable(x, match) for x in args] |
276 | 280 | results = self.functions[func](context, args) |
277 | 281 | for (k, v) in results: |
278 | - self.store.save(self.current_mode(), k, v) | |
282 | + self.store.save(k, v) | |
279 | 283 | |
280 | 284 | |
281 | 285 | if 'apply' in rule: |
@@ -285,7 +289,7 @@ class Parser(object): | ||
285 | 289 | |
286 | 290 | if 'switch' in rule: |
287 | 291 | for key in rule['switch'].keys(): |
288 | - value = self.store.load(self.current_mode(), key) | |
292 | + value = self.store.load(key) | |
289 | 293 | if value == None: |
290 | 294 | continue |
291 | 295 | if value not in rule['switch'][key]: |
@@ -297,17 +301,17 @@ class Parser(object): | ||
297 | 301 | arg = rule['store'] |
298 | 302 | if isinstance(arg, list): |
299 | 303 | for index in range(len(arg)): |
300 | - self.store.save(self.current_mode(), arg[index], match.group(index+1)) | |
304 | + self.store.save(arg[index], match.group(index+1)) | |
301 | 305 | else: |
302 | - self.store.save(self.current_mode(), arg, match.group(1)) | |
306 | + self.store.save(arg, match.group(1)) | |
303 | 307 | |
304 | 308 | if 'unset' in rule: |
305 | 309 | key = rule['unset'] |
306 | - self.store.delete(self.current_mode(), key) | |
310 | + self.store.delete(key) | |
307 | 311 | |
308 | 312 | if 'set' in rule: |
309 | 313 | arg = rule['set'] |
310 | - self.store.save(self.current_mode(), arg[0], arg[1]) | |
314 | + self.store.save(arg[0], arg[1]) | |
311 | 315 | |
312 | 316 | if 'replace' in rule: |
313 | 317 | text = rex.sub(rule['replace'], text) |
@@ -338,4 +342,7 @@ def getImageGeometry(context, args): | ||
338 | 342 | data = f.read() |
339 | 343 | f.close() |
340 | 344 | (w, h) = imagesize.get_image_size(data) |
341 | - return [('width', str(w)), ('height', str(h))] | |
345 | + return [ | |
346 | + ('width', str(w)), | |
347 | + ('height', str(h)) | |
348 | + ] |