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"[★*]\[(\S*) (.*?)\]", r'<a href="\1">\2</a>', line)
196 line = re.sub(ur"[★*]\[(\S*)\]", r'<a href="\1">\1</a>', line)
199 if re.search(ur"^☆#", line):
205 def _ulist(self, line):
208 while re.search(ur"^・", line):
209 line = self._default_markup_rule(line)
210 print re.sub(ur"^・(.*)$", ur"<li>\1</li>", line.strip())
211 line = self.input.next()
214 def _olist(self, line):
217 while re.search(ur"^[0-9]+\.", line):
218 line = self._default_markup_rule(line)
219 print re.sub(ur"^[0-9]+\.(.*)$", ur"<li>\1</li>", line.strip())
220 line = self.input.next()
225 def _begin_column(self, line):
226 """Proccess column"""
228 str_title = re.search(ur"^☆begin-column:(.*)$", line).group(1)
229 except AttributeError:
232 html = """<table bgcolor="#DDDDDD" border="0" cellpadding="6" width="95%%">
234 <tr><td><span style="font-size: 85%%;">
238 def _end_column(self, line):
239 print """</span></td></tr>
243 def _list_start(self):
249 def _list(self, line):
251 str_title = re.search(ur"^☆(リスト.*)$", line).group(1)
252 except AttributeError:
254 print "<p><b>%s</b></p>" % (str_title)
255 print self._list_start()
257 for line in self.input:
258 line = line.strip("\n\r")
259 line = self._escape(line)
260 line = line = re.sub(ur"[★*]b\[(.*?)]", ur"<b>\1</b>", line)
261 line = line = re.sub(ur"[★*]b{(.*?)}", ur"<b>\1</b>", line)
262 if re.search(ur"""^☆\+---""", line):
265 print self._list_end()
267 def _code(self, line):
268 print self._list_start()
270 for line in self.input:
271 line = self._escape(line)
272 line = line = re.sub(ur"[★*]b\[(.*?)]", ur"<b>\1</b>", line)
273 line = line = re.sub(ur"[★*]b{(.*?)}", ur"<b>\1</b>", line)
275 if re.search(ur"^☆\+---$", line):
278 print self._list_end()
280 def _inline(self, line):
281 for line in self.input:
282 # line = line.strip()
283 if re.search(ur"^☆}}}", line):
287 def _comment(self, line):
288 for line in self.input:
290 if re.search(ur"^☆}}}", line):
293 def _space(self, line):
296 def _flow(self, line):
297 down_arrow = "http://static.sourceforge.jp/crystal/22x22/actions/1downarrow.png"
298 flow_header = """<div style="text-align:center; border: 1px solid; background-color:#EFF2F0; width:90%; margin: 0 auto 1em;">
300 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;">
303 flow_footer = """</div>
311 arrow = '<div style="margin:1em auto;"><img src="%s"></div>\n' % (down_arrow,)
313 rex_title = re.compile(ur"^☆flow\s+(.*)$")
314 if rex_title.search(line):
315 title = rex_title.search(line).group(1)
319 rex_file = re.compile(ur"^([^:]*):(.*)$")
321 for line in self.input:
322 if re.search(r"^\s*$", line):
324 match = rex_file.search(line)
326 file = os.path.join(self._image_dir, match.group(1))
327 cap = self._default_markup_rule(match.group(2))
330 fig = self._anchored_fig(file, cap)
331 outputs.append(flow_item % (fig, cap))
334 print flow_title % (title,)
335 print arrow.join(outputs)
339 def _fig_start(self, cap="", styles=[], width=0, height=0):
340 params = dict(style="", tablewidth="")
342 params["style"] = "width:%d;" % (width,)
343 params["tablewidth"] = 'width="%d"' % (width,)
345 if "lfloat" in styles:
346 return """<table %(tablewidth)s align="center" border="0" cellpadding="0" cellspacing="0" style="float:left; padding-left: 0.5em; %(style)s">
347 <tr> <td valign="top" align="center">
349 elif "left" in styles:
350 return """<table %(tablewidth)s border="0" cellpadding="0" cellspacing="0" style="padding-left: 0.5em; %(style)s">
351 <tr> <td valign="top" align="center">
354 return """<table %(tablewidth)s align="center" border="0" cellpadding="0" cellspacing="0">
355 <tr> <td valign="top" align="center">
358 def _fig_end(self, cap="", styles=[]):
359 return """</td> </tr>
360 <tr> <td><span style="font-size: 80%%; font-weight: bold;">
366 def _get_png_geom(self, filepath):
367 desc = deterfile.file(filepath)
369 m = re.match(r"([0-9]+)\s*x\s*([0-9]+)", desc[1])
372 raise Exception("deterfile error: %s, file: %s" % (err,filepath))
376 return (int(w), int(h))
380 def _fig(self, line):
382 str_title = re.search(ur"^☆(図.*)$", line).group(1)
383 except AttributeError:
385 if str_title.find(u"図*") == 0:
386 str_title = str_title.replace(u"図*", "")
387 line = self.input.next()
389 if line.find("@") == 0:
390 styles = line.strip().replace("@", "").split(",")
391 line = self.input.next()
398 match_o1 = re.search(ur"<([^,]*?)>", line)
399 match_o2 = re.search(ur"<(.*?),\s*(.*?)>", line)
400 if not match_o1 == None:
401 imgname = match_o1.group(1)
402 imgname = os.path.join(self._image_dir, imgname)
403 imgname_s = re.sub(r"(.[A-Za-z0-9_]+)$", r"_s\1", imgname)
404 elif not match_o2 == None:
405 imgname = match_o2.group(1)
406 imgname = os.path.join(self._image_dir, imgname)
407 imgname_s = match_o2.group(2)
408 imgname_s = os.path.join(self._image_dir, imgname_s)
410 geom = self._get_png_geom(imgname_s)
414 print self._fig_start("", styles, width=w, height=h)
416 print self._fig_start("", styles)
417 print self._anchored_fig(imgname, str_title, imgname_s)
418 print self._fig_end(str_title, styles);
422 match_o = re.search(ur"\[(.*?),\s*(.*?)\]", line)
424 print """<table width="500" align="center" border="0" cellpadding="0" cellspacing="0">
425 <tr> <td valign="top" align="center">
427 print """<a href="/blob.pl?id=%s">
428 <slash-image id="%s" title="%s">
429 </a>""" % (match_o.group(1), match_o.group(2), str_title)
431 <tr> <td><span style="font-size: 80%%; font-weight: bold;">
437 print >> sys.stderr, "error:cannot parse id. " + line
439 def _photo(self, line):
441 str_title = re.search(ur"^☆(写真.*)$", line).group(1)
442 except AttributeError:
444 if str_title.find(u"写真*") == 0:
445 str_title = str_title.replace(u"写真*", "")
446 line = self.input.next()
448 if line.find("@") == 0:
449 styles = line.strip().replace("@", "").split(",")
450 line = self.input.next()
456 match_o1 = re.search(ur"<([^,]*?)>", line)
457 match_o2 = re.search(ur"<(.*?),\s*(.*?)>", line)
458 if not match_o1 == None:
459 imgname = match_o1.group(1)
460 imgname = os.path.join(self._image_dir, imgname)
461 imgname_s = re.sub(r"(.[A-Za-z0-9_]+)$", r"_s\1", imgname)
462 elif not match_o2 == None:
463 imgname = match_o2.group(1)
464 imgname = os.path.join(self._image_dir, imgname)
465 imgname_s = match_o2.group(2)
466 imgname_s = os.path.join(self._image_dir, imgname_s)
468 geom = self._get_png_geom(imgname_s)
472 print self._fig_start("", styles, width=w, height=h)
474 print self._fig_start("", styles)
475 print self._anchored_fig(imgname, str_title, imgname_s)
476 print self._fig_end(str_title, styles);
480 def _anchored_fig(self, file, alt, file_s=""):
483 file_s = re.sub(r"(.[A-Za-z0-9_]+)$", r"_s\1", file)
485 if not os.path.isfile(file_s):
488 alt = re.sub(r"""<[A-Za-z0-9!/]+.*?>""", "", alt)
490 ret = """<a href="%s">
491 <img src="%s" alt="%s">
493 """ % (file, file_s, alt)
499 def _fig_release(self, line):
501 str_title = re.search(ur"^☆(図.*)$", line).group(1)
502 except AttributeError:
504 print self._fig_start()
506 line = self.input.next()
511 match_o1 = re.search(ur"<([^,]*?)>", line)
512 match_o2 = re.search(ur"<(.*?),\s*(.*?)>", line)
513 if not match_o1 == None:
514 imgname = match_o1.group(1)
515 imgname_s = re.sub(r"(.[A-Za-z0-9_]+)$", r"_s\1", match_o1.group(1))
516 elif not match_o2 == None:
517 imgname = match_o1.group(1)
518 imgname_s = match_o1.group(2)
521 hash = self.hashlist.get(imgname, "")
522 hash_s = self.hashlist.get(imgname_s, "")
526 print """<a href="/blob.pl?id=%s">
527 <slash type="image" id="%s" title="%s">
529 """ % (hash, hash_s, str_title)
533 print self._fig_end(str_title);
536 def _table_start(self, cap):
537 return """<div style="width:90%%; margin-left:auto;margin-right:auto;"><table align="center" border="1" class="table" width="100%%">
538 <caption><b>%s</b></caption>
541 def _table_end(self, footnote=""):
542 return "</table>\n%s</div>\n" % (footnote,)
544 def _table(self, line):
546 self._table_buf1 = ""
549 str_title = re.search(ur"^☆(表.*)$", line).group(1)
550 fig_name = re.search(ur"^☆(表[0-9A-Z]*)", line).group(1)
551 except AttributeError:
554 if str_title.find(u"表*") == 0:
555 str_title = str_title.replace(u"表*", "")
557 print self._table_start(str_title)
558 self._table_buf1 = self._table_start(str_title)
563 for line in self.input:
564 line = line.strip(" \n")
565 line = self._default_markup_rule(line)
566 if re.search(ur"^\s*$", line):
568 if re.search(ur"^※", line):
569 footnote = re.search(ur"^(※.*)$", line).group(1)
571 if re.search(ur"^〓", line):
572 line = re.sub(ur"^〓", "", line)
576 table_contents.append([])
578 for item in line.split("\t"):
583 while table_contents[num_row-n][num_col]["item"] == "":
585 table_contents[num_row-n][num_col]["row"] += 1
591 while table_contents[num_row][num_col-n]["item"] == "":
593 table_contents[num_row][num_col-n]["col"] += 1
599 while table_contents[num_row-n][num_col]["item"] == "":
601 table_contents[num_row-n][num_col]["row"] += 1
606 if re.search(r'^".*"$', item):
607 item = re.search(r'^"(.*)"$', item).group(1)
608 table_contents[num_row].append({"tag":"th","item":item,"row":1,"col":1})
610 table_contents[num_row].append({"tag":tag_mode,"item":item,"row":1,"col":1})
611 num_col = num_col + 1
612 num_row = num_row + 1
614 for row_item in table_contents:
616 for item in row_item:
617 if item["item"] == "":
619 line = line + "<" + item["tag"]
620 if not item["row"] == 1:
621 line = line + (' rowspan="%s"' % item["row"])
622 if not item["col"] == 1:
623 line = line + (' colspan="%s"' % item["col"])
625 line = line + item["item"]
626 line = line + "</" + item["tag"] + ">"
627 line = line + "</tr>\n"
629 self._table_buf1 = self._table_buf1 + line
631 # line = "<tr><th>" + re.sub(ur"^〓", "", line) + "</th></tr>"
632 # line = line.replace("\t", "</th><th>")
635 # line = "<tr><td>" + line + "</td></tr>"
636 # line = line.replace("\t", "</td><td>")
639 print self._table_end(footnote)
640 self._table_buf1 = self._table_buf1 + self._table_end()
642 def _call_tables(self, line):
644 fig_name = re.search(ur"^☆call_tables\((表[0-9A-Z]+)", line).group(1)
645 except AttributeError: