generic text markup tools
リビジョン | 5649b28e2f691304c4faddbd26f6625dc3ea2dd6 (tree) |
---|---|
日時 | 2013-11-18 20:22:38 |
作者 | hylom <hylom@hylo...> |
コミッター | hylom |
add ctable plugin
@@ -1,12 +1,30 @@ | ||
1 | 1 | { |
2 | - "rules": { | |
2 | + "usePlugins": [ | |
3 | + "ctable" | |
4 | + ], | |
5 | + "modes": { | |
6 | + "global": { | |
7 | + "rules": { | |
8 | + }, | |
9 | + "transitions": [ | |
10 | + "figure", | |
11 | + "code", | |
12 | + "langCode", | |
13 | + "ctable", | |
14 | + "unOrderdList", | |
15 | + "note", | |
16 | + "comment", | |
17 | + "codeList" | |
18 | + ] | |
19 | + }, | |
3 | 20 | "ctable": { |
4 | 21 | "extends": "table", |
5 | 22 | "rules": { |
6 | 23 | "tableRow": { |
7 | 24 | "priority": 102, |
8 | 25 | "regexp": "^(.*)$", |
9 | - "call:" ["ctable.tableRow", "$1"] | |
26 | + "call": [ "ctable.tableRow", "$1" ], | |
27 | + "replace": "" | |
10 | 28 | }, |
11 | 29 | "tableHeaderRow": { |
12 | 30 | "priority": 104, |
@@ -15,7 +33,8 @@ | ||
15 | 33 | "isHeader", |
16 | 34 | "yes" |
17 | 35 | ], |
18 | - "call:" ["ctable.tableHeaderRow", "$1"] | |
36 | + "call": ["ctable.tableHeaderRow", "$1"], | |
37 | + "replace": "" | |
19 | 38 | }, |
20 | 39 | "tableCell": { |
21 | 40 | "priority": 103, |
@@ -23,9 +42,11 @@ | ||
23 | 42 | "replace": "\\t" |
24 | 43 | } |
25 | 44 | }, |
45 | + "begin": "^☆表.*", | |
46 | + "end": "^\\s*$", | |
26 | 47 | "onFinished": { |
27 | - "call:" ["ctable.getTable"], | |
28 | - "insert": "${table}" | |
48 | + "call": ["ctable.flushTable"], | |
49 | + "insert": "${ctable}\n</table></div>\n" | |
29 | 50 | } |
30 | 51 | } |
31 | 52 | } |
@@ -0,0 +1,31 @@ | ||
1 | +# | |
2 | +# -*- codeing: utf-8 -*- | |
3 | +'ctable.py' | |
4 | + | |
5 | +import re | |
6 | + | |
7 | +table_rows = [] | |
8 | + | |
9 | +def tableRow(context, args): | |
10 | + line = args[0] | |
11 | + cells = line.split("\t") | |
12 | + table_rows.append(cells) | |
13 | + return [] | |
14 | + | |
15 | +def tableHeaderRow(context, args): | |
16 | + return tableRow(context, args) | |
17 | + | |
18 | +def renderTable(context, args): | |
19 | + buf = "" | |
20 | + for row in table_rows: | |
21 | + buf += "<tr>\n " | |
22 | + for cell in row: | |
23 | + buf += "<td>" + cell + "</td>" | |
24 | + buf += "\n</tr>" | |
25 | + return [('ctable', buf)] | |
26 | + | |
27 | +def flushTable(context, args): | |
28 | + ret = renderTable(context, args) | |
29 | + table_rows = [] | |
30 | + return ret | |
31 | + |
@@ -0,0 +1,14 @@ | ||
1 | + | |
2 | +●ctableのテスト | |
3 | + | |
4 | + ctableでは、空セルが自動的に上下のセルに連結されます。 | |
5 | + | |
6 | +○実例 | |
7 | + | |
8 | +☆表1 ctableの例 | |
9 | +〓見出し1 見出し2 見出し3 見出し4 | |
10 | +アイテム11 アイテム21 アイテム31 アイテム41 | |
11 | + アイテム22 アイテム32 アイテム42 | |
12 | +アイテム13 アイテム33 アイテム43 | |
13 | +アイテム14 アイテム34 アイテム44 | |
14 | + |
@@ -0,0 +1,3 @@ | ||
1 | +python ../tlexi.py -r ../jarkup.json -a ../ctable.json ctable.txt out.txt | |
2 | +cat out.txt | |
3 | + |
@@ -109,6 +109,15 @@ class Mode(object): | ||
109 | 109 | '''action when mode finished''' |
110 | 110 | if self.has_attr("onFinished"): |
111 | 111 | m = self.attr("onFinished") |
112 | + if "call" in m: | |
113 | + param = m["call"] | |
114 | + (func, args) = param[0], param[1:] | |
115 | + if func in writer.functions: | |
116 | + context = writer.store | |
117 | + args = [writer._expand_variable(x, match) for x in args] | |
118 | + results = writer.functions[func](context, args) | |
119 | + for (k, v) in results: | |
120 | + writer.store.save(k, v) | |
112 | 121 | if "insert" in m: |
113 | 122 | writer.write(m["insert"]) |
114 | 123 | if "replace" in m: |
@@ -176,9 +185,14 @@ def embed_loader(parser, name): | ||
176 | 185 | plugins = __import__(mod_name, globals(), locals(), [], -1) |
177 | 186 | mod = plugins.__getattribute__(name) |
178 | 187 | for elem in mod.__dict__: |
188 | + if elem[0] == '_': | |
189 | + continue | |
190 | + func = mod.__getattribute__(elem) | |
191 | + if not hasattr(func, '__call__'): | |
192 | + continue | |
179 | 193 | func_name = name + "." + elem |
180 | - parser.add_function(func_name, mod.__getattribute__(elem)) | |
181 | - print func_name | |
194 | + parser.add_function(func_name, func) | |
195 | + print "load_function: " + func_name | |
182 | 196 | |
183 | 197 | class Parser(object): |
184 | 198 | """markupper class""" |
@@ -227,8 +241,11 @@ class Parser(object): | ||
227 | 241 | except StopIteration: |
228 | 242 | return |
229 | 243 | |
230 | - def write(self, text): | |
244 | + def raw_write(self, text): | |
231 | 245 | self.stream_out.write(text) |
246 | + | |
247 | + def write(self, text): | |
248 | + self.raw_write(self._expand_variable(text)) | |
232 | 249 | |
233 | 250 | def _expand_variable(self, text, match=None): |
234 | 251 | if text.find(u'$') < 0: |
@@ -339,6 +356,7 @@ class Parser(object): | ||
339 | 356 | if 'continue' in rule: |
340 | 357 | if rule['continue'] == False: |
341 | 358 | return (True, text) |
359 | + | |
342 | 360 | return (False, text) |
343 | 361 | |
344 | 362 |