Django

Code

DjangoDocumentKoreanTranslation: wiki_formatter.py

File wiki_formatter.py, 6.9 kB (added by spike, 5 months ago)
Line 
1 # -*- coding: utf-8 -*-
2 """
3  Copyright 2005 Spike^ekipS <spikeekips@gmail.com>
4
5     This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  GNU General Public License for more details.
14
15  You should have received a copy of the GNU General Public License
16  along with this program; if not, write to the Free Software
17  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18 """
19
20 import sys, re
21
22 try :
23     pagename = sys.argv[2]
24 except :
25     pagename = ""
26
27 PAGE_URL = "DjangoDocumentKoreanTranslation"
28
29 RE_HEAD = re.compile("^==[=][=]*$")
30 RE_HEAD0 = re.compile("^\-\-[\-][\-]*$")
31 RE_HEAD1 = re.compile("^~~[~][~]*$")
32 RE_HEAD2 = re.compile("^\*\*(?P<strings>.*):\*\*$")
33 RE_LINK = re.compile("(?P<linkcode>!?`(?P<link>.*?)`_)")
34 RE_LINK_TARGET = re.compile("[ ]*\.\. _(?P<name>.*): (?P<target>.*)")
35 RE_MONOSPACE = re.compile("(?P<inlinecode>!?``(?P<inline>.*?)``)")
36 RE_DEFINITION = re.compile("[ ]*\.\..*:: (?P<title>.*)$")
37 RE_LIST = re.compile("^[ ][ ]*(?P<s>\*)")
38 RE_PREFIX_BLANK = re.compile("^[ ][ ]*")
39 RE_DASH = re.compile("[ ][ ]*\-\-[ ][ ]*")
40 RE_BOLD = re.compile("(?P<s_orig>!?\*\*(?P<s>.*?)\*\*)")
41 RE_LINK_INTERNAL = re.compile("(?P<inlinecode>!?_`(?P<inline>.*?)`)")
42 RE_IMAGE = re.compile("^.. image:: (?P<src>.*)")
43 RE_IMAGE_ALT = re.compile("^   :(?P<k>(alt|target)): (?P<v>.*)")
44
45 TEMPLATE_WIKI_LINK_INSIDE = "[wiki:%(target)s %(name)s]"
46 TEMPLATE_WIKI_LINK_OUTSIDE = "[%(target)s %(name)s]"
47
48 n = 0
49 lines = list()
50
51 links = list()
52 link_targets = dict()
53 found_code_block = False
54 found_list = None
55 found_list_newline = False
56 found_list_newline_inline = False
57 found_image = False
58 data_image = {"alt" : "", "src": "", "target": "", }
59
60 lines_code = list()
61 for i in file(sys.argv[1]) :
62
63     n += 1
64
65     # list
66     # check is in list block
67     if found_list is not None :
68         if RE_LIST.findall(i) :
69             found_list_newline = False
70             found_list = i.index("*")
71         else :
72             if RE_PREFIX_BLANK.findall(i) and ( (found_list) == len(RE_PREFIX_BLANK.findall(i)[0]) or (found_list + 2) == len(RE_PREFIX_BLANK.findall(i)[0]) ) :
73                 if found_list_newline :
74                     found_list_newline = False
75                     found_list_newline_inline = len(RE_PREFIX_BLANK.findall(i)[0])
76                     i = re.compile("^  ").sub("", i)
77                 elif found_list_newline_inline == len(RE_PREFIX_BLANK.findall(i)[0]) :
78                     i = re.compile("^  ").sub("", i)
79
80             elif i.strip() :
81                 found_list_newline = False
82                 found_list = None
83             elif not i.strip() :
84                 found_list_newline = True
85
86     if RE_LIST.findall(i) :
87         found_list_newline = False
88         found_list = i.index("*")
89
90     if found_code_block :
91         if i.strip() and not re.compile("^[ ][ ]*").findall(i) :
92             #i = "}}}\n" + i
93             lines.append("{{{\n%s\n}}}" % "".join(lines_code).strip())
94             found_code_block = False
95         else :
96             lines_code.append(re.compile("^    ").sub("", i))
97             continue
98
99     if re.compile("::$").findall(i) :
100         found_code_block = True
101         lines_code = list()
102         i = re.compile("::$").sub(":", i)
103
104     if RE_MONOSPACE.findall(i) :
105         for j in RE_MONOSPACE.finditer(i) :
106             inline = j.groupdict().get("inline")
107             if inline.endswith("}") and inline.startswith("{") :
108                 inline = " %s " % inline
109
110             i = i.replace(j.groupdict().get("inlinecode"), "{{{%s}}}" % inline)
111
112
113     if RE_HEAD.findall(i) :
114         if len(lines) > 0 :
115             lines[-1] = "= %s =" % lines[-1]
116         continue
117
118     if RE_HEAD0.findall(i) :
119         if len(lines) > 1 :
120             lines[-1] = "== %s ==" % lines[-1]
121         continue
122
123     if RE_HEAD1.findall(i) :
124         if len(lines) > 1 :
125             lines[-1] = "=== %s ===" % lines[-1]
126         continue
127
128     if found_image and RE_IMAGE_ALT.findall(i) :
129         a = RE_IMAGE_ALT.search(i).groupdict()
130         data_image.update({a["k"]: a["v"].replace("\"", "'")})
131         continue
132     elif RE_IMAGE.findall(i) :
133         found_image = True
134         data_image = {"alt" : "", "src": "", "target": "", }
135         data_image.update(RE_IMAGE.search(i).groupdict())
136         continue
137
138     if RE_DEFINITION.findall(i) :
139         i = " %s::" % RE_DEFINITION.search(i).groupdict().get("title")
140
141     if RE_HEAD2.findall(i) :
142         i = "'''%s''':" % RE_HEAD2.search(i).groupdict()["strings"]
143
144     if RE_LINK_TARGET.findall(i) :
145         name = RE_LINK_TARGET.search(i).groupdict().get("name")
146         target = RE_LINK_TARGET.search(i).groupdict().get("target")
147         link_targets[name] = target
148         continue
149
150     # wiki dash
151     if RE_DASH.findall(i) :
152         i = RE_DASH.sub(" - ", i)
153
154     if RE_BOLD.findall(i) :
155         for j in RE_BOLD.finditer(i) :
156             i = i.replace(
157                 j.groupdict().get("s_orig"),
158                 "'''%s'''" % j.groupdict().get("s")
159             )
160
161     if RE_LINK_INTERNAL.findall(i) :
162         for j in RE_LINK_INTERNAL.finditer(i) :
163             i = i.replace(
164                 j.groupdict().get("inlinecode"),
165                 "[wiki: %s/%s#%s %s]" % (
166                     PAGE_URL,
167                     pagename,
168                     j.groupdict().get("inline"),
169                     j.groupdict().get("inline")
170                 ),
171             )
172
173     if found_image and not i.strip():
174         found_image = False
175         i = """{{{
176 #!html
177 <a target=\"%(target)s\" href=\"%(target)s\"><img src=\"%(src)s\" alt=\"%(alt)s\" /></a>
178 }}}
179 """ % data_image
180
181     lines.append(i.rstrip())
182
183     if RE_LINK.findall(i) :
184         for j in RE_LINK.finditer(i) :
185             links.append((j.groupdict(), len(lines) - 1, ))
186
187 # create wiki link to target
188 for i in links :
189     name = i[0].get("link")
190     name_orig = i[0].get("linkcode")
191     n = i[1]
192     if link_targets.has_key(name) :
193         target = link_targets[name]
194
195         if target.startswith("http://") :
196             __template = TEMPLATE_WIKI_LINK_OUTSIDE
197         else :
198             __template = TEMPLATE_WIKI_LINK_INSIDE
199
200         if target.startswith("../") :
201             target = "%s/%s" % (PAGE_URL, target.replace("../", ""), )
202
203         lines[n] = lines[n].replace(
204             name_orig,
205             __template % {"name" : name, "target": target}
206         )
207
208 if found_code_block :
209     lines.append("}}}")
210
211 print "[[PageOutline]]\n"
212 print "[wiki:%s 장고 도움말 페읎지로 가Ʞ]\n\n" % PAGE_URL
213
214 for i in lines :
215     print i
216
217
218 """
219 Description
220 -----------
221
222
223 ChangeLog
224 ---------
225
226
227 Usage
228 -----
229
230
231 """
232
233 __author__ =  "Spike^ekipS <spikeekips@gmail.com>"
234 __version__=  "0.1"
235 __nonsense__ = ""