generic text markup tools
Revisão | 5f2dafea1cf41da06d4e952903a6bf7b0cbfa759 (tree) |
---|---|
Hora | 2013-04-01 19:59:57 |
Autor | hylom <hylom@hylo...> |
Commiter | hylom |
writing 'call' action....
@@ -119,6 +119,7 @@ | ||
119 | 119 | "priority": 101, |
120 | 120 | "regexp": "^<(.*)>$", |
121 | 121 | "continue": false, |
122 | + "call": ["getImageGeom", "${image_dir}/\\1"], | |
122 | 123 | "replace": "<a href=\"${image_dir}/\\1\" target=\"_blank\"><img src=\"${image_dir}/\\1\" alt=\"図$ref $caption\" width=\"480\"></a><span class=\"caption\">図$ref $caption</span>" |
123 | 124 | }, |
124 | 125 | "caption": { |
@@ -17,7 +17,7 @@ | ||
17 | 17 | jareでは*[http://example.com/]のようにリンクを埋め込めます。*[http://hylom.net/ このように]リンクを埋めることも可能です。 |
18 | 18 | |
19 | 19 | ☆図1 図のキャプション |
20 | -<imagefile.png> | |
20 | +<network.png> | |
21 | 21 | |
22 | 22 | また、*図1のようにして参照を埋め込めます。*[http://example.com/]のようにリンクも埋め込めます。*[http://hylom.net/ このように]リンクを埋めることも可能です。 |
23 | 23 |
@@ -15,6 +15,9 @@ class Parser(object): | ||
15 | 15 | self.mode_stack = ["global", ] |
16 | 16 | self.mode_stores = {} |
17 | 17 | self._global_stores = {} |
18 | + self.functions = { | |
19 | +# "getImageGeom": getImageGeom, | |
20 | + } | |
18 | 21 | |
19 | 22 | def currentMode(self): |
20 | 23 | try: |
@@ -123,9 +126,6 @@ class Parser(object): | ||
123 | 126 | text = self._do_action(mode, text, new_actions, rex, match) |
124 | 127 | return text |
125 | 128 | |
126 | - if 'replace' in actions: | |
127 | - text = rex.sub(actions['replace'], text) | |
128 | - text = self._expand_variable(mode, text) | |
129 | 129 | if 'store' in actions: |
130 | 130 | arg = actions['store'] |
131 | 131 | if isinstance(arg, list): |
@@ -139,6 +139,17 @@ class Parser(object): | ||
139 | 139 | if 'set' in actions: |
140 | 140 | arg = actions['set'] |
141 | 141 | self._store(mode, arg[0], arg[1]) |
142 | + if 'call' in actions: | |
143 | + (func, newarg) = actions['call'] | |
144 | + if func in self.functions: | |
145 | + arg = match.group(1) | |
146 | + context = self.mode_stores.get(mode, {}) | |
147 | + results = self.functions[func](context, arg) | |
148 | + for (k, v) in results: | |
149 | + self._store(mode, k, v) | |
150 | + if 'replace' in actions: | |
151 | + text = rex.sub(actions['replace'], text) | |
152 | + text = self._expand_variable(mode, text) | |
142 | 153 | return text |
143 | 154 | |
144 | 155 | def _apply_rules(self, mode, text): |
@@ -183,3 +194,7 @@ class Parser(object): | ||
183 | 194 | break |
184 | 195 | |
185 | 196 | return text |
197 | + | |
198 | + | |
199 | +def getImageGeom(context, filename): | |
200 | + return [('width', str(128)), ('height', str(256))] |
@@ -9,7 +9,6 @@ from optparse import OptionParser | ||
9 | 9 | from lexi import Lexi |
10 | 10 | from textparser import Parser |
11 | 11 | |
12 | - | |
13 | 12 | def parse_arguments(): |
14 | 13 | "Generate option parser and parse arguments" |
15 | 14 | usage = "usage: %prog [options] <source_file> <output_file>" |
@@ -33,7 +32,6 @@ def parse_arguments(): | ||
33 | 32 | options.output_file = args[1] |
34 | 33 | return options |
35 | 34 | |
36 | - | |
37 | 35 | def main(): |
38 | 36 | opts = parse_arguments() |
39 | 37 | lexi = Lexi(opts.rule_file) |
@@ -48,12 +46,10 @@ def main(): | ||
48 | 46 | else: |
49 | 47 | output = codecs.open(opts.output_file, 'w', 'utf8') |
50 | 48 | |
51 | - | |
52 | 49 | # do markup |
53 | 50 | parser = Parser(lexi) |
54 | 51 | parser.markup(source, output) |
55 | 52 | |
56 | - | |
57 | 53 | if __name__ == '__main__': |
58 | 54 | main() |
59 | 55 |