2 # -*- coding: utf-8 -*-
3 """markupper2.py - markupper v2."""
13 class MarkUpper2(object):
20 def markup(self, input_iter):
24 @param input_iter: iterator to use as input
25 @type input_iter: iterator
27 self.input = input_iter
29 for line in self.input:
30 if re.search(ur"^☆{{{$", line):
33 elif re.search(ur'^\set{([A-Za-z0-9_]+)\s*,\s*"([^"]*)"\s*}', line):
34 g = re.search(ur'^\set{([A-Za-z0-9_]+)\s*,\s*"([^"]*)"\s*}', line)
35 self._vars[g.group(1)] = g.group(2)
37 elif re.search(ur"^☆image_border:\s(on|On|ON)", line):
38 self._image_border = 1
40 elif re.search(ur"^☆comment\s{{{$", line):
43 elif re.search(ur"^☆\*", line):
44 self._anchor = re.sub(ur"^☆\*", "", line).strip()
46 elif re.search(ur"^☆clear\s+", line):
49 elif re.search(ur"^・", line):
52 elif re.search(ur"^[0-9]\.", line):
55 elif re.search(ur"^☆begin-column:", line):
56 self._begin_column(line)
58 elif re.search(ur"^☆end-column", line):
59 self._end_column(line)
61 elif re.search(ur"^☆space", line):
64 elif re.search(ur"^☆call_tables", line):
65 self._call_tables(line)
67 elif re.search(ur"^●", line):
70 elif re.search(ur"^○", line):
73 elif re.search(ur"^☆----", line):
76 elif re.search(ur"^☆\+---", line):
79 elif re.search(ur"^☆表", line):
82 elif re.search(ur"^☆図", line):
85 elif re.search(ur"^☆写真", line):
88 elif re.search(ur"^☆リスト", line):
91 elif re.search(ur"^☆flow", line):
95 if re.search(ur"^ ", line):
100 if re.search(r"^\s*$", line):
108 def _clear(self, line):
109 print """<div style="clear:left;"> </div>
112 def _head_l(self, line):
114 if re.search(ur"\*{[a-zA-Z0-9_]*}\s*$", line):
115 self._anchor = re.search(ur"\*\{([a-zA-Z0-9_]*)\}\s*$", line).group(1)
116 line = re.sub(ur"\s*\*\{[a-zA-Z0-9_]*\}\s*$", "", line)
118 line = self._default_markup_rule(line)
119 if self._anchor != "":
120 line = re.sub(ur"^●(.*)$", ur'<div id="%s"><h3>\1</h3></div>' % self._anchor, line)
123 line = re.sub(ur"^●(.*)$", ur"<h3>\1</h3>", line)
126 def _head_m(self, line):
128 if re.search(ur"\*{[a-zA-Z0-9_]*}\s*$", line):
129 self._anchor = re.search(ur"\*\{([a-zA-Z0-9_]*)\}\s*$", line).group(1)
130 line = re.sub(ur"\s*\*\{[a-zA-Z0-9_]*\}\s*$", "", line)
132 line = self._default_markup_rule(line)
133 if self._anchor != "":
134 line = re.sub(ur"^○(.*)$", ur'<div id="%s"><h4>\1</h4></div>' % self._anchor, line)
137 line = re.sub(ur"^○(.*)$", ur"<h4>\1</h4>", line)
140 def _paragraph(self, line):
141 line = self._default_markup_rule(line)
142 line = "<p>" + line + "</p>"
145 def _newpage(self, line):
146 line = re.sub(ur"☆----.*-{0,1}", u"<hr>", line)
149 def make_hashlist(self, path_to_hashfile):
153 @param path_to_hashfile: hashfile's path
154 @type path_to_hashfile: string
157 file_img_hash = open(path_to_hashfile, "r")
159 sys.stderr.write("cannot open file: %s" % path_img_hash)
163 for line in file_img_hash:
164 splited = line.strip().split("\t", 2)
165 # hashlist's format: <hash> \t <filename>
166 self.hashlist[splited[1]] = splited[0]
168 def _call_tagles(self):
171 def _escape(self, line):
172 line = re.sub(ur"&", ur"&", line)
173 line = re.sub(ur"<", ur"<", line)
174 line = re.sub(ur">", ur">", line)
177 def _default_markup_rule(self, line):
179 apply default markup rules.
181 @param line: string to apply markup
184 line = self._escape(line)
187 # line = tag_filter.apply(line)
189 line = re.sub(ur"[★*](表[0-9~、]+)", ur"<b>\1</b>", line)
190 line = re.sub(ur"[★*](図[0-9~、]+)", ur"<b>\1</b>", line)
191 line = re.sub(ur"[★*](写真[0-9~、]+)", ur"<b>\1</b>", line)
192 line = re.sub(ur"[★*](リスト[0-9~、]+)", ur"<b>\1</b>", line)
193 line = re.sub(ur"[★*]b\[(.*?)\]", ur"<b>\1</b>", line)
194 line = re.sub(ur"[★*]b\{(.*?)\}", ur"<b>\1</b>", line)
195 line = re.sub(ur"[★*]g\[(.*?)]", ur"<span style='color:#BBBBBB'>\1</b>", line)
196 line = re.sub(ur"[★*]g{(.*?)}", ur"<span style='color:#BBBBBB'>\1</b>", line)
197 line = re.sub(ur"[★*]\[(\S*) (.*?)\]", r'<a href="\1">\2</a>', line)
198 line = re.sub(ur"[★*]\[(\S*)\]", r'<a href="\1">\1</a>', line)
201 if re.search(ur"^☆#", line):
207 def _ulist(self, line):
210 while re.search(ur"^・", line):
211 line = self._default_markup_rule(line)
212 print re.sub(ur"^・(.*)$", ur"<li>\1</li>", line.strip())
213 line = self.input.next()
216 def _olist(self, line):
219 while re.search(ur"^[0-9]+\.", line):
220 line = self._default_markup_rule(line)
221 print re.sub(ur"^[0-9]+\.(.*)$", ur"<li>\1</li>", line.strip())
222 line = self.input.next()
227 def _begin_column(self, line):
228 """Proccess column"""
230 str_title = re.search(ur"^☆begin-column:(.*)$", line).group(1)
231 except AttributeError:
234 html = """<table bgcolor="#DDDDDD" border="0" cellpadding="6" width="95%%">
236 <tr><td><span style="font-size: 85%%;">
240 def _end_column(self, line):
241 print """</span></td></tr>
245 def _list_start(self):
251 def _list(self, line):
253 str_title = re.search(ur"^☆(リスト.*)$", line).group(1)
254 except AttributeError:
256 print "<p><b>%s</b></p>" % (str_title)
257 print self._list_start()
259 for line in self.input:
260 line = line.strip("\n\r")
261 line = self._escape(line)
262 line = re.sub(ur"[★*]b\[(.*?)]", ur"<b>\1</b>", line)
263 line = re.sub(ur"[★*]b{(.*?)}", ur"<b>\1</b>", line)
264 line = re.sub(ur"[★*]g\[(.*?)]", ur"<span style='color:#BBBBBB'>\1</b>", line)
265 line = re.sub(ur"[★*]g{(.*?)}", ur"<span style='color:#BBBBBB'>\1</b>", line)
266 if re.search(ur"""^☆\+---""", line):
269 print self._list_end()
271 def _code(self, line):
272 print self._list_start()
274 for line in self.input:
275 line = self._escape(line)
276 line = re.sub(ur"[★*]b\[(.*?)]", ur"<b>\1</b>", line)
277 line = re.sub(ur"[★*]b{(.*?)}", ur"<b>\1</b>", line)
278 line = re.sub(ur"[★*]g\[(.*?)]", ur"<span style='color:#BBBBBB'>\1</b>", line)
279 line = re.sub(ur"[★*]g{(.*?)}", ur"<span style='color:#BBBBBB'>\1</b>", line)
281 if re.search(ur"^☆\+---$", line):
284 print self._list_end()
286 def _inline(self, line):
287 for line in self.input:
289 if re.search(ur"^☆}}}", line):
293 def _comment(self, line):
294 for line in self.input:
296 if re.search(ur"^☆}}}", line):
299 def _space(self, line):
302 def _flow(self, line):
303 down_arrow = "http://static.sourceforge.jp/crystal/22x22/actions/1downarrow.png"
304 flow_header = """<div style="text-align:center; border: 1px solid; background-color:#EFF2F0; width:90%; margin: 0 auto 1em;">
306 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;">
309 flow_footer = """</div>
317 arrow = '<div style="margin:1em auto;"><img src="%s"></div>\n' % (down_arrow,)
319 rex_title = re.compile(ur"^☆flow\s+(.*)$")
320 if rex_title.search(line):
321 title = rex_title.search(line).group(1)
325 rex_file = re.compile(ur"^([^:]*):(.*)$")
327 for line in self.input:
328 if re.search(r"^\s*$", line):
330 match = rex_file.search(line)
332 file = os.path.join(self._image_dir, match.group(1))
333 cap = self._default_markup_rule(match.group(2))
336 fig = self._anchored_fig(file, cap)
337 outputs.append(flow_item % (fig, cap))
340 print flow_title % (title,)
341 print arrow.join(outputs)
345 def _fig_start(self, cap="", styles=[], width=0, height=0):
346 params = dict(style="", tablewidth="")
348 params["style"] = "width:%d;" % (width,)
349 params["tablewidth"] = 'width="%d"' % (width,)
351 if "lfloat" in styles:
352 return """<table %(tablewidth)s align="center" border="0" cellpadding="0" cellspacing="0" style="float:left; padding-left: 0.5em; %(style)s">
353 <tr> <td valign="top" align="center">
355 elif "left" in styles:
356 return """<table %(tablewidth)s border="0" cellpadding="0" cellspacing="0" style="padding-left: 0.5em; %(style)s">
357 <tr> <td valign="top" align="center">
360 return """<table %(tablewidth)s align="center" border="0" cellpadding="0" cellspacing="0">
361 <tr> <td valign="top" align="center">
364 def _fig_end(self, cap="", styles=[]):
365 return """</td> </tr>
366 <tr> <td><span style="font-size: 80%%; font-weight: bold;">
372 def _get_png_geom(self, filepath):
373 desc = deterfile.file(filepath)
375 m = re.match(r"([0-9]+)\s*x\s*([0-9]+)", desc[1])
378 raise Exception("deterfile error: %s, file: %s" % (err,filepath))
382 return (int(w), int(h))
386 def _fig(self, line):
388 str_title = re.search(ur"^☆(図.*)$", line).group(1)
389 except AttributeError:
391 if str_title.find(u"図*") == 0:
392 str_title = str_title.replace(u"図*", "")
393 line = self.input.next()
395 if line.find("@") == 0:
396 styles = line.strip().replace("@", "").split(",")
397 line = self.input.next()
404 match_o1 = re.search(ur"<([^,]*?)>", line)
405 match_o2 = re.search(ur"<(.*?),\s*(.*?)>", line)
406 if not match_o1 == None:
407 imgname = match_o1.group(1)
408 imgname = os.path.join(self._image_dir, imgname)
409 imgname_s = re.sub(r"(.[A-Za-z0-9_]+)$", r"_s\1", imgname)
410 elif not match_o2 == None:
411 imgname = match_o2.group(1)
412 imgname = os.path.join(self._image_dir, imgname)
413 imgname_s = match_o2.group(2)
414 imgname_s = os.path.join(self._image_dir, imgname_s)
416 geom = self._get_png_geom(imgname_s)
420 print self._fig_start("", styles, width=w, height=h)
422 print self._fig_start("", styles)
423 print self._anchored_fig(imgname, str_title, imgname_s)
424 print self._fig_end(str_title, styles);
428 match_o = re.search(ur"\[(.*?),\s*(.*?)\]", line)
430 print """<table width="500" align="center" border="0" cellpadding="0" cellspacing="0">
431 <tr> <td valign="top" align="center">
433 print """<a href="/blob.pl?id=%s">
434 <slash-image id="%s" title="%s">
435 </a>""" % (match_o.group(1), match_o.group(2), str_title)
437 <tr> <td><span style="font-size: 80%%; font-weight: bold;">
443 print >> sys.stderr, "error:cannot parse id. " + line
445 def _photo(self, line):
447 str_title = re.search(ur"^☆(写真.*)$", line).group(1)
448 except AttributeError:
450 if str_title.find(u"写真*") == 0:
451 str_title = str_title.replace(u"写真*", "")
452 line = self.input.next()
454 if line.find("@") == 0:
455 styles = line.strip().replace("@", "").split(",")
456 line = self.input.next()
462 match_o1 = re.search(ur"<([^,]*?)>", line)
463 match_o2 = re.search(ur"<(.*?),\s*(.*?)>", line)
464 if not match_o1 == None:
465 imgname = match_o1.group(1)
466 imgname = os.path.join(self._image_dir, imgname)
467 imgname_s = re.sub(r"(.[A-Za-z0-9_]+)$", r"_s\1", imgname)
468 elif not match_o2 == None:
469 imgname = match_o2.group(1)
470 imgname = os.path.join(self._image_dir, imgname)
471 imgname_s = match_o2.group(2)
472 imgname_s = os.path.join(self._image_dir, imgname_s)
474 geom = self._get_png_geom(imgname_s)
478 print self._fig_start("", styles, width=w, height=h)
480 print self._fig_start("", styles)
481 print self._anchored_fig(imgname, str_title, imgname_s)
482 print self._fig_end(str_title, styles);
486 def _anchored_fig(self, file, alt, file_s=""):
489 file_s = re.sub(r"(.[A-Za-z0-9_]+)$", r"_s\1", file)
491 if not os.path.isfile(file_s):
494 alt = re.sub(r"""<[A-Za-z0-9!/]+.*?>""", "", alt)
496 ret = """<a href="%s">
497 <img src="%s" alt="%s">
499 """ % (file, file_s, alt)
505 def _fig_release(self, line):
507 str_title = re.search(ur"^☆(図.*)$", line).group(1)
508 except AttributeError:
510 print self._fig_start()
512 line = self.input.next()
517 match_o1 = re.search(ur"<([^,]*?)>", line)
518 match_o2 = re.search(ur"<(.*?),\s*(.*?)>", line)
519 if not match_o1 == None:
520 imgname = match_o1.group(1)
521 imgname_s = re.sub(r"(.[A-Za-z0-9_]+)$", r"_s\1", match_o1.group(1))
522 elif not match_o2 == None:
523 imgname = match_o1.group(1)
524 imgname_s = match_o1.group(2)
527 hash = self.hashlist.get(imgname, "")
528 hash_s = self.hashlist.get(imgname_s, "")
532 print """<a href="/blob.pl?id=%s">
533 <slash type="image" id="%s" title="%s">
535 """ % (hash, hash_s, str_title)
539 print self._fig_end(str_title);
542 def _table_start(self, cap):
543 return """<div style="width:90%%; margin-left:auto;margin-right:auto;"><table align="center" border="1" class="table" width="100%%">
544 <caption><b>%s</b></caption>
547 def _table_end(self, footnote=""):
548 return "</table>\n%s</div>\n" % (footnote,)
550 def _table(self, line):
552 self._table_buf1 = ""
555 str_title = re.search(ur"^☆(表.*)$", line).group(1)
556 fig_name = re.search(ur"^☆(表[0-9A-Z]*)", line).group(1)
557 except AttributeError:
560 if str_title.find(u"表*") == 0:
561 str_title = str_title.replace(u"表*", "")
563 print self._table_start(str_title)
564 self._table_buf1 = self._table_start(str_title)
569 for line in self.input:
570 line = line.strip(" \n")
571 line = self._default_markup_rule(line)
572 if re.search(ur"^\s*$", line):
574 if re.search(ur"^※", line):
575 footnote = re.search(ur"^(※.*)$", line).group(1)
577 if re.search(ur"^〓", line):
578 line = re.sub(ur"^〓", "", line)
582 table_contents.append([])
584 for item in line.split("\t"):
589 while table_contents[num_row-n][num_col]["item"] == "":
591 table_contents[num_row-n][num_col]["row"] += 1
597 while table_contents[num_row][num_col-n]["item"] == "":
599 table_contents[num_row][num_col-n]["col"] += 1
605 while table_contents[num_row-n][num_col]["item"] == "":
607 table_contents[num_row-n][num_col]["row"] += 1
612 if re.search(r'^".*"$', item):
613 item = re.search(r'^"(.*)"$', item).group(1)
614 table_contents[num_row].append({"tag":"th","item":item,"row":1,"col":1})
616 table_contents[num_row].append({"tag":tag_mode,"item":item,"row":1,"col":1})
617 num_col = num_col + 1
618 num_row = num_row + 1
620 for row_item in table_contents:
622 for item in row_item:
623 if item["item"] == "":
625 line = line + "<" + item["tag"]
626 if not item["row"] == 1:
627 line = line + (' rowspan="%s"' % item["row"])
628 if not item["col"] == 1:
629 line = line + (' colspan="%s"' % item["col"])
631 line = line + item["item"]
632 line = line + "</" + item["tag"] + ">"
633 line = line + "</tr>\n"
635 self._table_buf1 = self._table_buf1 + line
637 # line = "<tr><th>" + re.sub(ur"^〓", "", line) + "</th></tr>"
638 # line = line.replace("\t", "</th><th>")
641 # line = "<tr><td>" + line + "</td></tr>"
642 # line = line.replace("\t", "</td><td>")
645 print self._table_end(footnote)
646 self._table_buf1 = self._table_buf1 + self._table_end()
648 def _call_tables(self, line):
650 fig_name = re.search(ur"^☆call_tables\((表[0-9A-Z]+)", line).group(1)
651 except AttributeError: