2 # -*- coding: utf-8 -*-
12 sys.stdin = codecs.getreader('utf_8')(sys.stdin)
13 sys.stdout = codecs.getwriter('utf_8')(sys.stdout)
15 alist = ["a", "a:href", "a:name", "b", "br" ]
18 tag_filter = HTMLTagFilter.HTMLTagFilter(HTMLTagFilter.DENY_ALLOW, alist, dlist)
19 path_to_index = "./_markup_index"
21 class _InputStream(object):
22 """InputStream base class."""
31 """function for iterator"""
35 class Markupper(object):
39 self._input_iter = None
43 def index_add(self, key, val):
45 Add key and value to index.
53 self._index[key] = val
59 return self._index[key]
61 def index_haskey(self, key):
62 return self._index.has_key(key)
64 def markup(self, input_iter, release="0"):
68 @param input_iter: iterator to use as input
69 @type input_iter: iterator
71 self.input_iter = input_iter
72 self._page_counter = 1
73 # alist = ["a", "a:href", "a:name", "b", "br" ]
75 # tag_filter = HTMLTagFilter.HTMLTagFilter(HTMLTagFilter.DENY_ALLOW, alist, dlist)
76 self._release = release
78 self.index_add("figs", [])
81 for line in self.input_iter:
82 line = self._default_markup_rule(line)
84 if re.search(ur"^☆{{{$", line):
87 elif re.search(ur"^☆comment\s{{{$", line):
90 elif re.search(ur"^☆\*", line):
91 self._anchor = re.sub(ur"^☆\*", "", line).strip()
93 elif re.search(ur"^・", line):
96 elif re.search(ur"^☆begin-column:", line):
97 self._begin_column(line)
99 elif re.search(ur"^☆end-column", line):
100 self._end_column(line)
102 elif re.search(ur"^☆space", line):
105 elif re.search(ur"^☆call_tables", line):
106 self._call_tables(line)
108 elif re.search(ur"^●", line):
110 line = re.sub(ur"^●(.*)$", ur'<h4 id="%s">\1</h4>' % anchor, line)
113 line = re.sub(ur"^●(.*)$", ur"<h4>\1</h4>", line)
116 elif re.search(ur"^○", line):
118 line = re.sub(ur"^○(.*)$", ur'<b id="%s">\1</b>' % anchor, line)
121 line = re.sub(ur"^○(.*)$", ur"<b>\1</b>", line)
124 elif re.search(ur"^☆----", line):
125 line = re.sub(ur"☆----.*-{0,1}", u"<hr>", 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):
141 if re.search(ur"^ ", line):
142 line = "<p>" + line + "</p>"
144 if re.search(r"^\s*$", line):
152 def load_index(self, path_to_index):
156 @param path_to_index: index db's path
157 @type path_to_index: string
161 index_file = open(path_to_index, "r")
162 self._index_past = pickle.load(index_file)
165 sys.stderr.write("warn: cannot read index file,\n")
167 def save_index(self, path_to_index):
171 @param path_to_index: index db's path
172 @type path_to_index: string
176 index_file = open(path_to_index, "w")
177 pickle.dump(self._index, index_file)
180 sys.stderr.write("warn: cannot write index file,\n")
182 def make_hashlist(self, path_to_hashfile):
186 @param path_to_hashfile: hashfile's path
187 @type path_to_hashfile: string
190 file_img_hash = open(path_to_hashfile, "r")
192 sys.stderr.write("cannot open file: %s" % path_img_hash)
196 for line in file_img_hash:
197 splited = line.strip().split("\t", 2)
198 # hashlist's format: <hash> \t <filename>
199 self.hashlist[splited[1]] = splited[0]
201 def _call_tagles(self):
204 def _default_markup_rule(self, line):
206 apply default markup rules.
208 @param line: string to apply markup
211 # line = re.sub(ur"&", ur"&", line)
212 # line = re.sub(ur"<", ur"<", line)
213 # line = re.sub(ur">", ur">", line)
216 # line = tag_filter.apply(line)
218 line = re.sub(ur"[★*](表[0-9~]+)", ur"<b>\1</b>", line)
219 line = re.sub(ur"[★*](図[0-9~]+)", ur"<b>\1</b>", line)
220 line = re.sub(ur"[★*](リスト[0-9~]+)", ur"<b>\1</b>", line)
221 line = re.sub(ur"[★*]b\[(.*?)\]", ur"<b>\1</b>", line)
222 line = re.sub(ur"[★*]\[(\S*) (.*?)\]", r'<a href="\1">\2</a>', line)
225 if re.search(ur"^☆#", line):
231 def _ulist(self, line):
234 while re.search(ur"^・", line):
235 print re.sub(ur"^・(.*)$", ur"<li>\1</li>", line.strip())
236 line = self.input_iter.next()
239 def _begin_column(self, line):
240 """Proccess column"""
242 str_title = re.search(ur"^☆begin-column:(.*)$", line).group(1)
243 except AttributeError:
246 html = """<table bgcolor="#DDDDDD" border="0" cellpadding="6" width="95%%">
248 <tr><td><span style="font-size: 85%%;">
252 def _end_column(self, line):
253 print """</span></td></tr>
257 def _list_start(self):
263 def _list(self, line):
265 str_title = re.search("^☆(リスト.*)$", line).group(1)
266 except AttributeError:
268 print "<p><b>%s</b></p>" % (str_title)
269 print self._list_start(line)
271 for line in self.input_iter:
273 line = line.replace("&", "&")
274 line = line.replace("<", "<")
275 line = line.replace(">", ">")
279 print self._list_end()
281 def _code(self, line):
282 print self._list_start()
284 for line in self.input_iter:
285 # line = line.strip()
286 line = line.replace("&", "&")
287 line = line.replace("<", "<")
288 line = line.replace(">", ">")
289 line = line = re.sub(ur"[★*]b\[(.*?)]", ur"<b>\1</b>", line)
291 if re.search(ur"^☆\+---$", line):
294 print self._list_end()
297 def _inline(self, line):
298 for line in self.input_iter:
299 # line = line.strip()
300 if re.search(ur"^☆}}}", line):
305 def _comment(self, line):
306 for line in self.input_iter:
308 if re.search(ur"^☆}}}", line):
311 def _space(self, line):
315 def _fig_start(self, cap=""):
316 return """<table align="center" border="0" cellpadding="0" cellspacing="0">
317 <tr> <td valign="top" align="center">
320 def _fig_end(self, cap=""):
321 return """</td> </tr>
322 <tr> <td><span style="font-size: 80%%; font-weight: bold;">
328 def _fig(self, line):
329 if self._release == 1:
330 self._fig_release(line)
335 def _fig(self, line):
337 str_title = re.search(ur"^☆(図.*)$", line).group(1)
338 except AttributeError:
340 print self._fig_start()
342 line = self.input_iter.next()
347 match_o1 = re.search(ur"<([^,]*?)>", line)
348 match_o2 = re.search(ur"<(.*?),\s*(.*?)>", line)
349 if not match_o1 == None:
350 imgname = match_o1.group(1)
351 imgname_s = re.sub(r"(.[A-Za-z0-9_]+)$", r"_s\1", match_o1.group(1))
352 elif not match_o2 == None:
353 imgname = match_o1.group(1)
354 imgname_s = match_o1.group(2)
356 if not os.path.isfile(imgname_s):
359 print """<a href="%s">
360 <img src="%s" alt="%s">
362 """ % (imgname, imgname_s, str_title)
365 dic = self.index("figs")
368 dic.append(imgname_s)
370 print self._fig_end(str_title);
373 def _fig_release(self, line):
375 str_title = re.search(ur"^☆(図.*)$", line).group(1)
376 except AttributeError:
378 print self._fig_start()
380 line = self.input_iter.next()
385 match_o1 = re.search(ur"<([^,]*?)>", line)
386 match_o2 = re.search(ur"<(.*?),\s*(.*?)>", line)
387 if not match_o1 == None:
388 imgname = match_o1.group(1)
389 imgname_s = re.sub(r"(.[A-Za-z0-9_]+)$", r"_s\1", match_o1.group(1))
390 elif not match_o2 == None:
391 imgname = match_o1.group(1)
392 imgname_s = match_o1.group(2)
395 hash = self.hashlist.get(imgname, "")
396 hash_s = self.hashlist.get(imgname_s, "")
400 print """<a href="/blob.pl?id=%s">
401 <slash type="image" id="%s" title="%s">
403 """ % (hash, hash_s, str_title)
406 dic = self.index("figs")
409 dic.append(imgname_s)
411 print self._fig_end(str_title);
414 def _table_start(self, cap):
415 return """<table align="center" border="1" width="90%%">
416 <caption><b>%s</b></caption>
419 def _table_end(self):
422 def _table(self, line):
424 self._table_buf1 = ""
427 str_title = re.search(ur"^☆(表.*)$", line).group(1)
428 fig_name = re.search(ur"^☆(表[0-9A-Z]+)", line).group(1)
429 except AttributeError:
432 print self._table_start(str_title)
433 self._table_buf1 = self._table_start(str_title)
437 for line in self.input_iter:
438 line = line.strip(" \n")
439 if re.search(ur"^\s*$", line):
441 line = self._default_markup_rule(line)
442 if re.search(ur"^〓", line):
443 line = re.sub(ur"^〓", "", line)
447 table_contents.append([])
449 for item in line.split("\t"):
454 while table_contents[num_row-n][num_col]["item"] == "":
456 table_contents[num_row-n][num_col]["row"] += 1
462 while table_contents[num_row][num_col-n]["item"] == "":
464 table_contents[num_row][num_col-n]["col"] += 1
468 table_contents[num_row].append({"tag":tag_mode,"item":item,"row":1,"col":1})
469 num_col = num_col + 1
470 num_row = num_row + 1
472 for row_item in table_contents:
474 for item in row_item:
475 if item["item"] == "":
477 line = line + "<" + item["tag"]
478 if not item["row"] == 1:
479 line = line + (' rowspan="%s"' % item["row"])
480 if not item["col"] == 1:
481 line = line + (' colspan="%s"' % item["col"])
483 line = line + item["item"]
484 line = line + "</" + item["tag"] + ">"
485 line = line + "</tr>\n"
487 self._table_buf1 = self._table_buf1 + line
489 # line = "<tr><th>" + re.sub(ur"^〓", "", line) + "</th></tr>"
490 # line = line.replace("\t", "</th><th>")
493 # line = "<tr><td>" + line + "</td></tr>"
494 # line = line.replace("\t", "</td><td>")
497 print self._table_end()
498 self._table_buf1 = self._table_buf1 + self._table_end()
499 if self.index_haskey("tables"):
500 self.index("tables")[fig_name] = self._table_buf1
502 self.index_add("tables", {fig_name:self._table_buf1})
504 def _call_tables(self, line):
506 fig_name = re.search(ur"^☆call_tables\((表[0-9A-Z]+)", line).group(1)
507 except AttributeError:
509 print self.index("tables")[fig_name]