comparison MoinMoin/script/migration/_conv160_wiki.py @ 2602:b601db2e4d34

1.6 converter: improve content conversion, add test for it
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Sat, 04 Aug 2007 21:28:22 +0200
parents 13f0331f3a42
children c61c10e3fcde
comparison
equal deleted inserted replaced
2601:29657277dd37 2602:b601db2e4d34
27 import re, codecs 27 import re, codecs
28 from MoinMoin import i18n 28 from MoinMoin import i18n
29 i18n.wikiLanguages = lambda : [] 29 i18n.wikiLanguages = lambda : []
30 from MoinMoin import config, wikiutil 30 from MoinMoin import config, wikiutil
31 from MoinMoin.parser.text_moin_wiki import Parser 31 from MoinMoin.parser.text_moin_wiki import Parser
32 from MoinMoin.action import AttachFile
32 33
33 class Converter(Parser): 34 class Converter(Parser):
34 def __init__(self, request, raw, renames): 35 def __init__(self, request, pagename, raw, renames):
35 self.request = request 36 self.request = request
37 self.pagename = pagename
36 self.raw = raw 38 self.raw = raw
37 self.renames = renames 39 self.renames = renames
38 self.in_pre = False 40 self.in_pre = False
39 self._ = None 41 self._ = None
40 42
41 def _replace(self, item_type, item_name): 43 def _replace(self, key):
42 """ replace a item_name if it is in the renames dict """ 44 """ replace a item_name if it is in the renames dict """
43 key = (item_type, item_name) 45 if key[0] == 'PAGE':
46 item_name = key[1] # pagename
47 elif key[0] == 'FILE':
48 item_name = key[2] # filename, key[1] is pagename
44 try: 49 try:
45 return self.renames[key] 50 return self.renames[key] # new pagename or new filename
46 except KeyError: 51 except KeyError:
47 return item_name 52 return item_name
48 53
49 def return_word(self, word): 54 def return_word(self, word):
50 return word 55 return word
108 113
109 def _replace_target(self, target): 114 def _replace_target(self, target):
110 target_and_anchor = target.split('#', 1) 115 target_and_anchor = target.split('#', 1)
111 if len(target_and_anchor) > 1: 116 if len(target_and_anchor) > 1:
112 target, anchor = target_and_anchor 117 target, anchor = target_and_anchor
113 target = self._replace('PAGE', target) 118 target = self._replace(('PAGE', target))
114 return '%s#%s' % (target, anchor) 119 return '%s#%s' % (target, anchor)
115 else: 120 else:
116 target = self._replace('PAGE', target) 121 target = self._replace(('PAGE', target))
117 return target 122 return target
118 123
119 def interwiki(self, target_and_text, **kw): 124 def interwiki(self, target_and_text, **kw):
120 # TODO: maybe support [wiki:Page http://wherever/image.png] ? 125 # TODO: maybe support [wiki:Page http://wherever/image.png] ?
121 scheme, rest = target_and_text.split(':', 1) 126 scheme, rest = target_and_text.split(':', 1)
123 if not text: 128 if not text:
124 text = pagename 129 text = pagename
125 #self.request.log("interwiki: split_wiki -> %s.%s.%s" % (wikiname,pagename,text)) 130 #self.request.log("interwiki: split_wiki -> %s.%s.%s" % (wikiname,pagename,text))
126 131
127 if wikiname.lower() == 'self': # [wiki:Self:LocalPage text] or [:LocalPage:text] 132 if wikiname.lower() == 'self': # [wiki:Self:LocalPage text] or [:LocalPage:text]
128 return target_and_text # self._word_repl(pagename, text) 133 return '[%s %s]' % (wikiutil.quoteName(pagename), text) # ["LocalPage" text]
129 134
130 # check for image URL, and possibly return IMG tag 135 # check for image URL, and possibly return IMG tag
131 if not kw.get('pretty_url', 0) and wikiutil.isPicture(pagename): 136 if not kw.get('pretty_url', 0) and wikiutil.isPicture(pagename):
132 dummy, wikiurl, dummy, wikitag_bad = wikiutil.resolve_wiki(self.request, rest) 137 dummy, wikiurl, dummy, wikitag_bad = wikiutil.resolve_wiki(self.request, rest)
133 href = wikiutil.join_wiki(wikiurl, pagename) 138 href = wikiutil.join_wiki(wikiurl, pagename)
139 def attachment(self, target_and_text, **kw): 144 def attachment(self, target_and_text, **kw):
140 """ This gets called on attachment URLs """ 145 """ This gets called on attachment URLs """
141 _ = self._ 146 _ = self._
142 #self.request.log("attachment: target_and_text %s" % target_and_text) 147 #self.request.log("attachment: target_and_text %s" % target_and_text)
143 scheme, fname, text = wikiutil.split_wiki(target_and_text) 148 scheme, fname, text = wikiutil.split_wiki(target_and_text)
144 if not text: 149 pagename, fname = AttachFile.absoluteName(fname, self.pagename)
145 text = fname 150 from_this_page = pagename == self.pagename
146 151 fname = self._replace(('FILE', pagename, fname))
147 if scheme == 'drawing': 152 if '%20' in fname:
148 return target_and_text # self.formatter.attachment_drawing(fname, text) 153 fname = fname.replace('%20', ' ')
149 154 fname = self._replace(('FILE', pagename, fname))
150 # check for image, and possibly return IMG tag (images are always inlined) 155 pagename = self._replace(('PAGE', pagename))
151 if not kw.get('pretty_url', 0) and wikiutil.isPicture(fname): 156 if from_this_page:
152 return target_and_text # self.formatter.attachment_image(fname) 157 name = fname
153 158 else:
154 # inline the attachment 159 name = "%s/%s" % (pagename, fname)
155 if scheme == 'inline': 160 if ' ' in name:
156 return target_and_text # self.formatter.attachment_inlined(fname, text) 161 qname = wikiutil.quoteName(name)
157 162 else:
158 return target_and_text # self.formatter.attachment_link(fname, text) 163 qname = name
159 164
165 if text:
166 text = ' ' + text
167 return "%s:%s%s" % (scheme, qname, text)
160 168
161 def _url_repl(self, word): 169 def _url_repl(self, word):
162 """Handle literal URLs including inline images.""" 170 """Handle literal URLs including inline images."""
163 scheme = word.split(":", 1)[0] 171 scheme = word.split(":", 1)[0]
164 172
184 first_char = word[0] 192 first_char = word[0]
185 if first_char in wikiutil.QUOTE_CHARS: 193 if first_char in wikiutil.QUOTE_CHARS:
186 # split on closing quote 194 # split on closing quote
187 target, linktext = word[1:].split(first_char, 1) 195 target, linktext = word[1:].split(first_char, 1)
188 target = self._replace_target(target) 196 target = self._replace_target(target)
189 return '[%s%s%s%s]' % (first_char, target, first_char, linktext) 197 target = wikiutil.quoteName(target)
190 else: # not quoted 198 else: # not quoted
191 # split on whitespace 199 # split on whitespace
192 target, linktext = word.split(None, 1) 200 target, linktext = word.split(None, 1)
193 target = target.replace("_", " ") 201 target = self._replace_target(target)
194 return '[%s%s%s]' % (target, word[len(target)], linktext) 202 if ' ' in target:
203 target = wikiutil.quoteName(target)
204 if linktext:
205 linktext = ' ' + linktext
206 return '[%s%s]' % (target, linktext)
195 207
196 208
197 def _url_bracket_repl(self, word): 209 def _url_bracket_repl(self, word):
198 """Handle bracketed URLs.""" 210 """Handle bracketed URLs."""
199 word = word[1:-1] # strip brackets 211 word = word[1:-1] # strip brackets
201 # Local extended link? [:page name:link text] 213 # Local extended link? [:page name:link text]
202 if word[0] == ':': 214 if word[0] == ':':
203 words = word[1:].split(':', 1) 215 words = word[1:].split(':', 1)
204 words[0] = self._replace_target(words[0]) 216 words[0] = self._replace_target(words[0])
205 if len(words) == 1: 217 if len(words) == 1:
206 return '[:%s]' % words[0] 218 link = words[0]
207 return '[:%s:%s]' % (words[0], words[1]) 219 link = wikiutil.quoteName(link)
220 return '[%s]' % link # use freelink
221 else:
222 link, text = words
223 link = wikiutil.quoteName(link)
224 if text:
225 text = ' ' + text
226 return '[%s%s]' % (link, text) # use freelink with text
208 227
209 return '[%s]' % word 228 return '[%s]' % word
210 229
211 230
212 # SCANNING --------------------------------------------------------------- 231 # SCANNING ---------------------------------------------------------------
305 # Scan line, format and write 324 # Scan line, format and write
306 scanning_re = self.in_pre and pre_scan_re or scan_re 325 scanning_re = self.in_pre and pre_scan_re or scan_re
307 formatted_line = self.scan(scanning_re, line) 326 formatted_line = self.scan(scanning_re, line)
308 self.request.write(formatted_line + '\r\n') 327 self.request.write(formatted_line + '\r\n')
309 328
310 def convert_wiki(intext, renames): 329 def convert_wiki(pagename, intext, renames):
311 """ Convert content written in wiki markup """ 330 """ Convert content written in wiki markup """
312 import StringIO 331 import StringIO
313 request = StringIO.StringIO() 332 request = StringIO.StringIO()
314 noeol = False 333 noeol = False
315 if not intext.endswith('\r\n'): 334 if not intext.endswith('\r\n'):
316 intext += '\r\n' 335 intext += '\r\n'
317 noeol = True 336 noeol = True
318 p = Converter(request, intext, renames) 337 p = Converter(request, pagename, intext, renames)
319 p.convert() 338 p.convert()
320 res = request.getvalue() 339 res = request.getvalue()
321 if noeol: 340 if noeol:
322 res = res[:-2] 341 res = res[:-2]
323 return res 342 return res