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"[★*](コラム[0-9〜、]+)", ur"<b>\1</b>", line)
280 line = re.sub(ur"[★*]b\[(.*?)\]", ur"<b>\1</b>", line)
281 line = re.sub(ur"[★*]b\{(.*?)\}", ur"<b>\1</b>", line)
282 line = re.sub(ur"[★*]\[(\S*) (.*?)\]", r'<a href="\1">\2</a>', line)
283 line = re.sub(ur"[★*]\[(\S*)\]", r'<a href="\1">\1</a>', line)
286 if re.search(ur"^☆#", line):
292 def _ulist(self, line):
295 while re.search(ur"^・", line):
296 line = self._default_markup_rule(line)
297 print re.sub(ur"^・(.*)$", ur"<li>\1</li>", line.strip())
298 line = self.input_iter.next()
301 def _olist(self, line):
304 while re.search(ur"^[0-9]+\.", line):
305 line = self._default_markup_rule(line)
306 print re.sub(ur"^[0-9]+\.(.*)$", ur"<li>\1</li>", line.strip())
307 line = self.input_iter.next()
312 def _begin_column(self, line):
313 """Proccess column"""
315 str_title = re.search(ur"^☆begin-column:(.*)$", line).group(1)
316 except AttributeError:
319 html = """<table bgcolor="#DDDDDD" border="0" cellpadding="6" width="95%%">
321 <tr><td><span style="font-size: 85%%;">
325 def _end_column(self, line):
326 print """</span></td></tr>
330 def _list_start(self):
336 def _list(self, line):
338 str_title = re.search(ur"^☆(リスト.*)$", line).group(1)
339 except AttributeError:
341 print "<p><b>%s</b></p>" % (str_title)
342 print self._list_start()
344 for line in self.input_iter:
345 line = line.strip("\n\r")
346 line = self._escape(line)
347 line = line = re.sub(ur"[★*]b\[(.*?)]", ur"<b>\1</b>", line)
348 line = line = re.sub(ur"[★*]b{(.*?)}", ur"<b>\1</b>", line)
349 if re.search(ur"""^☆\+---""", line):
352 print self._list_end()
354 def _code(self, line):
355 print self._list_start()
357 for line in self.input_iter:
358 line = self._escape(line)
359 line = line = re.sub(ur"[★*]b\[(.*?)]", ur"<b>\1</b>", line)
360 line = line = re.sub(ur"[★*]b{(.*?)}", ur"<b>\1</b>", line)
362 if re.search(ur"^☆\+---$", line):
365 print self._list_end()
367 def _inline(self, line):
368 for line in self.input_iter:
370 if re.search(ur"^☆}}}", line):
374 def _comment(self, line):
375 for line in self.input_iter:
377 if re.search(ur"^☆}}}", line):
380 def _space(self, line):
383 def _flow(self, line):
384 down_arrow = "http://static.sourceforge.jp/crystal/22x22/actions/1downarrow.png"
385 flow_header = """<div style="text-align:center; border: 1px solid; background-color:#EFF2F0; width:90%; margin: 0 auto 1em;">
387 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;">
390 flow_footer = """</div>
398 arrow = '<div style="margin:1em auto;"><img src="%s"></div>\n' % (down_arrow,)
400 rex_title = re.compile(ur"^☆flow\s+(.*)$")
401 if rex_title.search(line):
402 title = rex_title.search(line).group(1)
406 rex_file = re.compile(ur"^([^:]*):(.*)$")
408 for line in self.input_iter:
409 if re.search(r"^\s*$", line):
411 match = rex_file.search(line)
413 file = os.path.join(self._image_dir, match.group(1))
414 cap = self._default_markup_rule(match.group(2))
417 fig = self._anchored_fig(file, cap)
418 outputs.append(flow_item % (fig, cap))
421 print flow_title % (title,)
422 print arrow.join(outputs)
426 def _fig_start(self, cap="", styles=[], width=0, height=0):
427 params = dict(style="", tablewidth="")
429 params["style"] = "width:%d;" % (width,)
430 params["tablewidth"] = 'width="%d"' % (width,)
432 if "lfloat" in styles:
433 return """<table %(tablewidth)s align="center" border="0" cellpadding="0" cellspacing="0" style="float:left; padding-left: 0.5em; %(style)s">
434 <tr> <td valign="top" align="center">
436 elif "left" in styles:
437 return """<table %(tablewidth)s border="0" cellpadding="0" cellspacing="0" style="padding-left: 0.5em; %(style)s">
438 <tr> <td valign="top" align="center">
441 return """<table %(tablewidth)s align="center" border="0" cellpadding="0" cellspacing="0">
442 <tr> <td valign="top" align="center">
445 def _fig_end(self, cap="", styles=[]):
446 return """</td> </tr>
447 <tr> <td><span style="font-size: 80%%; font-weight: bold;">
453 def _get_png_geom(self, filepath):
454 desc = deterfile.file(filepath)
456 m = re.match(r"([0-9]+)\s*x\s*([0-9]+)", desc[1])
459 raise Exception("deterfile error: %s, file: %s . " % (err,filepath))
463 return (int(w), int(h))
467 def _fig(self, line):
469 str_title = re.search(ur"^☆(図.*)$", line).group(1)
470 except AttributeError:
472 if str_title.find(u"図*") == 0:
473 str_title = str_title.replace(u"図*", "")
474 line = self.input_iter.next()
476 if line.find("@") == 0:
477 styles = line.strip().replace("@", "").split(",")
478 line = self.input_iter.next()
484 match_o1 = re.search(ur"<([^,]*?)>", line)
485 match_o2 = re.search(ur"<(.*?),\s*(.*?)>", line)
486 if not match_o1 == None:
487 imgname = match_o1.group(1)
488 imgname = os.path.join(self._image_dir, imgname)
489 imgname_s = re.sub(r"(.[A-Za-z0-9_]+)$", r"_s\1", imgname)
490 elif not match_o2 == None:
491 imgname = match_o2.group(1)
492 imgname = os.path.join(self._image_dir, imgname)
493 imgname_s = match_o2.group(2)
494 imgname_s = os.path.join(self._image_dir, imgname_s)
497 geom = self._get_png_geom(imgname_s)
499 sys.stderr.write(str(e) + "\nline: " + line.encode("utf-8"))
505 print self._fig_start("", styles, width=w, height=h)
507 print self._fig_start("", styles)
508 print self._anchored_fig(imgname, str_title, imgname_s)
509 print self._fig_end(str_title, styles);
511 dic = self.index("figs")
514 dic.append(imgname_s)
516 def _photo(self, line):
518 str_title = re.search(ur"^☆(写真.*)$", line).group(1)
519 except AttributeError:
521 if str_title.find(u"写真*") == 0:
522 str_title = str_title.replace(u"写真*", "")
523 line = self.input_iter.next()
525 if line.find("@") == 0:
526 styles = line.strip().replace("@", "").split(",")
527 line = self.input_iter.next()
533 match_o1 = re.search(ur"<([^,]*?)>", line)
534 match_o2 = re.search(ur"<(.*?),\s*(.*?)>", line)
535 if not match_o1 == None:
536 imgname = match_o1.group(1)
537 imgname = os.path.join(self._image_dir, imgname)
538 imgname_s = re.sub(r"(.[A-Za-z0-9_]+)$", r"_s\1", imgname)
539 elif not match_o2 == None:
540 imgname = match_o2.group(1)
541 imgname = os.path.join(self._image_dir, imgname)
542 imgname_s = match_o2.group(2)
543 imgname_s = os.path.join(self._image_dir, imgname_s)
545 geom = self._get_png_geom(imgname_s)
549 print self._fig_start("", styles, width=w, height=h)
551 print self._fig_start("", styles)
552 print self._anchored_fig(imgname, str_title, imgname_s)
553 print self._fig_end(str_title, styles);
555 dic = self.index("figs")
558 dic.append(imgname_s)
561 def _anchored_fig(self, file, alt, file_s=""):
564 file_s = re.sub(r"(.[A-Za-z0-9_]+)$", r"_s\1", file)
566 if not os.path.isfile(file_s):
569 alt = re.sub(r"""<[A-Za-z0-9!/]+.*?>""", "", alt)
571 ret = """<a href="%s">
572 <img src="%s" alt="%s">
574 """ % (file, file_s, alt)
580 def _fig_release(self, line):
582 str_title = re.search(ur"^☆(図.*)$", line).group(1)
583 except AttributeError:
585 print self._fig_start()
587 line = self.input_iter.next()
592 match_o1 = re.search(ur"<([^,]*?)>", line)
593 match_o2 = re.search(ur"<(.*?),\s*(.*?)>", line)
594 if not match_o1 == None:
595 imgname = match_o1.group(1)
596 imgname_s = re.sub(r"(.[A-Za-z0-9_]+)$", r"_s\1", match_o1.group(1))
597 elif not match_o2 == None:
598 imgname = match_o1.group(1)
599 imgname_s = match_o1.group(2)
602 hash = self.hashlist.get(imgname, "")
603 hash_s = self.hashlist.get(imgname_s, "")
607 print """<a href="/blob.pl?id=%s">
608 <slash type="image" id="%s" title="%s">
610 """ % (hash, hash_s, str_title)
613 dic = self.index("figs")
616 dic.append(imgname_s)
618 print self._fig_end(str_title);
621 def _table_start(self, cap):
622 return """<div style="width:90%%; margin-left:auto;margin-right:auto;"><table align="center" border="1" class="table" width="100%%">
623 <caption><b>%s</b></caption>
626 def _table_end(self, footnote=""):
627 return "</table>\n%s</div>\n" % (footnote,)
629 def _table(self, line):
631 self._table_buf1 = ""
634 str_title = re.search(ur"^☆(表.*)$", line).group(1)
635 fig_name = re.search(ur"^☆(表[0-9A-Z]*)", line).group(1)
636 except AttributeError:
639 if str_title.find(u"表*") == 0:
640 str_title = str_title.replace(u"表*", "")
642 print self._table_start(str_title)
643 self._table_buf1 = self._table_start(str_title)
648 for line in self.input_iter:
649 line = line.strip(" \n")
650 line = self._default_markup_rule(line)
651 if re.search(ur"^\s*$", line):
653 if re.search(ur"^※", line):
654 footnote = re.search(ur"^(※.*)$", line).group(1)
656 if re.search(ur"^〓", line):
657 line = re.sub(ur"^〓", "", line)
661 table_contents.append([])
663 for item in line.split("\t"):
668 while table_contents[num_row-n][num_col]["item"] == "":
670 table_contents[num_row-n][num_col]["row"] += 1
676 while table_contents[num_row][num_col-n]["item"] == "":
678 table_contents[num_row][num_col-n]["col"] += 1
684 while table_contents[num_row-n][num_col]["item"] == "":
686 table_contents[num_row-n][num_col]["row"] += 1
691 if re.search(r'^".*"$', item):
692 item = re.search(r'^"(.*)"$', item).group(1)
693 table_contents[num_row].append({"tag":"th","item":item,"row":1,"col":1})
695 table_contents[num_row].append({"tag":tag_mode,"item":item,"row":1,"col":1})
696 num_col = num_col + 1
697 num_row = num_row + 1
699 for row_item in table_contents:
701 for item in row_item:
702 if item["item"] == "":
704 line = line + "<" + item["tag"]
705 if not item["row"] == 1:
706 line = line + (' rowspan="%s"' % item["row"])
707 if not item["col"] == 1:
708 line = line + (' colspan="%s"' % item["col"])
710 line = line + item["item"]
711 line = line + "</" + item["tag"] + ">"
712 line = line + "</tr>\n"
714 self._table_buf1 = self._table_buf1 + line
716 # line = "<tr><th>" + re.sub(ur"^〓", "", line) + "</th></tr>"
717 # line = line.replace("\t", "</th><th>")
720 # line = "<tr><td>" + line + "</td></tr>"
721 # line = line.replace("\t", "</td><td>")
724 print self._table_end(footnote)
725 self._table_buf1 = self._table_buf1 + self._table_end()
726 if self.index_haskey("tables"):
727 self.index("tables")[fig_name] = self._table_buf1
729 self.index_add("tables", {fig_name:self._table_buf1})
731 def _call_tables(self, line):
733 fig_name = re.search(ur"^☆call_tables\((表[0-9A-Z]+)", line).group(1)
734 except AttributeError:
736 print self.index("tables")[fig_name]