OSDN Git Service

fix get_png_geom
[otptools/otptools.git] / markupper.py
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3  
4 import sys
5 import os
6 import re
7 import codecs
8 import pickle
9
10 import HTMLTagFilter
11 import deterfile
12 import getjpggeom
13
14 #sys.stdin = codecs.getreader('utf_8')(sys.stdin)
15 #sys.stdout = codecs.getwriter('utf_8')(sys.stdout)
16
17 alist = ["a", "a:href", "a:name", "b", "br" ]
18 dlist = ["*"]
19
20 tag_filter = HTMLTagFilter.HTMLTagFilter(HTMLTagFilter.DENY_ALLOW, alist, dlist)
21 path_to_index = "./_markup_index"
22
23 class _InputStream(object):
24     """InputStream base class."""
25     def __init__(self):
26         pass
27
28     def __iter__(self):
29         """return Iterator"""
30         return self
31
32     def next(self):
33         """function for iterator"""
34         pass
35
36
37 class Markupper(object):
38     """
39     """
40     def __init__(self):
41         self._input_iter = None
42         self._index_past = {}
43         self._index = {}
44         self._image_dir = ""
45
46     def index_add(self, key, val):
47         """
48         Add key and value to index.
49
50         @param key:
51          @type key:
52
53         @param val:
54         @key val:
55         """
56         self._index[key] = val
57
58     def index(self, key):
59         """
60         Get index
61         """
62         return self._index[key]
63
64     def index_haskey(self, key):
65         return self._index.has_key(key)
66
67     def markup(self, input_iter, release="0"):
68         """
69         Do markup.
70
71         @param input_iter: iterator to use as input
72         @type input_iter: iterator
73         """
74         self.input_iter = input_iter
75         self._page_counter = 1
76         self._image_border = 0
77         # alist = ["a", "a:href", "a:name", "b", "br" ]
78         # dlist = ["*"]
79         # tag_filter = HTMLTagFilter.HTMLTagFilter(HTMLTagFilter.DENY_ALLOW, alist, dlist)
80         self._release = release
81
82         self.index_add("figs", [])
83
84         self._anchor = ""
85         for line in self.input_iter:
86             # line = self._default_markup_rule(line)
87             # head-of-line rules
88             if re.search(ur"^☆{{{$", line):
89                 self._inline(line)
90                 continue
91             elif re.search(ur"^☆image_dir:", line):
92                 self._image_dir = re.search(ur"^☆image_dir:\s*(.*)$", line).group(1)
93                 continue
94             elif re.search(ur"^☆image_border:\s(on|On|ON)", line):
95                 self._image_border = 1
96                 continue
97             elif re.search(ur"^☆comment\s{{{$", line):
98                 self._comment(line)
99                 continue
100             elif re.search(ur"^☆\*", line):
101                 self._anchor = re.sub(ur"^☆\*", "", line).strip()
102                 continue
103             elif re.search(ur"^☆clear\s+", line):
104                 self._clear(line)
105                 continue
106             elif re.search(ur"^・", line):
107                 self._ulist(line)
108                 continue
109             elif re.search(ur"^[0-9]\.", line):
110                 self._olist(line)
111                 continue
112             elif re.search(ur"^☆begin-column:", line):
113                 self._begin_column(line)
114                 continue
115             elif re.search(ur"^☆end-column", line):
116                 self._end_column(line)
117                 continue
118             elif re.search(ur"^☆space", line):
119                 self._space(line)
120                 continue
121             elif re.search(ur"^☆call_tables", line):
122                 self._call_tables(line)
123                 continue
124             elif re.search(ur"^●", line):
125                 self._head_l(line)
126                 continue
127             elif re.search(ur"^○", line):
128                 self._head_m(line)
129                 continue
130             elif re.search(ur"^◇", line):
131                 self._head_s(line)
132                 continue
133             elif re.search(ur"^☆----", line):
134                 self._newpage(line)
135                 continue
136             elif re.search(ur"^☆\+---", line):
137                 self._code(line)
138                 continue
139             elif re.search(ur"^☆表", line):
140                 self._table(line)
141                 continue
142             elif re.search(ur"^☆図", line):
143                 self._fig(line)
144                 continue
145             elif re.search(ur"^☆写真", line):
146                 self._photo(line)
147                 continue
148             elif re.search(ur"^☆リスト", line):
149                 self._list(line)
150                 continue
151             elif re.search(ur"^☆flow", line):
152                 self._flow(line)
153                 continue
154
155             if re.search(ur"^ ", line):
156                 self._paragraph(line)
157                 continue
158
159
160             if re.search(r"^\s*$", line):
161                 line = ""
162
163             line = line.strip()
164             print line
165
166         # end-of-loop
167
168     def _clear(self, line):
169         print """<div style="clear:left;"> </div>
170 """
171
172     def _head_l(self, line):
173         line = line.rstrip()
174         if re.search(ur"\*{[a-zA-Z0-9_]*}\s*$", line):
175             self._anchor = re.search(ur"\*\{([a-zA-Z0-9_]*)\}\s*$", line).group(1)
176             line = re.sub(ur"\s*\*\{[a-zA-Z0-9_]*\}\s*$", "", line)
177
178         line = self._default_markup_rule(line)
179         if self._anchor != "":
180             line = re.sub(ur"^●(.*)$", ur'<div id="%s"><h3>\1</h3></div>' % self._anchor, line)
181             self._anchor = ""
182         else:
183             line = re.sub(ur"^●(.*)$", ur"<h3>\1</h3>", line)
184         print line
185
186     def _head_m(self, line):
187         line = line.rstrip()
188         if re.search(ur"\*{[a-zA-Z0-9_]*}\s*$", line):
189             self._anchor = re.search(ur"\*\{([a-zA-Z0-9_]*)\}\s*$", line).group(1)
190             line = re.sub(ur"\s*\*\{[a-zA-Z0-9_]*\}\s*$", "", line)
191
192         line = self._default_markup_rule(line)
193         if self._anchor != "":
194             line = re.sub(ur"^○(.*)$", ur'<div id="%s"><h4>\1</h4></div>' % self._anchor, line)
195             self._anchor = ""
196         else:
197             line = re.sub(ur"^○(.*)$", ur"<h4>\1</h4>", line)
198         print line
199
200     def _head_s(self, line):
201         line = line.rstrip()
202         if re.search(ur"\*{[a-zA-Z0-9_]*}\s*$", line):
203             self._anchor = re.search(ur"\*\{([a-zA-Z0-9_]*)\}\s*$", line).group(1)
204             line = re.sub(ur"\s*\*\{[a-zA-Z0-9_]*\}\s*$", "", line)
205
206         line = self._default_markup_rule(line)
207         if self._anchor != "":
208             line = re.sub(ur"^◇(.*)$", ur'<div id="%s"><h5>\1</h5></div>' % self._anchor, line)
209             self._anchor = ""
210         else:
211             line = re.sub(ur"^◇(.*)$", ur"<h5>\1</h5>", line)
212         print line
213
214     def _paragraph(self, line):
215         line = self._default_markup_rule(line)
216         line = "<p>" + line + "</p>"
217         print line
218
219     def _newpage(self, line):
220         line = re.sub(ur"☆----.*-{0,1}", u"<hr>", line)
221         print line
222
223     def load_index(self, path_to_index):
224         """
225         load index database.
226
227         @param path_to_index: index db's path
228         @type path_to_index: string
229         """
230         # load index
231         try:
232             index_file = open(path_to_index, "r")
233             self._index_past = pickle.load(index_file)
234             index_file.close()
235         except IOError:
236             sys.stderr.write("warn: cannot read index file,\n")
237
238     def save_index(self, path_to_index):
239         """
240         save index database.
241
242         @param path_to_index: index db's path
243         @type path_to_index: string
244         """
245         # save index
246         try:
247             index_file = open(path_to_index, "w")
248             pickle.dump(self._index, index_file)
249             index_file.close()
250         except IOError:
251             sys.stderr.write("warn: cannot write index file,\n")
252
253     def make_hashlist(self, path_to_hashfile):
254         """
255         create hash list.
256
257         @param path_to_hashfile: hashfile's path
258         @type path_to_hashfile: string
259         """
260         try:
261             file_img_hash = open(path_to_hashfile, "r")
262         except IOError:
263             sys.stderr.write("cannot open file: %s" % path_img_hash)
264             return None;
265
266         self.hashlist = {};
267         for line in file_img_hash:
268             splited = line.strip().split("\t", 2)
269             # hashlist's format: <hash> \t <filename>
270             self.hashlist[splited[1]] = splited[0]
271
272     def _call_tagles(self):
273         pass
274
275     def _escape(self, line):
276         line = re.sub(ur"&", ur"&amp;", line)
277         line = re.sub(ur"<", ur"&lt;", line)
278         line = re.sub(ur">", ur"&gt;", line)
279         return line
280
281     def _default_markup_rule(self, line):
282         """
283         apply default markup rules.
284
285         @param line: string to apply markup
286         @type line: string
287         """
288         line = self._escape(line)
289
290         # apply filter
291         # line = tag_filter.apply(line)
292
293         line = re.sub(ur"[★*](動画[0-9〜~、]+)", ur"<b>\1</b>", line)
294         line = re.sub(ur"[★*](表[0-9〜~、]+)", ur"<b>\1</b>", line)
295         line = re.sub(ur"[★*](図[0-9〜~、]+)", ur"<b>\1</b>", line)
296         line = re.sub(ur"[★*](写真[0-9〜~、]+)", ur"<b>\1</b>", line)
297         line = re.sub(ur"[★*](リスト[0-9~〜、]+)", ur"<b>\1</b>", line)
298         line = re.sub(ur"[★*](コラム[0-9〜~、]+)", ur"<b>\1</b>", line)
299         line = re.sub(ur"[★*]b\[(.*?)\]", ur"<b>\1</b>", line)
300         line = re.sub(ur"[★*]b\{(.*?)\}", ur"<b>\1</b>", line)
301         line = re.sub(ur"[★*]g\[(.*?)]", ur"<span style='color:#F55;font-weight:bold;'>\1</span>", line)
302         line = re.sub(ur"[★*]g{(.*?)}", ur"<span style='color:#F55;font-weight:bold;'>\1</span>", line)
303         line = re.sub(ur"[★*]\[(\S*) (.*?)\]", r'<a href="\1">\2</a>', line)
304         line = re.sub(ur"[★*]\[(\S*)\]", r'<a href="\1">\1</a>', line)
305
306         # comment
307         if re.search(ur"^☆#", line):
308             line = ""
309
310         return line
311
312
313     def _ulist(self, line):
314         """Proccess ul"""
315         print "<ul>"
316         while re.search(ur"^・", line):
317             line = self._default_markup_rule(line)
318             print re.sub(ur"^・(.*)$", ur"<li>\1</li>", line.strip())
319             line = self.input_iter.next()
320         print "</ul>\n"
321
322     def _olist(self, line):
323         """Proccess ul"""
324         print "<ol>"
325         while re.search(ur"^[0-9]+\.", line):
326             line = self._default_markup_rule(line)
327             print re.sub(ur"^[0-9]+\.(.*)$", ur"<li>\1</li>", line.strip())
328             line = self.input_iter.next()
329         print "</ol>\n"
330
331
332
333     def _begin_column(self, line):
334         """Proccess column"""
335         try:
336             str_title = re.search(ur"^☆begin-column:(.*)$", line).group(1)
337         except AttributeError:
338             str_title = ""
339
340         html = """
341 <div class="column" style="background:#DDDDDD;font-size:85%%;padding:8px;"> 
342 <h4>%s</h4>
343     """ % (str_title)
344         print html
345
346     def _end_column(self, line):
347         print """
348 </div>
349     """
350
351     def _list_start(self):
352         return "<pre>"
353
354     def _list_end(self):
355         return "</pre>"
356
357     def _list(self, line):
358         try:
359             str_title = re.search(ur"^☆(リスト.*)$", line).group(1)
360         except AttributeError:
361             str_title = ""
362         print "<p class='caption'><b>%s</b></p>" % (str_title)
363         print self._list_start()
364
365         for line in self.input_iter:
366             line = line.strip("\n\r")
367             line = self._escape(line)
368             line = re.sub(ur"[★*]b\[(.*?)]", ur"<b>\1</b>", line)
369             line = re.sub(ur"[★*]b{(.*?)}", ur"<b>\1</b>", line)
370             line = re.sub(ur"[★*]g\[(.*?)]", ur"<span style='color:#F55;font-weight:bold;'>\1</span>", line)
371             line = re.sub(ur"[★*]g{(.*?)}", ur"<span style='color:#F55;font-weight:bold;'>\1</span>", line)
372             if re.search(ur"""^☆\+---""", line):
373                 break
374             print line
375         print self._list_end()
376
377     def _code(self, line):
378         print self._list_start()
379
380         for line in self.input_iter:
381             line = self._escape(line)
382             line = re.sub(ur"[★*]b\[(.*?)]", ur"<b>\1</b>", line)
383             line = re.sub(ur"[★*]b{(.*?)}", ur"<b>\1</b>", line)
384             line = re.sub(ur"[★*]g\[(.*?)]", ur"<span style='color:#F55;font-weight:bold;'>\1</span>", line)
385             line = re.sub(ur"[★*]g{(.*?)}", ur"<span style='color:#F55;font-weight:bold;'>\1</span>", line)
386
387             if re.search(ur"^☆\+---$", line):
388                 break
389             print line,
390         print self._list_end()
391
392     def _inline(self, line):
393         for line in self.input_iter:
394             line = line.strip()
395             if re.search(ur"^☆}}}", line):
396                 break
397             print line
398
399     def _comment(self, line):
400         for line in self.input_iter:
401             line = line.strip()
402             if re.search(ur"^☆}}}", line):
403                 break
404
405     def _space(self, line):
406         print "<br><br>"
407
408     def _flow(self, line):
409         down_arrow = "http://static.sourceforge.jp/crystal/22x22/actions/1downarrow.png"
410         flow_header = """<div style="text-align:center; border: 1px solid; background-color:#EFF2F0; width:90%; margin: 0 auto 1em;">
411 """
412         flow_title = """<div style="text-align:left; padding:4px 4px 4px 1em; margin-bottom: 1em; border-bottom: 1px solid; font-weight: bold; background-color:#BCD;">
413 %s
414 </div>"""
415         flow_footer = """</div>
416 """
417         flow_item = """<div>
418 %s
419 <p>%s</p>
420 </div>
421
422 """
423         arrow = '<div style="margin:1em auto;"><img src="%s"></div>\n' % (down_arrow,)
424
425         rex_title = re.compile(ur"^☆flow\s+(.*)$")
426         if rex_title.search(line):
427             title = rex_title.search(line).group(1)
428         else:
429             title = ""
430
431         rex_file = re.compile(ur"^([^:]*):(.*)$")
432         outputs = []
433         for line in self.input_iter:
434             if re.search(r"^\s*$", line):
435                 break
436             match = rex_file.search(line)
437             if match:
438                 file = os.path.join(self._image_dir, match.group(1))
439                 cap = self._default_markup_rule(match.group(2))
440             else:
441                 continue
442             fig = self._anchored_fig(file, cap)
443             outputs.append(flow_item % (fig, cap))
444
445         print flow_header
446         print flow_title % (title,)
447         print arrow.join(outputs)
448         print flow_footer
449         
450
451     def _fig_start(self, cap="", styles=[], width=0, height=0):
452         params = dict(style="", tablewidth="")
453         if width != 0:
454             params["style"] = "width:%d;" % (width,)
455             params["tablewidth"] = 'width="%d"' % (width,)
456
457         if "lfloat" in styles:
458             return """<table %(tablewidth)s align="center" border="0" cellpadding="0" cellspacing="0" style="float:left; padding-left: 0.5em; %(style)s">
459     <tr> <td valign="top" align="center">
460     """ % params
461         elif "left" in styles:
462             return """<table %(tablewidth)s border="0" cellpadding="0" cellspacing="0" style="padding-left: 0.5em; %(style)s">
463     <tr> <td valign="top" align="center">
464     """ %params
465         else:
466             return """<table %(tablewidth)s align="center" border="0" cellpadding="0" cellspacing="0">
467     <tr> <td valign="top" align="center">
468     """ % params
469
470     def _fig_end(self, cap="", styles=[]):
471         return """</td> </tr>
472     <tr> <td><span style="font-size: 80%%; font-weight: bold;">
473     %s
474     </span></td> </tr>
475     </table>
476     """ % (cap)
477
478     def _get_png_geom(self, filepath):
479         s = filepath.split('.')
480         ext = s[-1]
481         if (ext == 'JPG') or (ext == 'jpg'):
482             (w, h) = getjpggeom.get_jpeg_geometory(filepath)
483             return (w, h)
484         else:
485             desc = deterfile.file(filepath)
486
487         try:
488             m = re.match(r"([0-9]+)\s*x\s*([0-9]+)", desc[1])
489         except IndexError:
490             err = ", ".join(desc)
491             raise Exception("deterfile error: %s, file: %s . " % (err,filepath))
492         if m:
493             w = m.group(1)
494             h = m.group(2)
495             return (int(w), int(h))
496         else:
497             return None
498
499     def _fig(self, line):
500         try:
501             str_title = re.search(ur"^☆(図.*)$", line).group(1)
502         except AttributeError:
503             str_title = ""
504         if str_title.find(u"図*") == 0:
505             str_title = str_title.replace(u"図*", "")
506         line = self.input_iter.next()
507         styles = []
508         if line.find("@") == 0:
509             styles = line.strip().replace("@", "").split(",")
510             line = self.input_iter.next()
511
512         imgname = ""
513         imgname_s = ""
514         hash = ""
515         hash_s = ""
516         match_o1 = re.search(ur"<([^,]*?)>", line)
517         match_o2 = re.search(ur"<(.*?),\s*(.*?)>", line)
518         if not match_o1 == None:
519             imgname = match_o1.group(1)
520             imgname = os.path.join(self._image_dir, imgname)
521             imgname_s = re.sub(r"(.[A-Za-z0-9_]+)$", r"_s\1", imgname)
522         elif not match_o2 == None:
523             imgname = match_o2.group(1)
524             imgname = os.path.join(self._image_dir, imgname)
525             imgname_s = match_o2.group(2)
526             imgname_s = os.path.join(self._image_dir, imgname_s)
527
528         try:
529             geom = self._get_png_geom(imgname_s)
530         except Exception, e:
531             sys.stderr.write(str(e) + "\nline: " + line.encode("utf-8"))
532             sys.exit(-1)
533
534         if geom:
535             w = geom[0]
536             h = geom[1]
537             print self._fig_start("", styles, width=w, height=h)
538         else:
539             print self._fig_start("", styles)
540         print self._anchored_fig(imgname, str_title, imgname_s)
541         print self._fig_end(str_title, styles);
542
543         dic = self.index("figs")
544         dic.append(imgname)
545         if imgname_s != "":
546             dic.append(imgname_s)
547
548     def _photo(self, line):
549         try:
550             str_title = re.search(ur"^☆(写真.*)$", line).group(1)
551         except AttributeError:
552             str_title = ""
553         if str_title.find(u"写真*") == 0:
554             str_title = str_title.replace(u"写真*", "")
555         line = self.input_iter.next()
556         styles = []
557         if line.find("@") == 0:
558             styles = line.strip().replace("@", "").split(",")
559             line = self.input_iter.next()
560
561         imgname = ""
562         imgname_s = ""
563         hash = ""
564         hash_s = ""
565         match_o1 = re.search(ur"<([^,]*?)>", line)
566         match_o2 = re.search(ur"<(.*?),\s*(.*?)>", line)
567         if not match_o1 == None:
568             imgname = match_o1.group(1)
569             imgname = os.path.join(self._image_dir, imgname)
570             imgname_s = re.sub(r"(.[A-Za-z0-9_]+)$", r"_s\1", imgname)
571         elif not match_o2 == None:
572             imgname = match_o2.group(1)
573             imgname = os.path.join(self._image_dir, imgname)
574             imgname_s = match_o2.group(2)
575             imgname_s = os.path.join(self._image_dir, imgname_s)
576
577         geom = self._get_png_geom(imgname_s)
578         if geom:
579             w = geom[0]
580             h = geom[1]
581             print self._fig_start("", styles, width=w, height=h)
582         else:
583             print self._fig_start("", styles)
584         print self._anchored_fig(imgname, str_title, imgname_s)
585         print self._fig_end(str_title, styles);
586
587         dic = self.index("figs")
588         dic.append(imgname)
589         if imgname_s != "":
590             dic.append(imgname_s)
591
592         
593     def _anchored_fig(self, file, alt, file_s=""):
594
595         if file_s == "":
596             file_s = re.sub(r"(.[A-Za-z0-9_]+)$", r"_s\1", file)
597
598         if not os.path.isfile(file_s):
599             file_s = file
600
601         alt = re.sub(r"""<[A-Za-z0-9!/]+.*?>""", "", alt)
602
603         ret = """<a href="%s">
604   <img src="%s" alt="%s">
605 </a>
606 """ % (file, file_s, alt)
607
608
609         return ret
610         
611
612     def _fig_release(self, line):
613         try:
614             str_title = re.search(ur"^☆(図.*)$", line).group(1)
615         except AttributeError:
616             str_title = ""
617         print self._fig_start()
618
619         line = self.input_iter.next()
620         imgname = ""
621         imgname_s = ""
622         hash = ""
623         hash_s = ""
624         match_o1 = re.search(ur"<([^,]*?)>", line)
625         match_o2 = re.search(ur"<(.*?),\s*(.*?)>", line)
626         if not match_o1 == None:
627             imgname = match_o1.group(1)
628             imgname_s = re.sub(r"(.[A-Za-z0-9_]+)$", r"_s\1", match_o1.group(1))
629         elif not match_o2 == None:
630             imgname = match_o1.group(1)
631             imgname_s = match_o1.group(2)
632
633
634         hash = self.hashlist.get(imgname, "")
635         hash_s = self.hashlist.get(imgname_s, "")
636         if hash_s == "":
637             hash_s = hash
638
639         print """<a href="/blob.pl?id=%s">
640      <slash type="image" id="%s" title="%s">
641      </a>
642      """ % (hash, hash_s, str_title)
643         
644
645         dic = self.index("figs")
646         dic.append(imgname)
647         if imgname_s != "":
648             dic.append(imgname_s)
649
650         print self._fig_end(str_title);
651
652
653     def _table_start(self, cap):
654         return """<div style="width:90%%; margin-left:auto;margin-right:auto;"><table align="center" border="1" class="table" width="100%%">
655     <caption><b>%s</b></caption>
656     """ % cap
657
658     def _table_end(self, footnote=""):
659         return "</table>\n%s</div>\n" % (footnote,)
660
661     def _table(self, line):
662         str_title = ""
663         self._table_buf1 = ""
664
665         try:
666             str_title = re.search(ur"^☆(表.*)$", line).group(1)
667             fig_name =  re.search(ur"^☆(表[0-9A-Z]*)", line).group(1)
668         except AttributeError:
669             str_title = ""
670             fig_name = ""
671         if str_title.find(u"表*") == 0:
672             str_title = str_title.replace(u"表*", "")
673
674         print self._table_start(str_title)
675         self._table_buf1 =  self._table_start(str_title)
676
677         num_row = 0
678         table_contents = []
679         footnote = ""
680         for line in self.input_iter:
681             line = line.strip(" \n")
682             line = self._default_markup_rule(line)
683             if re.search(ur"^\s*$", line):
684                 break
685             if re.search(ur"^※", line):
686                 footnote = re.search(ur"^(※.*)$", line).group(1)
687                 break
688             if re.search(ur"^〓", line):
689                 line = re.sub(ur"^〓", "", line)
690                 tag_mode = "th"
691             else:
692                 tag_mode = "td"
693             table_contents.append([])
694             num_col = 0
695             for item in line.split("\t"):
696                 if item == "":
697                     if num_col == 0:
698                         n = 1
699                         try:
700                             while table_contents[num_row-n][num_col]["item"] == "":
701                                 n += 1
702                             table_contents[num_row-n][num_col]["row"] += 1
703                         except IndexError:
704                             pass
705                     else:
706                         n = 1
707                         try:
708                             while table_contents[num_row][num_col-n]["item"] == "":
709                                 n += 1
710                             table_contents[num_row][num_col-n]["col"] += 1
711                         except IndexError:
712                             pass
713                 if item == u"↓":
714                     n = 1
715                     try:
716                         while table_contents[num_row-n][num_col]["item"] == "":
717                             n += 1
718                         table_contents[num_row-n][num_col]["row"] += 1
719                         item = ""
720                     except IndexError:
721                         pass
722
723                 if re.search(r'^".*"$', item):
724                     item = re.search(r'^"(.*)"$', item).group(1)
725                     table_contents[num_row].append({"tag":"th","item":item,"row":1,"col":1})
726                 else:
727                     table_contents[num_row].append({"tag":tag_mode,"item":item,"row":1,"col":1})
728                 num_col = num_col + 1
729             num_row = num_row + 1
730
731         for row_item in table_contents:
732             line = "<tr>"
733             for item in row_item:
734                 if item["item"] == "":
735                     continue
736                 line = line + "<" + item["tag"]
737                 if not item["row"] == 1:
738                     line = line + (' rowspan="%s"' % item["row"])
739                 if not item["col"] == 1:
740                     line = line + (' colspan="%s"' % item["col"])
741                 line = line +  ">"
742                 line = line + item["item"]
743                 line = line + "</" + item["tag"] + ">"
744             line = line + "</tr>\n"
745             print line,
746             self._table_buf1 = self._table_buf1 + line
747
748             # line = "<tr><th>" + re.sub(ur"^〓", "", line) + "</th></tr>"
749             # line = line.replace("\t", "</th><th>")
750             # print line
751             # else:
752             # line = "<tr><td>" + line + "</td></tr>"
753             # line = line.replace("\t", "</td><td>")
754             # print line
755
756         print self._table_end(footnote)
757         self._table_buf1 =  self._table_buf1 + self._table_end()
758         if self.index_haskey("tables"):
759             self.index("tables")[fig_name] = self._table_buf1
760         else:
761             self.index_add("tables", {fig_name:self._table_buf1})
762
763     def _call_tables(self, line):
764         try:
765             fig_name =  re.search(ur"^☆call_tables\((表[0-9A-Z]+)", line).group(1)
766         except AttributeError:
767             return
768         print self.index("tables")[fig_name]