OSDN Git Service

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