2 # -*- coding: utf-8 -*-
12 #sys.stdin = codecs.getreader('utf_8')(sys.stdin)
13 #sys.stdout = codecs.getwriter('utf_8')(sys.stdout)
15 class WikiMarkupper(markupper.Markupper):
16 def _head_l(self, line):
17 line = self._default_markup_rule(line)
18 if self._anchor != "":
19 line = re.sub(ur"^●(.*)$", ur'== \1 == #%s' % self._anchor, line)
22 line = re.sub(ur"^●(.*)$", ur"== \1 ==", line)
25 def _head_m(self, line):
26 line = self._default_markup_rule(line)
27 if self._anchor != "":
28 line = re.sub(ur"^○(.*)$", ur'=== \1 === #%s' % self._anchor, line)
31 line = re.sub(ur"^○(.*)$", ur"=== \1 ===", line)
34 def _paragraph(self, line):
35 line = self._default_markup_rule(line)
38 def _newpage(self, line):
41 def _default_markup_rule(self, line):
43 apply default markup rules.
45 @param line: string to apply markup
48 # line = re.sub(ur"&", ur"&", line)
49 # line = re.sub(ur"<", ur"<", line)
50 # line = re.sub(ur">", ur">", line)
53 # line = tag_filter.apply(line)
55 line = re.sub(ur"[★*](表[0-9~、]+)", ur"'''\1'''", line)
56 line = re.sub(ur"[★*](図[0-9~、]+)", ur"'''\1'''", line)
57 line = re.sub(ur"[★*](リスト[0-9~、]+)", ur"'''\1'''", line)
58 line = re.sub(ur"[★*]b\[(.*?)\]", ur"'''\1'''", line)
59 line = re.sub(ur"[★*]b\{(.*?)\}", ur"'''\1'''", line)
60 line = re.sub(ur"[★*]\[(\S*) (.*?)\]", r'[\1 \2]', line)
61 line = re.sub(ur"[★*]\[(\S*)\]", r'[\1]', line)
66 def _ulist(self, line):
68 while re.search(ur"^・", line):
69 line = self._default_markup_rule(line)
70 print re.sub(ur"^・(.*)$", ur" * \1", line.strip())
71 line = self.input_iter.next()
73 def _begin_column(self, line):
76 str_title = re.search(ur"^☆begin-column:(.*)$", line).group(1)
77 except AttributeError:
80 html = """=== %s ===""" % (str_title)
83 def _end_column(self, line):
86 def _list_start(self):
92 def _list(self, line):
94 str_title = re.search(ur"^☆(リスト.*)$", line).group(1)
95 except AttributeError:
97 print "'''%s'''" % (str_title)
98 print self._list_start()
100 for line in self.input_iter:
101 line = line.strip("\n\r")
102 if re.search(ur"""^☆\+---""", line):
105 print self._list_end()
107 def _code(self, line):
108 print self._list_start()
110 for line in self.input_iter:
111 # line = line.strip()
112 line = line = re.sub(ur"[★*]b\[(.*?)]", ur"\1", line)
113 line = line = re.sub(ur"[★*]b{(.*?)}", ur"\1", line)
115 if re.search(ur"^☆\+---$", line):
118 print self._list_end()
121 def _inline(self, line):
123 for line in self.input_iter:
124 # line = line.strip()
125 if re.search(ur"^☆}}}", line):
130 def _comment(self, line):
131 for line in self.input_iter:
133 if re.search(ur"^☆}}}", line):
136 def _space(self, line):
139 def _flow(self, line):
140 flow_header = """{{{ html
141 <div style="text-align:center; border: 1px solid; background-color:#EFF2F0; width:90%; margin: 0 auto 1em;">
143 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;">
146 flow_footer = """</div>
156 down_arrow = "http://static.sourceforge.jp/crystal/22x22/actions/1downarrow.png"
158 rex_title = re.compile(ur"^☆flow\s+(.*)$")
159 if rex_title.search(line):
160 title = rex_title.search(line).group(1)
164 rex_file = re.compile(ur"^([^:]*):(.*)$")
166 for line in self.input_iter:
167 if re.search(r"^\s*$", line):
169 match = rex_file.search(line)
171 #file = os.path.join(self._image_dir, match.group(1))
172 file = match.group(1)
176 fig = self._anchored_fig(file, cap)
177 outputs.append(flow_item % (fig))
179 arrow = '<div style="margin: 1em auto;"><img src="%s"></div>\n' % (down_arrow,)
181 print flow_title % (title,)
182 print arrow.join(outputs)
186 def _fig_start(self, cap=""):
189 def _fig_end(self, cap=""):
192 def _fig(self, line):
193 if self._release == 1:
194 self._fig_release(line)
199 def _fig_org(self, line):
201 str_title = re.search(ur"^☆(図.*)$", line).group(1)
202 except AttributeError:
204 if str_title.find(u"図*") == 0:
205 str_title = str_title.replace(u"図*", "")
206 print self._fig_start()
208 line = self.input_iter.next()
213 match_o1 = re.search(ur"<([^,]*?)>", line)
214 match_o2 = re.search(ur"<(.*?),\s*(.*?)>", line)
215 if not match_o1 == None:
216 imgname = match_o1.group(1)
217 #imgname = os.path.join(self._image_dir, imgname)
218 imgname_s = re.sub(r"(.[A-Za-z0-9_]+)$", r"_s\1", imgname)
219 elif not match_o2 == None:
220 imgname = match_o1.group(1)
221 #imgname = os.path.join(self._image_dir, imgname)
222 imgname_s = match_o1.group(2)
224 print self._anchored_fig(imgname, str_title, imgname_s)
226 # if not os.path.isfile(imgname_s):
227 # imgname_s = imgname
229 # print """<a href="%s">
230 # <img src="%s" alt="%s">
232 # """ % (imgname, imgname_s, str_title)
234 dic = self.index("figs")
237 dic.append(imgname_s)
239 print self._fig_end(str_title);
241 def _anchored_fig(self, file, alt, file_s=""):
244 file_s = re.sub(r"(.[A-Za-z0-9_]+)$", r"_s\1", file)
246 if not os.path.isfile(file_s):
249 return """[[Thumb(%s, size=large, caption=%s)]]""" % (file, alt)
252 def _fig_release(self, line):
254 str_title = re.search(ur"^☆(図.*)$", line).group(1)
255 except AttributeError:
257 print self._fig_start()
259 line = self.input_iter.next()
264 match_o1 = re.search(ur"<([^,]*?)>", line)
265 match_o2 = re.search(ur"<(.*?),\s*(.*?)>", line)
266 if not match_o1 == None:
267 imgname = match_o1.group(1)
268 imgname_s = re.sub(r"(.[A-Za-z0-9_]+)$", r"_s\1", match_o1.group(1))
269 elif not match_o2 == None:
270 imgname = match_o1.group(1)
271 imgname_s = match_o1.group(2)
274 hash = self.hashlist.get(imgname, "")
275 hash_s = self.hashlist.get(imgname_s, "")
279 print """<a href="/blob.pl?id=%s">
280 <slash type="image" id="%s" title="%s">
282 """ % (hash, hash_s, str_title)
285 dic = self.index("figs")
288 dic.append(imgname_s)
290 print self._fig_end(str_title);
293 def _table_start(self, cap):
294 return "'''%s'''" % (cap,)
296 def _table_end(self, footnote=""):
297 return "%s" % (footnote,)
299 def _table(self, line):
301 self._table_buf1 = ""
304 str_title = re.search(ur"^☆(表.*)$", line).group(1)
305 fig_name = re.search(ur"^☆(表[0-9A-Z]*)", line).group(1)
306 except AttributeError:
309 if str_title.find(u"表*") == 0:
310 str_title = str_title.replace(u"表*", "")
311 # print self._table_start(str_title)
312 self._table_buf1 = self._table_start(str_title) + "\n"
317 for line in self.input_iter:
318 line = line.strip(" \n")
319 line = self._default_markup_rule(line)
320 if re.search(ur"^\s*$", line):
322 if re.search(ur"^※", line):
323 footnote = re.search(ur"^(※.*)$", line).group(1)
325 line = self._default_markup_rule(line)
326 if re.search(ur"^〓", line):
327 line = re.sub(ur"^〓", "", line)
331 self._table_buf1 = self._table_buf1 + "||"
332 for item in line.split("\t"):
334 self._table_buf1 = self._table_buf1 + "'''%s'''||" % (item,)
336 self._table_buf1 = self._table_buf1 + "%s||" % (item,)
337 self._table_buf1 = self._table_buf1 + "\n"
339 self._table_buf1 = self._table_buf1 + self._table_end(footnote)
340 print self._table_buf1
341 if self.index_haskey("tables"):
342 self.index("tables")[fig_name] = self._table_buf1
344 self.index_add("tables", {fig_name:self._table_buf1})
346 def _call_tables(self, line):
348 fig_name = re.search(ur"^☆call_tables\((表[0-9A-Z]+)", line).group(1)
349 except AttributeError:
351 print self.index("tables")[fig_name]