generic text markup tools
修订版 | dca924bc0ab8f5d661ed0dee9d86b57bde12cd94 (tree) |
---|---|
时间 | 2013-04-18 19:05:06 |
作者 | hylom <hylom@hylo...> |
Commiter | hylom |
fix action: call
@@ -158,7 +158,7 @@ | ||
158 | 158 | "priority": 101, |
159 | 159 | "regexp": "^<(.*)>$", |
160 | 160 | "continue": false, |
161 | - "call": ["getImageGeometry", "${image_dir}/\\1"], | |
161 | + "call": ["getImageGeometry", "${image_dir}/$1"], | |
162 | 162 | "replace": "<figure style=\"width:${width}px;\">\n<a href=\"${image_dir}/\\1\" target=\"_blank\"><img src=\"${image_dir}/\\1\" alt=\"図$ref $caption\" width=\"${width}\" height=\"${height}\"></a><figcaption>図$ref $caption</figcaption>" |
163 | 163 | }, |
164 | 164 | "caption": { |
@@ -211,10 +211,18 @@ class Parser(object): | ||
211 | 211 | def write(self, text): |
212 | 212 | self.stream_out.write(text) |
213 | 213 | |
214 | - def _expand_variable(self, text): | |
214 | + def _expand_variable(self, text, match=None): | |
215 | 215 | mode = self.current_mode() |
216 | - if not text.find('$'): | |
216 | + if text.find(u'$') < 0: | |
217 | 217 | return text |
218 | + # expand $[0-9]+ | |
219 | + rex = re.compile('\$([0-9]+)') | |
220 | + m = rex.search(text) | |
221 | + if m and match: | |
222 | + sub_func = lambda x:match.group(int(x.group(1))) | |
223 | + text = rex.sub(sub_func, text) | |
224 | + | |
225 | + # expand vars | |
218 | 226 | rex = re.compile('\${?([A-Za-z0-9_]+)}?') |
219 | 227 | m = rex.search(text) |
220 | 228 | sub_func = lambda x:self.store.load(mode, x.group(1), '') |
@@ -260,11 +268,12 @@ class Parser(object): | ||
260 | 268 | |
261 | 269 | def execute_action(self, rex, match, rule, text): |
262 | 270 | if 'call' in rule: |
263 | - (func, newarg) = rule['call'] | |
271 | + param = rule['call'] | |
272 | + (func, args) = param[0], param[1:] | |
264 | 273 | if func in self.functions: |
265 | - arg = match.group(1) | |
266 | 274 | context = self.store |
267 | - results = self.functions[func](context, arg) | |
275 | + args = [self._expand_variable(x, match) for x in args] | |
276 | + results = self.functions[func](context, args) | |
268 | 277 | for (k, v) in results: |
269 | 278 | self.store.save(self.current_mode(), k, v) |
270 | 279 |
@@ -323,7 +332,8 @@ class Parser(object): | ||
323 | 332 | return (False, text) |
324 | 333 | |
325 | 334 | |
326 | -def getImageGeometry(context, filepath): | |
335 | +def getImageGeometry(context, args): | |
336 | + filepath = args[0] | |
327 | 337 | f = open(filepath, 'r') |
328 | 338 | data = f.read() |
329 | 339 | f.close() |