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"^☆begin-column:", line):
105 self._begin_column(line)
107 elif re.search(ur"^☆end-column", line):
108 self._end_column(line)
110 elif re.search(ur"^☆space", line):
113 elif re.search(ur"^☆call_tables", line):
114 self._call_tables(line)
116 elif re.search(ur"^●", 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"^☆flow", line):
141 if re.search(ur"^ ", line):
142 self._paragraph(line)
146 if re.search(r"^\s*$", line):
154 def _clear(self, line):
155 print """<div style="clear:left;"> </div>
158 def _head_l(self, line):
160 if re.search(ur"\*{[a-zA-Z0-9_]*}\s*$", line):
161 self._anchor = re.search(ur"\*\{([a-zA-Z0-9_]*)\}\s*$", line).group(1)
162 line = re.sub(ur"\s*\*\{[a-zA-Z0-9_]*\}\s*$", "", line)
164 line = self._default_markup_rule(line)
165 if self._anchor != "":
166 line = re.sub(ur"^●(.*)$", ur'<div id="%s"><h3>\1</h3></div>' % self._anchor, line)
169 line = re.sub(ur"^●(.*)$", ur"<h3>\1</h3>", line)
172 def _head_m(self, line):
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)
178 line = self._default_markup_rule(line)
179 if self._anchor != "":
180 line = re.sub(ur"^○(.*)$", ur'<div id="%s"><h4>\1</h4></div>' % self._anchor, line)
183 line = re.sub(ur"^○(.*)$", ur"<h4>\1</h4>", line)
186 def _paragraph(self, line):
187 line = self._default_markup_rule(line)
188 line = "<p>" + line + "</p>"
191 def _newpage(self, line):
192 line = re.sub(ur"☆----.*-{0,1}", u"<hr>", line)
195 def load_index(self, path_to_index):
199 @param path_to_index: index db's path
200 @type path_to_index: string
204 index_file = open(path_to_index, "r")
205 self._index_past = pickle.load(index_file)
208 sys.stderr.write("warn: cannot read index file,\n")
210 def save_index(self, path_to_index):
214 @param path_to_index: index db's path
215 @type path_to_index: string
219 index_file = open(path_to_index, "w")
220 pickle.dump(self._index, index_file)
223 sys.stderr.write("warn: cannot write index file,\n")
225 def make_hashlist(self, path_to_hashfile):
229 @param path_to_hashfile: hashfile's path
230 @type path_to_hashfile: string
233 file_img_hash = open(path_to_hashfile, "r")
235 sys.stderr.write("cannot open file: %s" % path_img_hash)
239 for line in file_img_hash:
240 splited = line.strip().split("\t", 2)
241 # hashlist's format: <hash> \t <filename>
242 self.hashlist[splited[1]] = splited[0]
244 def _call_tagles(self):
247 def _escape(self, line):
248 line = re.sub(ur"&", ur"&", line)
249 line = re.sub(ur"<", ur"<", line)
250 line = re.sub(ur">", ur">", line)
253 def _default_markup_rule(self, line):
255 apply default markup rules.
257 @param line: string to apply markup
260 line = self._escape(line)
263 # line = tag_filter.apply(line)
265 line = re.sub(ur"[★*](表[0-9~、]+)", ur"<b>\1</b>", line)
266 line = re.sub(ur"[★*](図[0-9~、]+)", ur"<b>\1</b>", line)
267 line = re.sub(ur"[★*](リスト[0-9~、]+)", ur"<b>\1</b>", line)
268 line = re.sub(ur"[★*]b\[(.*?)\]", ur"<b>\1</b>", line)
269 line = re.sub(ur"[★*]b\{(.*?)\}", ur"<b>\1</b>", line)
270 line = re.sub(ur"[★*]\[(\S*) (.*?)\]", r'<a href="\1">\2</a>', line)
271 line = re.sub(ur"[★*]\[(\S*)\]", r'<a href="\1">\1</a>', line)
274 if re.search(ur"^☆#", line):
280 def _ulist(self, line):
283 while re.search(ur"^・", line):
284 line = self._default_markup_rule(line)
285 print re.sub(ur"^・(.*)$", ur"<li>\1</li>", line.strip())
286 line = self.input_iter.next()
289 def _begin_column(self, line):
290 """Proccess column"""
292 str_title = re.search(ur"^☆begin-column:(.*)$", line).group(1)
293 except AttributeError:
296 html = """<table bgcolor="#DDDDDD" border="0" cellpadding="6" width="95%%">
298 <tr><td><span style="font-size: 85%%;">
302 def _end_column(self, line):
303 print """</span></td></tr>
307 def _list_start(self):
313 def _list(self, line):
315 str_title = re.search(ur"^☆(リスト.*)$", line).group(1)
316 except AttributeError:
318 print "<p><b>%s</b></p>" % (str_title)
319 print self._list_start()
321 for line in self.input_iter:
322 line = line.strip("\n\r")
323 line = self._escape(line)
324 if re.search(ur"""^☆\+---""", line):
327 print self._list_end()
329 def _code(self, line):
330 print self._list_start()
332 for line in self.input_iter:
333 line = self._escape(line)
334 line = line = re.sub(ur"[★*]b\[(.*?)]", ur"<b>\1</b>", line)
335 line = line = re.sub(ur"[★*]b{(.*?)}", ur"<b>\1</b>", line)
337 if re.search(ur"^☆\+---$", line):
340 print self._list_end()
342 def _inline(self, line):
343 for line in self.input_iter:
344 # line = line.strip()
345 if re.search(ur"^☆}}}", line):
349 def _comment(self, line):
350 for line in self.input_iter:
352 if re.search(ur"^☆}}}", line):
355 def _space(self, line):
358 def _flow(self, line):
359 down_arrow = "http://static.sourceforge.jp/crystal/22x22/actions/1downarrow.png"
360 flow_header = """<div style="text-align:center; border: 1px solid; background-color:#EFF2F0; width:90%; margin: 0 auto 1em;">
362 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;">
365 flow_footer = """</div>
373 arrow = '<div style="margin:1em auto;"><img src="%s"></div>\n' % (down_arrow,)
375 rex_title = re.compile(ur"^☆flow\s+(.*)$")
376 if rex_title.search(line):
377 title = rex_title.search(line).group(1)
381 rex_file = re.compile(ur"^([^:]*):(.*)$")
383 for line in self.input_iter:
384 if re.search(r"^\s*$", line):
386 match = rex_file.search(line)
388 file = os.path.join(self._image_dir, match.group(1))
392 fig = self._anchored_fig(file, cap)
393 outputs.append(flow_item % (fig, cap))
396 print flow_title % (title,)
397 print arrow.join(outputs)
401 def _fig_start(self, cap="", styles=[], width=0, height=0):
402 params = dict(style="", tablewidth="")
404 params["style"] = "width:%d;" % (width,)
405 params["tablewidth"] = 'width="%d"' % (width,)
407 if "lfloat" in styles:
408 return """<table %(tablewidth)s align="center" border="0" cellpadding="0" cellspacing="0" style="float:left; padding-left: 0.5em; %(style)s">
409 <tr> <td valign="top" align="center">
411 elif "left" in styles:
412 return """<table %(tablewidth)s border="0" cellpadding="0" cellspacing="0" style="padding-left: 0.5em; %(style)s">
413 <tr> <td valign="top" align="center">
416 return """<table %(tablewidth)s align="center" border="0" cellpadding="0" cellspacing="0">
417 <tr> <td valign="top" align="center">
420 def _fig_end(self, cap="", styles=[]):
421 return """</td> </tr>
422 <tr> <td><span style="font-size: 80%%; font-weight: bold;">
428 def _fig(self, line):
429 if self._release == 1:
430 self._fig_release(line)
434 def _get_png_geom(self, filepath):
435 desc = deterfile.file(filepath)
436 m = re.match(r"([0-9]+)\s*x\s*([0-9]+)", desc[1])
440 return (int(w), int(h))
444 def _fig(self, line):
446 str_title = re.search(ur"^☆(図.*)$", line).group(1)
447 except AttributeError:
449 if str_title.find(u"図*") == 0:
450 str_title = str_title.replace(u"図*", "")
451 line = self.input_iter.next()
453 if line.find("@") == 0:
454 styles = line.strip().replace("@", "").split(",")
455 line = self.input_iter.next()
461 match_o1 = re.search(ur"<([^,]*?)>", line)
462 match_o2 = re.search(ur"<(.*?),\s*(.*?)>", line)
463 if not match_o1 == None:
464 imgname = match_o1.group(1)
465 imgname = os.path.join(self._image_dir, imgname)
466 imgname_s = re.sub(r"(.[A-Za-z0-9_]+)$", r"_s\1", imgname)
467 elif not match_o2 == None:
468 imgname = match_o1.group(1)
469 imgname = os.path.join(self._image_dir, imgname)
470 imgname_s = match_o1.group(2)
472 geom = self._get_png_geom(imgname_s)
476 print self._fig_start("", styles, width=w, height=h)
478 print self._fig_start("", styles)
479 print self._anchored_fig(imgname, str_title, imgname_s)
480 print self._fig_end(str_title, styles);
482 dic = self.index("figs")
485 dic.append(imgname_s)
488 def _anchored_fig(self, file, alt, file_s=""):
491 file_s = re.sub(r"(.[A-Za-z0-9_]+)$", r"_s\1", file)
493 if not os.path.isfile(file_s):
496 return """<a href="%s">
497 <img src="%s" alt="%s">
499 """ % (file, file_s, alt)
502 def _fig_release(self, line):
504 str_title = re.search(ur"^☆(図.*)$", line).group(1)
505 except AttributeError:
507 print self._fig_start()
509 line = self.input_iter.next()
514 match_o1 = re.search(ur"<([^,]*?)>", line)
515 match_o2 = re.search(ur"<(.*?),\s*(.*?)>", line)
516 if not match_o1 == None:
517 imgname = match_o1.group(1)
518 imgname_s = re.sub(r"(.[A-Za-z0-9_]+)$", r"_s\1", match_o1.group(1))
519 elif not match_o2 == None:
520 imgname = match_o1.group(1)
521 imgname_s = match_o1.group(2)
524 hash = self.hashlist.get(imgname, "")
525 hash_s = self.hashlist.get(imgname_s, "")
529 print """<a href="/blob.pl?id=%s">
530 <slash type="image" id="%s" title="%s">
532 """ % (hash, hash_s, str_title)
535 dic = self.index("figs")
538 dic.append(imgname_s)
540 print self._fig_end(str_title);
543 def _table_start(self, cap):
544 return """<div style="width:90%%; margin-left:auto;margin-right:auto;"><table align="center" border="1" class="table">
545 <caption><b>%s</b></caption>
548 def _table_end(self, footnote=""):
549 return "</table>\n%s</div>\n" % (footnote,)
551 def _table(self, line):
553 self._table_buf1 = ""
556 str_title = re.search(ur"^☆(表.*)$", line).group(1)
557 fig_name = re.search(ur"^☆(表[0-9A-Z]*)", line).group(1)
558 except AttributeError:
561 if str_title.find(u"表*") == 0:
562 str_title = str_title.replace(u"表*", "")
564 print self._table_start(str_title)
565 self._table_buf1 = self._table_start(str_title)
570 for line in self.input_iter:
571 line = line.strip(" \n")
572 line = self._default_markup_rule(line)
573 if re.search(ur"^\s*$", line):
575 if re.search(ur"^※", line):
576 footnote = re.search(ur"^(※.*)$", line).group(1)
578 line = self._default_markup_rule(line)
579 if re.search(ur"^〓", line):
580 line = re.sub(ur"^〓", "", line)
584 table_contents.append([])
586 for item in line.split("\t"):
591 while table_contents[num_row-n][num_col]["item"] == "":
593 table_contents[num_row-n][num_col]["row"] += 1
599 while table_contents[num_row][num_col-n]["item"] == "":
601 table_contents[num_row][num_col-n]["col"] += 1
605 table_contents[num_row].append({"tag":tag_mode,"item":item,"row":1,"col":1})
606 num_col = num_col + 1
607 num_row = num_row + 1
609 for row_item in table_contents:
611 for item in row_item:
612 if item["item"] == "":
614 line = line + "<" + item["tag"]
615 if not item["row"] == 1:
616 line = line + (' rowspan="%s"' % item["row"])
617 if not item["col"] == 1:
618 line = line + (' colspan="%s"' % item["col"])
620 line = line + item["item"]
621 line = line + "</" + item["tag"] + ">"
622 line = line + "</tr>\n"
624 self._table_buf1 = self._table_buf1 + line
626 # line = "<tr><th>" + re.sub(ur"^〓", "", line) + "</th></tr>"
627 # line = line.replace("\t", "</th><th>")
630 # line = "<tr><td>" + line + "</td></tr>"
631 # line = line.replace("\t", "</td><td>")
634 print self._table_end(footnote)
635 self._table_buf1 = self._table_buf1 + self._table_end()
636 if self.index_haskey("tables"):
637 self.index("tables")[fig_name] = self._table_buf1
639 self.index_add("tables", {fig_name:self._table_buf1})
641 def _call_tables(self, line):
643 fig_name = re.search(ur"^☆call_tables\((表[0-9A-Z]+)", line).group(1)
644 except AttributeError:
646 print self.index("tables")[fig_name]