generic text markup tools
Revisión | ff58a35752148fc8750ef8d95b8b49f87727be5f (tree) |
---|---|
Tiempo | 2014-02-06 19:45:05 |
Autor | hylom <hylom@hylo...> |
Commiter | hylom |
implement ctable and fix expand vars bug
@@ -22,9 +22,9 @@ | ||
22 | 22 | "rules": { |
23 | 23 | "tableRow": { |
24 | 24 | "priority": 102, |
25 | - "regexp": "^(.*)$", | |
25 | + "regexp": "^([^〓].*)$", | |
26 | 26 | "call": [ "ctable.tableRow", "$1" ], |
27 | - "replace": "" | |
27 | + "replace": null | |
28 | 28 | }, |
29 | 29 | "tableHeaderRow": { |
30 | 30 | "priority": 104, |
@@ -34,7 +34,7 @@ | ||
34 | 34 | "yes" |
35 | 35 | ], |
36 | 36 | "call": ["ctable.tableHeaderRow", "$1"], |
37 | - "replace": "" | |
37 | + "replace": null | |
38 | 38 | }, |
39 | 39 | "tableCell": { |
40 | 40 | "priority": 103, |
@@ -3,4 +3,10 @@ BASE_DIR=~/repos/tlexi | ||
3 | 3 | TLEXI="python ${BASE_DIR}/tlexi.py" |
4 | 4 | LEXI=${BASE_DIR}jare.py |
5 | 5 | |
6 | -$TLEXI -r ${BASE_DIR}/jarkup.json $1 $2 | |
6 | +case $1 in | |
7 | +-c) | |
8 | + $TLEXI -r ${BASE_DIR}/jarkup.json -a ${BASE_DIR}/ctable.json $2 $3 ;; | |
9 | +*) | |
10 | + $TLEXI -r ${BASE_DIR}/jarkup.json $1 $2 ;; | |
11 | +esac | |
12 | + |
@@ -22,7 +22,7 @@ def tableHeaderRow(context, args): | ||
22 | 22 | |
23 | 23 | def start_tag(tag, attrs={}): |
24 | 24 | if len(attrs) > 0: |
25 | - attr = " " + " ".join([x + "=" + '"' + attrs[x] + '"' for x in attrs.keys()]) | |
25 | + attr = " " + " ".join([x + "=" + '"' + str(attrs[x]) + '"' for x in attrs.keys()]) | |
26 | 26 | else: |
27 | 27 | attr = "" |
28 | 28 | return "<" + tag + attr + ">" |
@@ -37,7 +37,8 @@ def renderTable(context, args): | ||
37 | 37 | buf += "<tr>\n " |
38 | 38 | width = len(table_rows[i]) |
39 | 39 | for j in range(0, width): |
40 | - print ":" + str(i) + "," + str(j) | |
40 | + if table_rows[i][j] == "": | |
41 | + continue | |
41 | 42 | attrs = {} |
42 | 43 | # check: cell is header? |
43 | 44 | if table_attrs.get((i, j), False): |
@@ -45,14 +46,21 @@ def renderTable(context, args): | ||
45 | 46 | else: |
46 | 47 | tag = "td" |
47 | 48 | # check: |
48 | - if i == 0: | |
49 | - rowspan = 0 | |
50 | - for x in range(i+1, height): | |
51 | - if table_rows[x][j] != "": | |
52 | - break; | |
53 | - rowspan += 1; | |
54 | - if rowspan > 0: | |
55 | - attrs["rowspan"] = rowspan | |
49 | + if j == 0: | |
50 | + rowspan = 0 | |
51 | + for x in range(i+1, height): | |
52 | + if table_rows[x][j] != "": | |
53 | + break; | |
54 | + rowspan += 1; | |
55 | + if rowspan > 0: | |
56 | + attrs["rowspan"] = rowspan | |
57 | + colspan = 0 | |
58 | + for y in range(j+1, width): | |
59 | + if table_rows[i][y] != "": | |
60 | + break; | |
61 | + colspan += 1; | |
62 | + if colspan > 0: | |
63 | + attrs["colspan"] = colspan | |
56 | 64 | |
57 | 65 | buf += start_tag(tag, attrs) + table_rows[i][j] + end_tag(tag) |
58 | 66 | buf += "\n</tr>" |
@@ -1,4 +1,73 @@ | ||
1 | 1 | |
2 | + | |
3 | +☆image_dir: img | |
4 | + | |
5 | +<h2 class="title"> | |
6 | +基本的に生HTMLや文字列はそのまま出力されます。 | |
7 | +</h2> | |
8 | + | |
9 | + 行頭が全角スペースの行はパラグラフテキストとして認識されます。 | |
10 | + | |
11 | +●大見出し | |
12 | + | |
13 | + 空行、もしくはホワイトスペースのみの行は無視されます。 | |
14 | + Wiki記法などとは異なり、改行はパラグラフの終了と見なされます。 | |
15 | + | |
16 | + 文字中の<や>、&は適切にエンティティに変換されます。 | |
17 | + | |
18 | + バグがありそうなのは文中に$hogeがあるケースです。 | |
19 | + | |
20 | +○中見出し | |
21 | + jareでは*[http://example.com/]のようにリンクを埋め込めます。*[http://hylom.net/ このように]リンクを埋めることも可能です。 | |
22 | + | |
23 | +☆図1 図のキャプション | |
24 | +<network.png> | |
25 | + | |
26 | + また、*図1のようにして参照を埋め込めます。*[http://example.com/]のようにリンクも埋め込めます。*[http://hylom.net/ このように]リンクを埋めることも可能です。 | |
27 | + | |
28 | +○箇条書き | |
29 | + 箇条書きは以下のようにliタグに変換されます。 | |
30 | + | |
31 | +・1つめ | |
32 | +・2つめ(*g[強調]付き) | |
33 | +・3つめ(*[http://example.com/ リンク]付き | |
34 | + | |
35 | +○実行例/リストの埋め込み | |
36 | + | |
37 | +☆+--- | |
38 | +実行例/リストなどは | |
39 | +このようにして | |
40 | +埋め込めます | |
41 | +文字装飾*b[太字]や*g[赤字]なども | |
42 | +もしくは*b{こういう感じ}や*g{こんな風}にもできます | |
43 | +☆+--- | |
44 | + | |
45 | +☆note 注釈/コラム | |
46 | + 注釈/コラム内ではほかのマークアップがそのまま使えます。 | |
47 | +☆note-end | |
48 | + | |
49 | + 下記、テストその2。 | |
50 | + | |
51 | +☆+--- | |
52 | +実行例/リストなどは | |
53 | +このようにして | |
54 | +うめこめます | |
55 | +☆+--- | |
56 | + | |
57 | +☆表1 表のキャプション | |
58 | +〓表の見出し1 見出し2 | |
59 | +アイテム1 アイテム2 | |
60 | + アイテム3 | |
61 | +foo bar | |
62 | + | |
63 | +☆comment | |
64 | +コメント部分です。 | |
65 | +何も出力されません。 | |
66 | +☆comment-end | |
67 | + | |
68 | +hylomのWebサイト | |
69 | +http:/hylom.net/ | |
70 | + | |
2 | 71 | ●ctableのテスト |
3 | 72 | |
4 | 73 | ctableでは、空セルが自動的に上下のセルに連結されます。 |
@@ -15,6 +15,8 @@ | ||
15 | 15 | |
16 | 16 | 文字中の<や>、&は適切にエンティティに変換されます。 |
17 | 17 | |
18 | + バグがありそうなのは文中に$hogeがあるケースです。 | |
19 | + | |
18 | 20 | ○中見出し |
19 | 21 | jareでは*[http://example.com/]のようにリンクを埋め込めます。*[http://hylom.net/ このように]リンクを埋めることも可能です。 |
20 | 22 |
@@ -1,3 +1,4 @@ | ||
1 | +python ../tlexi.py -r ../jarkup.json ctable.txt out0.txt | |
1 | 2 | python ../tlexi.py -r ../jarkup.json -a ../ctable.json ctable.txt out.txt |
2 | 3 | cat out.txt |
3 | 4 |
@@ -236,18 +236,22 @@ class Parser(object): | ||
236 | 236 | while self.current_mode(): |
237 | 237 | l = iter_in.next().strip('\r\n') |
238 | 238 | out = self._markup(l) |
239 | - self.write(out) | |
240 | - self.write('\n') | |
239 | + if out != None: | |
240 | + self.write(out) | |
241 | + self.write('\n') | |
241 | 242 | except StopIteration: |
242 | 243 | return |
243 | 244 | |
244 | 245 | def raw_write(self, text): |
245 | - self.stream_out.write(text) | |
246 | + if text != None: | |
247 | + self.stream_out.write(text) | |
246 | 248 | |
247 | 249 | def write(self, text): |
248 | 250 | self.raw_write(self._expand_variable(text)) |
249 | 251 | |
250 | 252 | def _expand_variable(self, text, match=None): |
253 | + if text == None: | |
254 | + return None | |
251 | 255 | if text.find(u'$') < 0: |
252 | 256 | return text |
253 | 257 | # expand $[0-9]+ |
@@ -264,8 +268,14 @@ class Parser(object): | ||
264 | 268 | # expand vars |
265 | 269 | rex = re.compile('\${?([A-Za-z0-9_]+)}?') |
266 | 270 | m = rex.search(text) |
267 | - sub_func = lambda x:self.store.load(x.group(1), x.string) | |
268 | - text = rex.sub(sub_func, text) | |
271 | + if m: | |
272 | + def sub_func(x): | |
273 | + val = self.store.load(x.group(1)) | |
274 | + if val != None: | |
275 | + return val | |
276 | + else: | |
277 | + return "$" + x.group(1) | |
278 | + text = rex.sub(sub_func, text) | |
269 | 279 | return text |
270 | 280 | |
271 | 281 | def _markup(self, text): |
@@ -350,8 +360,11 @@ class Parser(object): | ||
350 | 360 | self.store.save(arg[0], arg[1]) |
351 | 361 | |
352 | 362 | if 'replace' in rule: |
353 | - text = rex.sub(rule['replace'], text) | |
354 | - text = self._expand_variable(text) | |
363 | + if rule['replace'] == None: | |
364 | + text = None | |
365 | + else: | |
366 | + text = rex.sub(rule['replace'], text) | |
367 | + text = self._expand_variable(text) | |
355 | 368 | |
356 | 369 | if 'continue' in rule: |
357 | 370 | if rule['continue'] == False: |
@@ -366,9 +379,10 @@ class Parser(object): | ||
366 | 379 | return (False, text) |
367 | 380 | |
368 | 381 | if 'regexp' in rule: |
369 | - rex = re.compile(rule['regexp']) | |
370 | - match = rex.search(text) | |
371 | - if match: | |
372 | - return self.execute_action(rex, match, rule, text) | |
382 | + if text != None: | |
383 | + rex = re.compile(rule['regexp']) | |
384 | + match = rex.search(text) | |
385 | + if match: | |
386 | + return self.execute_action(rex, match, rule, text) | |
373 | 387 | return (False, text) |
374 | 388 |