2 # -*- coding: utf-8 -*-
13 #sys.stdin = codecs.getreader('utf_8')(sys.stdin)
14 #sys.stdout = codecs.getwriter('utf_8')(sys.stdout)
16 alist = ["a", "a:href", "a:name", "b", "br" ]
19 tag_filter = HTMLTagFilter.HTMLTagFilter(HTMLTagFilter.DENY_ALLOW, alist, dlist)
20 path_to_index = "./_markup_index"
22 class _InputStream(object):
23 """InputStream base class."""
32 """function for iterator"""
36 class Markupper(object):
40 self._input_iter = None
45 def index_add(self, key, val):
47 Add key and value to index.
55 self._index[key] = val
61 return self._index[key]
63 def index_haskey(self, key):
64 return self._index.has_key(key)
66 def markup(self, input_iter, release="0"):
70 @param input_iter: iterator to use as input
71 @type input_iter: iterator
73 self.input_iter = input_iter
74 self._page_counter = 1
75 self._image_border = 0
76 # alist = ["a", "a:href", "a:name", "b", "br" ]
78 # tag_filter = HTMLTagFilter.HTMLTagFilter(HTMLTagFilter.DENY_ALLOW, alist, dlist)
79 self._release = release
81 self.index_add("figs", [])
84 for line in self.input_iter:
85 # line = self._default_markup_rule(line)
87 if re.search(ur"^☆{{{$", line):
90 elif re.search(ur"^☆image_dir:", line):
91 self._image_dir = re.search(ur"^☆image_dir:\s*(.*)$", line).group(1)
93 elif re.search(ur"^☆image_border:\s(on|On|ON)", line):
94 self._image_border = 1
96 elif re.search(ur"^☆comment\s{{{$", line):
99 elif re.search(ur"^☆\*", line):
100 self._anchor = re.sub(ur"^☆\*", "", line).strip()
102 elif re.search(ur"^☆clear\s+", line):
105 elif re.search(ur"^・", line):
108 elif re.search(ur"^[0-9]\.", line):
111 elif re.search(ur"^☆begin-column:", line):
112 self._begin_column(line)
114 elif re.search(ur"^☆end-column", line):
115 self._end_column(line)
117 elif re.search(ur"^☆space", line):
120 elif re.search(ur"^☆call_tables", line):
121 self._call_tables(line)
123 elif re.search(ur"^●", line):
126 elif re.search(ur"^○", line):
129 elif re.search(ur"^☆----", line):
132 elif re.search(ur"^☆\+---", line):
135 elif re.search(ur"^☆表", line):
138 elif re.search(ur"^☆図", line):
141 elif re.search(ur"^☆写真", line):
144 elif re.search(ur"^☆リスト", line):
147 elif re.search(ur"^☆flow", line):
151 if re.search(ur"^ ", line):
152 self._paragraph(line)
156 if re.search(r"^\s*$", line):
164 def _clear(self, line):
165 print """<div style="clear:left;"> </div>
168 def _head_l(self, line):
170 if re.search(ur"\*{[a-zA-Z0-9_]*}\s*$", line):
171 self._anchor = re.search(ur"\*\{([a-zA-Z0-9_]*)\}\s*$", line).group(1)
172 line = re.sub(ur"\s*\*\{[a-zA-Z0-9_]*\}\s*$", "", line)
174 line = self._default_markup_rule(line)
175 if self._anchor != "":
176 line = re.sub(ur"^●(.*)$", ur'<div id="%s"><h3>\1</h3></div>' % self._anchor, line)
179 line = re.sub(ur"^●(.*)$", ur"<h3>\1</h3>", line)
182 def _head_m(self, line):
184 if re.search(ur"\*{[a-zA-Z0-9_]*}\s*$", line):
185 self._anchor = re.search(ur"\*\{([a-zA-Z0-9_]*)\}\s*$", line).group(1)
186 line = re.sub(ur"\s*\*\{[a-zA-Z0-9_]*\}\s*$", "", line)
188 line = self._default_markup_rule(line)
189 if self._anchor != "":
190 line = re.sub(ur"^○(.*)$", ur'<div id="%s"><h4>\1</h4></div>' % self._anchor, line)
193 line = re.sub(ur"^○(.*)$", ur"<h4>\1</h4>", line)
196 def _paragraph(self, line):
197 line = self._default_markup_rule(line)
198 line = "<p>" + line + "</p>"
201 def _newpage(self, line):
202 line = re.sub(ur"☆----.*-{0,1}", u"<hr>", line)
205 def load_index(self, path_to_index):
209 @param path_to_index: index db's path
210 @type path_to_index: string
214 index_file = open(path_to_index, "r")
215 self._index_past = pickle.load(index_file)
218 sys.stderr.write("warn: cannot read index file,\n")
220 def save_index(self, path_to_index):
224 @param path_to_index: index db's path
225 @type path_to_index: string
229 index_file = open(path_to_index, "w")
230 pickle.dump(self._index, index_file)
233 sys.stderr.write("warn: cannot write index file,\n")
235 def make_hashlist(self, path_to_hashfile):
239 @param path_to_hashfile: hashfile's path
240 @type path_to_hashfile: string
243 file_img_hash = open(path_to_hashfile, "r")
245 sys.stderr.write("cannot open file: %s" % path_img_hash)
249 for line in file_img_hash:
250 splited = line.strip().split("\t", 2)
251 # hashlist's format: <hash> \t <filename>
252 self.hashlist[splited[1]] = splited[0]
254 def _call_tagles(self):
257 def _escape(self, line):
258 line = re.sub(ur"&", ur"&", line)
259 line = re.sub(ur"<", ur"<", line)
260 line = re.sub(ur">", ur">", line)
263 def _default_markup_rule(self, line):
265 apply default markup rules.
267 @param line: string to apply markup
270 line = self._escape(line)
273 # line = tag_filter.apply(line)
275 line = re.sub(ur"[★*](表[0-9〜、]+)", ur"<b>\1</b>", line)
276 line = re.sub(ur"[★*](図[0-9〜、]+)", ur"<b>\1</b>", line)
277 line = re.sub(ur"[★*](写真[0-9〜、]+)", ur"<b>\1</b>", line)
278 line = re.sub(ur"[★*](リスト[0-9〜、]+)", ur"<b>\1</b>", line)
279 line = re.sub(ur"[★*]b\[(.*?)\]", ur"<b>\1</b>", line)
280 line = re.sub(ur"[★*]b\{(.*?)\}", ur"<b>\1</b>", line)
281 line = re.sub(ur"[★*]\[(\S*) (.*?)\]", r'<a href="\1">\2</a>', line)
282 line = re.sub(ur"[★*]\[(\S*)\]", r'<a href="\1">\1</a>', line)
285 if re.search(ur"^☆#", line):
291 def _ulist(self, line):
294 while re.search(ur"^・", line):
295 line = self._default_markup_rule(line)
296 print re.sub(ur"^・(.*)$", ur"<li>\1</li>", line.strip())
297 line = self.input_iter.next()
300 def _olist(self, line):
303 while re.search(ur"^[0-9]+\.", line):
304 line = self._default_markup_rule(line)
305 print re.sub(ur"^[0-9]+\.(.*)$", ur"<li>\1</li>", line.strip())
306 line = self.input_iter.next()
311 def _begin_column(self, line):
312 """Proccess column"""
314 str_title = re.search(ur"^☆begin-column:(.*)$", line).group(1)
315 except AttributeError:
318 html = """<table bgcolor="#DDDDDD" border="0" cellpadding="6" width="95%%">
320 <tr><td><span style="font-size: 85%%;">
324 def _end_column(self, line):
325 print """</span></td></tr>
329 def _list_start(self):
335 def _list(self, line):
337 str_title = re.search(ur"^☆(リスト.*)$", line).group(1)
338 except AttributeError:
340 print "<p><b>%s</b></p>" % (str_title)
341 print self._list_start()
343 for line in self.input_iter:
344 line = line.strip("\n\r")
345 line = self._escape(line)
346 line = line = re.sub(ur"[★*]b\[(.*?)]", ur"<b>\1</b>", line)
347 line = line = re.sub(ur"[★*]b{(.*?)}", ur"<b>\1</b>", line)
348 if re.search(ur"""^☆\+---""", line):
351 print self._list_end()
353 def _code(self, line):
354 print self._list_start()
356 for line in self.input_iter:
357 line = self._escape(line)
358 line = line = re.sub(ur"[★*]b\[(.*?)]", ur"<b>\1</b>", line)
359 line = line = re.sub(ur"[★*]b{(.*?)}", ur"<b>\1</b>", line)
361 if re.search(ur"^☆\+---$", line):
364 print self._list_end()
366 def _inline(self, line):
367 for line in self.input_iter:
368 # line = line.strip()
369 if re.search(ur"^☆}}}", line):
373 def _comment(self, line):
374 for line in self.input_iter:
376 if re.search(ur"^☆}}}", line):
379 def _space(self, line):
382 def _flow(self, line):
383 down_arrow = "http://static.sourceforge.jp/crystal/22x22/actions/1downarrow.png"
384 flow_header = """<div style="text-align:center; border: 1px solid; background-color:#EFF2F0; width:90%; margin: 0 auto 1em;">
386 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;">
389 flow_footer = """</div>
397 arrow = '<div style="margin:1em auto;"><img src="%s"></div>\n' % (down_arrow,)
399 rex_title = re.compile(ur"^☆flow\s+(.*)$")
400 if rex_title.search(line):
401 title = rex_title.search(line).group(1)
405 rex_file = re.compile(ur"^([^:]*):(.*)$")
407 for line in self.input_iter:
408 if re.search(r"^\s*$", line):
410 match = rex_file.search(line)
412 file = os.path.join(self._image_dir, match.group(1))
413 cap = self._default_markup_rule(match.group(2))
416 fig = self._anchored_fig(file, cap)
417 outputs.append(flow_item % (fig, cap))
420 print flow_title % (title,)
421 print arrow.join(outputs)
425 def _fig_start(self, cap="", styles=[], width=0, height=0):
426 params = dict(style="", tablewidth="")
428 params["style"] = "width:%d;" % (width,)
429 params["tablewidth"] = 'width="%d"' % (width,)
431 if "lfloat" in styles:
432 return """<table %(tablewidth)s align="center" border="0" cellpadding="0" cellspacing="0" style="float:left; padding-left: 0.5em; %(style)s">
433 <tr> <td valign="top" align="center">
435 elif "left" in styles:
436 return """<table %(tablewidth)s border="0" cellpadding="0" cellspacing="0" style="padding-left: 0.5em; %(style)s">
437 <tr> <td valign="top" align="center">
440 return """<table %(tablewidth)s align="center" border="0" cellpadding="0" cellspacing="0">
441 <tr> <td valign="top" align="center">
444 def _fig_end(self, cap="", styles=[]):
445 return """</td> </tr>
446 <tr> <td><span style="font-size: 80%%; font-weight: bold;">
452 def _get_png_geom(self, filepath):
453 desc = deterfile.file(filepath)
455 m = re.match(r"([0-9]+)\s*x\s*([0-9]+)", desc[1])
458 raise Exception("deterfile error: %s, file: %s" % (err,filepath))
462 return (int(w), int(h))
466 def _fig(self, line):
468 str_title = re.search(ur"^☆(図.*)$", line).group(1)
469 except AttributeError:
471 if str_title.find(u"図*") == 0:
472 str_title = str_title.replace(u"図*", "")
473 line = self.input_iter.next()
475 if line.find("@") == 0:
476 styles = line.strip().replace("@", "").split(",")
477 line = self.input_iter.next()
483 match_o1 = re.search(ur"<([^,]*?)>", line)
484 match_o2 = re.search(ur"<(.*?),\s*(.*?)>", line)
485 if not match_o1 == None:
486 imgname = match_o1.group(1)
487 imgname = os.path.join(self._image_dir, imgname)
488 imgname_s = re.sub(r"(.[A-Za-z0-9_]+)$", r"_s\1", imgname)
489 elif not match_o2 == None:
490 imgname = match_o2.group(1)
491 imgname = os.path.join(self._image_dir, imgname)
492 imgname_s = match_o2.group(2)
493 imgname_s = os.path.join(self._image_dir, imgname_s)
495 geom = self._get_png_geom(imgname_s)
499 print self._fig_start("", styles, width=w, height=h)
501 print self._fig_start("", styles)
502 print self._anchored_fig(imgname, str_title, imgname_s)
503 print self._fig_end(str_title, styles);
505 dic = self.index("figs")
508 dic.append(imgname_s)
510 def _photo(self, line):
512 str_title = re.search(ur"^☆(写真.*)$", line).group(1)
513 except AttributeError:
515 if str_title.find(u"写真*") == 0:
516 str_title = str_title.replace(u"写真*", "")
517 line = self.input_iter.next()
519 if line.find("@") == 0:
520 styles = line.strip().replace("@", "").split(",")
521 line = self.input_iter.next()
527 match_o1 = re.search(ur"<([^,]*?)>", line)
528 match_o2 = re.search(ur"<(.*?),\s*(.*?)>", line)
529 if not match_o1 == None:
530 imgname = match_o1.group(1)
531 imgname = os.path.join(self._image_dir, imgname)
532 imgname_s = re.sub(r"(.[A-Za-z0-9_]+)$", r"_s\1", imgname)
533 elif not match_o2 == None:
534 imgname = match_o2.group(1)
535 imgname = os.path.join(self._image_dir, imgname)
536 imgname_s = match_o2.group(2)
537 imgname_s = os.path.join(self._image_dir, imgname_s)
539 geom = self._get_png_geom(imgname_s)
543 print self._fig_start("", styles, width=w, height=h)
545 print self._fig_start("", styles)
546 print self._anchored_fig(imgname, str_title, imgname_s)
547 print self._fig_end(str_title, styles);
549 dic = self.index("figs")
552 dic.append(imgname_s)
555 def _anchored_fig(self, file, alt, file_s=""):
558 file_s = re.sub(r"(.[A-Za-z0-9_]+)$", r"_s\1", file)
560 if not os.path.isfile(file_s):
563 alt = re.sub(r"""<[A-Za-z0-9!/]+.*?>""", "", alt)
565 ret = """<a href="%s">
566 <img src="%s" alt="%s">
568 """ % (file, file_s, alt)
574 def _fig_release(self, line):
576 str_title = re.search(ur"^☆(図.*)$", line).group(1)
577 except AttributeError:
579 print self._fig_start()
581 line = self.input_iter.next()
586 match_o1 = re.search(ur"<([^,]*?)>", line)
587 match_o2 = re.search(ur"<(.*?),\s*(.*?)>", line)
588 if not match_o1 == None:
589 imgname = match_o1.group(1)
590 imgname_s = re.sub(r"(.[A-Za-z0-9_]+)$", r"_s\1", match_o1.group(1))
591 elif not match_o2 == None:
592 imgname = match_o1.group(1)
593 imgname_s = match_o1.group(2)
596 hash = self.hashlist.get(imgname, "")
597 hash_s = self.hashlist.get(imgname_s, "")
601 print """<a href="/blob.pl?id=%s">
602 <slash type="image" id="%s" title="%s">
604 """ % (hash, hash_s, str_title)
607 dic = self.index("figs")
610 dic.append(imgname_s)
612 print self._fig_end(str_title);
615 def _table_start(self, cap):
616 return """<div style="width:90%%; margin-left:auto;margin-right:auto;"><table align="center" border="1" class="table" width="100%%">
617 <caption><b>%s</b></caption>
620 def _table_end(self, footnote=""):
621 return "</table>\n%s</div>\n" % (footnote,)
623 def _table(self, line):
625 self._table_buf1 = ""
628 str_title = re.search(ur"^☆(表.*)$", line).group(1)
629 fig_name = re.search(ur"^☆(表[0-9A-Z]*)", line).group(1)
630 except AttributeError:
633 if str_title.find(u"表*") == 0:
634 str_title = str_title.replace(u"表*", "")
636 print self._table_start(str_title)
637 self._table_buf1 = self._table_start(str_title)
642 for line in self.input_iter:
643 line = line.strip(" \n")
644 line = self._default_markup_rule(line)
645 if re.search(ur"^\s*$", line):
647 if re.search(ur"^※", line):
648 footnote = re.search(ur"^(※.*)$", line).group(1)
650 if re.search(ur"^〓", line):
651 line = re.sub(ur"^〓", "", line)
655 table_contents.append([])
657 for item in line.split("\t"):
662 while table_contents[num_row-n][num_col]["item"] == "":
664 table_contents[num_row-n][num_col]["row"] += 1
670 while table_contents[num_row][num_col-n]["item"] == "":
672 table_contents[num_row][num_col-n]["col"] += 1
678 while table_contents[num_row-n][num_col]["item"] == "":
680 table_contents[num_row-n][num_col]["row"] += 1
685 if re.search(r'^".*"$', item):
686 item = re.search(r'^"(.*)"$', item).group(1)
687 table_contents[num_row].append({"tag":"th","item":item,"row":1,"col":1})
689 table_contents[num_row].append({"tag":tag_mode,"item":item,"row":1,"col":1})
690 num_col = num_col + 1
691 num_row = num_row + 1
693 for row_item in table_contents:
695 for item in row_item:
696 if item["item"] == "":
698 line = line + "<" + item["tag"]
699 if not item["row"] == 1:
700 line = line + (' rowspan="%s"' % item["row"])
701 if not item["col"] == 1:
702 line = line + (' colspan="%s"' % item["col"])
704 line = line + item["item"]
705 line = line + "</" + item["tag"] + ">"
706 line = line + "</tr>\n"
708 self._table_buf1 = self._table_buf1 + line
710 # line = "<tr><th>" + re.sub(ur"^〓", "", line) + "</th></tr>"
711 # line = line.replace("\t", "</th><th>")
714 # line = "<tr><td>" + line + "</td></tr>"
715 # line = line.replace("\t", "</td><td>")
718 print self._table_end(footnote)
719 self._table_buf1 = self._table_buf1 + self._table_end()
720 if self.index_haskey("tables"):
721 self.index("tables")[fig_name] = self._table_buf1
723 self.index_add("tables", {fig_name:self._table_buf1})
725 def _call_tables(self, line):
727 fig_name = re.search(ur"^☆call_tables\((表[0-9A-Z]+)", line).group(1)
728 except AttributeError:
730 print self.index("tables")[fig_name]