OSDN Git Service

add wp_imgswap2.py for new OSDN Magazine
[otptools/otptools.git] / wikimarkupper.py
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3  
4 import sys
5 import os
6 import re
7 import codecs
8 import pickle
9 import markupper
10 import HTMLTagFilter
11
12 #sys.stdin = codecs.getreader('utf_8')(sys.stdin)
13 #sys.stdout = codecs.getwriter('utf_8')(sys.stdout)
14
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)
20             self._anchor = ""
21         else:
22             line = re.sub(ur"^●(.*)$", ur"== \1 ==", line)
23         print line
24
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)
29             self._anchor = ""
30         else:
31             line = re.sub(ur"^○(.*)$", ur"=== \1 ===", line)
32         print line
33
34     def _paragraph(self, line):
35         line = self._default_markup_rule(line)
36         print line,
37
38     def _newpage(self, line):
39         pass
40
41     def _default_markup_rule(self, line):
42         """
43         apply default markup rules.
44
45         @param line: string to apply markup
46         @type line: string
47         """
48         # line = re.sub(ur"&", ur"&amp", line)
49         # line = re.sub(ur"<", ur"&lt;", line)
50         # line = re.sub(ur">", ur"&gt;", line)
51
52         # apply filter
53         # line = tag_filter.apply(line)
54
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)
62
63         return line
64
65
66     def _ulist(self, line):
67         """Proccess ul"""
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()
72
73     def _begin_column(self, line):
74         """Proccess column"""
75         try:
76             str_title = re.search(ur"^☆begin-column:(.*)$", line).group(1)
77         except AttributeError:
78             str_title = ""
79
80         html = """=== %s ===""" % (str_title)
81         print html
82
83     def _end_column(self, line):
84         pass
85
86     def _list_start(self):
87         return "{{{"
88
89     def _list_end(self):
90         return "}}}"
91
92     def _list(self, line):
93         try:
94             str_title = re.search(ur"^☆(リスト.*)$", line).group(1)
95         except AttributeError:
96             str_title = ""
97         print "'''%s'''" % (str_title)
98         print self._list_start()
99
100         for line in self.input_iter:
101             line = line.strip("\n\r")
102             if re.search(ur"""^☆\+---""", line):
103                 break
104             print line
105         print self._list_end()
106
107     def _code(self, line):
108         print self._list_start()
109
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)
114
115             if re.search(ur"^☆\+---$", line):
116                 break
117             print line,
118         print self._list_end()
119
120
121     def _inline(self, line):
122         print "{{{ html"
123         for line in self.input_iter:
124             #        line = line.strip()
125             if re.search(ur"^☆}}}", line):
126                 break
127             print line,
128         print "}}}"
129
130     def _comment(self, line):
131         for line in self.input_iter:
132             line = line.strip()
133             if re.search(ur"^☆}}}", line):
134                 break
135
136     def _space(self, line):
137         print "[[BR]]"
138
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;">
142 """
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;">
144 %s
145 </div>"""
146         flow_footer = """</div>
147 }}}
148 """
149         flow_item = """<div>
150 }}}
151 %s
152 {{{ html
153 </div>
154
155 """
156         down_arrow = "http://static.sourceforge.jp/crystal/22x22/actions/1downarrow.png"
157
158         rex_title = re.compile(ur"^☆flow\s+(.*)$")
159         if rex_title.search(line):
160             title = rex_title.search(line).group(1)
161         else:
162             title = ""
163
164         rex_file = re.compile(ur"^([^:]*):(.*)$")
165         outputs = []
166         for line in self.input_iter:
167             if re.search(r"^\s*$", line):
168                 break
169             match = rex_file.search(line)
170             if match:
171                 #file = os.path.join(self._image_dir, match.group(1))
172                 file = match.group(1)
173                 cap = match.group(2)
174             else:
175                 continue
176             fig = self._anchored_fig(file, cap)
177             outputs.append(flow_item % (fig))
178
179         arrow = '<div style="margin: 1em auto;"><img src="%s"></div>\n' % (down_arrow,)
180         print flow_header
181         print flow_title % (title,)
182         print arrow.join(outputs)
183         print flow_footer
184         
185
186     def _fig_start(self, cap=""):
187         return ""
188
189     def _fig_end(self, cap=""):
190         return ""
191
192     def _fig(self, line):
193         if self._release == 1:
194             self._fig_release(line)
195         else:
196             self._fig_org(line)
197
198
199     def _fig_org(self, line):
200         try:
201             str_title = re.search(ur"^☆(図.*)$", line).group(1)
202         except AttributeError:
203             str_title = ""
204         if str_title.find(u"図*") == 0:
205             str_title = str_title.replace(u"図*", "")
206         print self._fig_start()
207
208         line = self.input_iter.next()
209         imgname = ""
210         imgname_s = ""
211         hash = ""
212         hash_s = ""
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)
223
224         print self._anchored_fig(imgname, str_title, imgname_s)
225
226 #         if not os.path.isfile(imgname_s):
227 #             imgname_s = imgname
228
229 #         print """<a href="%s">
230 #      <img src="%s" alt="%s">
231 # </a>
232 #      """ % (imgname, imgname_s, str_title)
233
234         dic = self.index("figs")
235         dic.append(imgname)
236         if imgname_s != "":
237             dic.append(imgname_s)
238
239         print self._fig_end(str_title);
240         
241     def _anchored_fig(self, file, alt, file_s=""):
242
243         if file_s == "":
244             file_s = re.sub(r"(.[A-Za-z0-9_]+)$", r"_s\1", file)
245
246         if not os.path.isfile(file_s):
247             file_s = file
248
249         return """[[Thumb(%s, size=large, caption=%s)]]""" % (file, alt)
250         
251
252     def _fig_release(self, line):
253         try:
254             str_title = re.search(ur"^☆(図.*)$", line).group(1)
255         except AttributeError:
256             str_title = ""
257         print self._fig_start()
258
259         line = self.input_iter.next()
260         imgname = ""
261         imgname_s = ""
262         hash = ""
263         hash_s = ""
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)
272
273
274         hash = self.hashlist.get(imgname, "")
275         hash_s = self.hashlist.get(imgname_s, "")
276         if hash_s == "":
277             hash_s = hash
278
279         print """<a href="/blob.pl?id=%s">
280      <slash type="image" id="%s" title="%s">
281      </a>
282      """ % (hash, hash_s, str_title)
283         
284
285         dic = self.index("figs")
286         dic.append(imgname)
287         if imgname_s != "":
288             dic.append(imgname_s)
289
290         print self._fig_end(str_title);
291
292
293     def _table_start(self, cap):
294         return "'''%s'''" % (cap,)
295
296     def _table_end(self, footnote=""):
297         return "%s" % (footnote,)
298
299     def _table(self, line):
300         str_title = ""
301         self._table_buf1 = ""
302
303         try:
304             str_title = re.search(ur"^☆(表.*)$", line).group(1)
305             fig_name =  re.search(ur"^☆(表[0-9A-Z]*)", line).group(1)
306         except AttributeError:
307             str_title = ""
308             fig_name = ""
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"
313
314         num_row = 0
315         table_contents = []
316         footnote = ""
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):
321                 break
322             if re.search(ur"^※", line):
323                 footnote = re.search(ur"^(※.*)$", line).group(1)
324                 break
325             line = self._default_markup_rule(line)
326             if re.search(ur"^〓", line):
327                 line = re.sub(ur"^〓", "", line)
328                 tag_mode = "th"
329             else:
330                 tag_mode = "td"
331             self._table_buf1 = self._table_buf1 + "||"
332             for item in line.split("\t"):
333                 if tag_mode == "th":
334                     self._table_buf1 = self._table_buf1 + "'''%s'''||" % (item,)
335                 else:
336                     self._table_buf1 = self._table_buf1 + "%s||" % (item,)
337             self._table_buf1 = self._table_buf1 + "\n"
338
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
343         else:
344             self.index_add("tables", {fig_name:self._table_buf1})
345
346     def _call_tables(self, line):
347         try:
348             fig_name =  re.search(ur"^☆call_tables\((表[0-9A-Z]+)", line).group(1)
349         except AttributeError:
350             return
351         print self.index("tables")[fig_name]