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 # alist = ["a", "a:href", "a:name", "b", "br" ]
77 # tag_filter = HTMLTagFilter.HTMLTagFilter(HTMLTagFilter.DENY_ALLOW, alist, dlist)
78 self._release = release
80 self.index_add("figs", [])
83 for line in self.input_iter:
84 # line = self._default_markup_rule(line)
86 if re.search(ur"^☆{{{$", line):
89 elif re.search(ur"^☆image_dir:", line):
90 self._image_dir = re.search(ur"^☆image_dir:\s*(.*)$", line).group(1)
92 elif re.search(ur"^☆comment\s{{{$", line):
95 elif re.search(ur"^☆\*", line):
96 self._anchor = re.sub(ur"^☆\*", "", line).strip()
98 elif re.search(ur"^☆clear\s+", line):
101 elif re.search(ur"^・", line):
104 elif re.search(ur"^[0-9]\.", line):
107 elif re.search(ur"^☆begin-column:", line):
108 self._begin_column(line)
110 elif re.search(ur"^☆end-column", line):
111 self._end_column(line)
113 elif re.search(ur"^☆space", line):
116 elif re.search(ur"^☆call_tables", line):
117 self._call_tables(line)
119 elif re.search(ur"^●", line):
122 elif re.search(ur"^○", line):
125 elif re.search(ur"^☆----", line):
128 elif re.search(ur"^☆\+---", line):
131 elif re.search(ur"^☆表", line):
134 elif re.search(ur"^☆図", line):
137 elif re.search(ur"^☆リスト", line):
140 elif re.search(ur"^☆flow", line):
144 if re.search(ur"^ ", line):
145 self._paragraph(line)
149 if re.search(r"^\s*$", line):
157 def _clear(self, line):
158 print """<div style="clear:left;"> </div>
161 def _head_l(self, line):
163 if re.search(ur"\*{[a-zA-Z0-9_]*}\s*$", line):
164 self._anchor = re.search(ur"\*\{([a-zA-Z0-9_]*)\}\s*$", line).group(1)
165 line = re.sub(ur"\s*\*\{[a-zA-Z0-9_]*\}\s*$", "", line)
167 line = self._default_markup_rule(line)
168 if self._anchor != "":
169 line = re.sub(ur"^●(.*)$", ur'<div id="%s"><h3>\1</h3></div>' % self._anchor, line)
172 line = re.sub(ur"^●(.*)$", ur"<h3>\1</h3>", line)
175 def _head_m(self, line):
177 if re.search(ur"\*{[a-zA-Z0-9_]*}\s*$", line):
178 self._anchor = re.search(ur"\*\{([a-zA-Z0-9_]*)\}\s*$", line).group(1)
179 line = re.sub(ur"\s*\*\{[a-zA-Z0-9_]*\}\s*$", "", line)
181 line = self._default_markup_rule(line)
182 if self._anchor != "":
183 line = re.sub(ur"^○(.*)$", ur'<div id="%s"><h4>\1</h4></div>' % self._anchor, line)
186 line = re.sub(ur"^○(.*)$", ur"<h4>\1</h4>", line)
189 def _paragraph(self, line):
190 line = self._default_markup_rule(line)
191 line = "<p>" + line + "</p>"
194 def _newpage(self, line):
195 line = re.sub(ur"☆----.*-{0,1}", u"<hr>", line)
198 def load_index(self, path_to_index):
202 @param path_to_index: index db's path
203 @type path_to_index: string
207 index_file = open(path_to_index, "r")
208 self._index_past = pickle.load(index_file)
211 sys.stderr.write("warn: cannot read index file,\n")
213 def save_index(self, path_to_index):
217 @param path_to_index: index db's path
218 @type path_to_index: string
222 index_file = open(path_to_index, "w")
223 pickle.dump(self._index, index_file)
226 sys.stderr.write("warn: cannot write index file,\n")
228 def make_hashlist(self, path_to_hashfile):
232 @param path_to_hashfile: hashfile's path
233 @type path_to_hashfile: string
236 file_img_hash = open(path_to_hashfile, "r")
238 sys.stderr.write("cannot open file: %s" % path_img_hash)
242 for line in file_img_hash:
243 splited = line.strip().split("\t", 2)
244 # hashlist's format: <hash> \t <filename>
245 self.hashlist[splited[1]] = splited[0]
247 def _call_tagles(self):
250 def _escape(self, line):
251 line = re.sub(ur"&", ur"&", line)
252 line = re.sub(ur"<", ur"<", line)
253 line = re.sub(ur">", ur">", line)
256 def _default_markup_rule(self, line):
258 apply default markup rules.
260 @param line: string to apply markup
263 line = self._escape(line)
266 # line = tag_filter.apply(line)
268 line = re.sub(ur"[★*](表[0-9~、]+)", ur"<b>\1</b>", line)
269 line = re.sub(ur"[★*](図[0-9~、]+)", ur"<b>\1</b>", line)
270 line = re.sub(ur"[★*](リスト[0-9~、]+)", ur"<b>\1</b>", line)
271 line = re.sub(ur"[★*]b\[(.*?)\]", ur"<b>\1</b>", line)
272 line = re.sub(ur"[★*]b\{(.*?)\}", ur"<b>\1</b>", line)
273 line = re.sub(ur"[★*]\[(\S*) (.*?)\]", r'<a href="\1">\2</a>', line)
274 line = re.sub(ur"[★*]\[(\S*)\]", r'<a href="\1">\1</a>', line)
277 if re.search(ur"^☆#", line):
283 def _ulist(self, line):
286 while re.search(ur"^・", line):
287 line = self._default_markup_rule(line)
288 print re.sub(ur"^・(.*)$", ur"<li>\1</li>", line.strip())
289 line = self.input_iter.next()
292 def _olist(self, line):
295 while re.search(ur"^[0-9]+\.", line):
296 line = self._default_markup_rule(line)
297 print re.sub(ur"^[0-9]+\.(.*)$", ur"<li>\1</li>", line.strip())
298 line = self.input_iter.next()
303 def _begin_column(self, line):
304 """Proccess column"""
306 str_title = re.search(ur"^☆begin-column:(.*)$", line).group(1)
307 except AttributeError:
310 html = """<table bgcolor="#DDDDDD" border="0" cellpadding="6" width="95%%">
312 <tr><td><span style="font-size: 85%%;">
316 def _end_column(self, line):
317 print """</span></td></tr>
321 def _list_start(self):
327 def _list(self, line):
329 str_title = re.search(ur"^☆(リスト.*)$", line).group(1)
330 except AttributeError:
332 print "<p><b>%s</b></p>" % (str_title)
333 print self._list_start()
335 for line in self.input_iter:
336 line = line.strip("\n\r")
337 line = self._escape(line)
338 if re.search(ur"""^☆\+---""", line):
341 print self._list_end()
343 def _code(self, line):
344 print self._list_start()
346 for line in self.input_iter:
347 line = self._escape(line)
348 line = line = re.sub(ur"[★*]b\[(.*?)]", ur"<b>\1</b>", line)
349 line = line = re.sub(ur"[★*]b{(.*?)}", ur"<b>\1</b>", line)
351 if re.search(ur"^☆\+---$", line):
354 print self._list_end()
356 def _inline(self, line):
357 for line in self.input_iter:
358 # line = line.strip()
359 if re.search(ur"^☆}}}", line):
363 def _comment(self, line):
364 for line in self.input_iter:
366 if re.search(ur"^☆}}}", line):
369 def _space(self, line):
372 def _flow(self, line):
373 down_arrow = "http://static.sourceforge.jp/crystal/22x22/actions/1downarrow.png"
374 flow_header = """<div style="text-align:center; border: 1px solid; background-color:#EFF2F0; width:90%; margin: 0 auto 1em;">
376 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;">
379 flow_footer = """</div>
387 arrow = '<div style="margin:1em auto;"><img src="%s"></div>\n' % (down_arrow,)
389 rex_title = re.compile(ur"^☆flow\s+(.*)$")
390 if rex_title.search(line):
391 title = rex_title.search(line).group(1)
395 rex_file = re.compile(ur"^([^:]*):(.*)$")
397 for line in self.input_iter:
398 if re.search(r"^\s*$", line):
400 match = rex_file.search(line)
402 file = os.path.join(self._image_dir, match.group(1))
403 cap = self._default_markup_rule(match.group(2))
406 fig = self._anchored_fig(file, cap)
407 outputs.append(flow_item % (fig, cap))
410 print flow_title % (title,)
411 print arrow.join(outputs)
415 def _fig_start(self, cap="", styles=[], width=0, height=0):
416 params = dict(style="", tablewidth="")
418 params["style"] = "width:%d;" % (width,)
419 params["tablewidth"] = 'width="%d"' % (width,)
421 if "lfloat" in styles:
422 return """<table %(tablewidth)s align="center" border="0" cellpadding="0" cellspacing="0" style="float:left; padding-left: 0.5em; %(style)s">
423 <tr> <td valign="top" align="center">
425 elif "left" in styles:
426 return """<table %(tablewidth)s border="0" cellpadding="0" cellspacing="0" style="padding-left: 0.5em; %(style)s">
427 <tr> <td valign="top" align="center">
430 return """<table %(tablewidth)s align="center" border="0" cellpadding="0" cellspacing="0">
431 <tr> <td valign="top" align="center">
434 def _fig_end(self, cap="", styles=[]):
435 return """</td> </tr>
436 <tr> <td><span style="font-size: 80%%; font-weight: bold;">
442 def _fig(self, line):
443 if self._release == 1:
444 self._fig_release(line)
448 def _get_png_geom(self, filepath):
449 desc = deterfile.file(filepath)
451 m = re.match(r"([0-9]+)\s*x\s*([0-9]+)", desc[1])
454 raise Exception("deterfile error: %s, file: %s" % (err,filepath))
458 return (int(w), int(h))
462 def _fig(self, line):
464 str_title = re.search(ur"^☆(図.*)$", line).group(1)
465 except AttributeError:
467 if str_title.find(u"図*") == 0:
468 str_title = str_title.replace(u"図*", "")
469 line = self.input_iter.next()
471 if line.find("@") == 0:
472 styles = line.strip().replace("@", "").split(",")
473 line = self.input_iter.next()
479 match_o1 = re.search(ur"<([^,]*?)>", line)
480 match_o2 = re.search(ur"<(.*?),\s*(.*?)>", line)
481 if not match_o1 == None:
482 imgname = match_o1.group(1)
483 imgname = os.path.join(self._image_dir, imgname)
484 imgname_s = re.sub(r"(.[A-Za-z0-9_]+)$", r"_s\1", imgname)
485 elif not match_o2 == None:
486 imgname = match_o2.group(1)
487 imgname = os.path.join(self._image_dir, imgname)
488 imgname_s = match_o2.group(2)
490 geom = self._get_png_geom(imgname_s)
494 print self._fig_start("", styles, width=w, height=h)
496 print self._fig_start("", styles)
497 print self._anchored_fig(imgname, str_title, imgname_s)
498 print self._fig_end(str_title, styles);
500 dic = self.index("figs")
503 dic.append(imgname_s)
506 def _anchored_fig(self, file, alt, file_s=""):
509 file_s = re.sub(r"(.[A-Za-z0-9_]+)$", r"_s\1", file)
511 if not os.path.isfile(file_s):
514 alt = re.sub(r"""<[A-Za-z0-9!/]+.*?>""", "", alt)
516 return """<a href="%s">
517 <img src="%s" alt="%s">
519 """ % (file, file_s, alt)
522 def _fig_release(self, line):
524 str_title = re.search(ur"^☆(図.*)$", line).group(1)
525 except AttributeError:
527 print self._fig_start()
529 line = self.input_iter.next()
534 match_o1 = re.search(ur"<([^,]*?)>", line)
535 match_o2 = re.search(ur"<(.*?),\s*(.*?)>", line)
536 if not match_o1 == None:
537 imgname = match_o1.group(1)
538 imgname_s = re.sub(r"(.[A-Za-z0-9_]+)$", r"_s\1", match_o1.group(1))
539 elif not match_o2 == None:
540 imgname = match_o1.group(1)
541 imgname_s = match_o1.group(2)
544 hash = self.hashlist.get(imgname, "")
545 hash_s = self.hashlist.get(imgname_s, "")
549 print """<a href="/blob.pl?id=%s">
550 <slash type="image" id="%s" title="%s">
552 """ % (hash, hash_s, str_title)
555 dic = self.index("figs")
558 dic.append(imgname_s)
560 print self._fig_end(str_title);
563 def _table_start(self, cap):
564 return """<div style="width:90%%; margin-left:auto;margin-right:auto;"><table align="center" border="1" class="table">
565 <caption><b>%s</b></caption>
568 def _table_end(self, footnote=""):
569 return "</table>\n%s</div>\n" % (footnote,)
571 def _table(self, line):
573 self._table_buf1 = ""
576 str_title = re.search(ur"^☆(表.*)$", line).group(1)
577 fig_name = re.search(ur"^☆(表[0-9A-Z]*)", line).group(1)
578 except AttributeError:
581 if str_title.find(u"表*") == 0:
582 str_title = str_title.replace(u"表*", "")
584 print self._table_start(str_title)
585 self._table_buf1 = self._table_start(str_title)
590 for line in self.input_iter:
591 line = line.strip(" \n")
592 line = self._default_markup_rule(line)
593 if re.search(ur"^\s*$", line):
595 if re.search(ur"^※", line):
596 footnote = re.search(ur"^(※.*)$", line).group(1)
598 line = self._default_markup_rule(line)
599 if re.search(ur"^〓", line):
600 line = re.sub(ur"^〓", "", line)
604 table_contents.append([])
606 for item in line.split("\t"):
611 while table_contents[num_row-n][num_col]["item"] == "":
613 table_contents[num_row-n][num_col]["row"] += 1
619 while table_contents[num_row][num_col-n]["item"] == "":
621 table_contents[num_row][num_col-n]["col"] += 1
625 table_contents[num_row].append({"tag":tag_mode,"item":item,"row":1,"col":1})
626 num_col = num_col + 1
627 num_row = num_row + 1
629 for row_item in table_contents:
631 for item in row_item:
632 if item["item"] == "":
634 line = line + "<" + item["tag"]
635 if not item["row"] == 1:
636 line = line + (' rowspan="%s"' % item["row"])
637 if not item["col"] == 1:
638 line = line + (' colspan="%s"' % item["col"])
640 line = line + item["item"]
641 line = line + "</" + item["tag"] + ">"
642 line = line + "</tr>\n"
644 self._table_buf1 = self._table_buf1 + line
646 # line = "<tr><th>" + re.sub(ur"^〓", "", line) + "</th></tr>"
647 # line = line.replace("\t", "</th><th>")
650 # line = "<tr><td>" + line + "</td></tr>"
651 # line = line.replace("\t", "</td><td>")
654 print self._table_end(footnote)
655 self._table_buf1 = self._table_buf1 + self._table_end()
656 if self.index_haskey("tables"):
657 self.index("tables")[fig_name] = self._table_buf1
659 self.index_add("tables", {fig_name:self._table_buf1})
661 def _call_tables(self, line):
663 fig_name = re.search(ur"^☆call_tables\((表[0-9A-Z]+)", line).group(1)
664 except AttributeError:
666 print self.index("tables")[fig_name]