comparison MoinMoin/script/migration/_conv160_wiki.py @ 2608:3ff3beef1edf

1.6 converter: add more tests, fixed bugs, better quoting, cleanup
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Sun, 05 Aug 2007 19:19:49 +0200
parents 482f0a9e555c
children 7aac9e2ecb3a
comparison
equal deleted inserted replaced
2607:2e6227a50d27 2608:3ff3beef1edf
107 s_word = word.strip() 107 s_word = word.strip()
108 self.in_pre = True 108 self.in_pre = True
109 return origw 109 return origw
110 110
111 # LINKS ------------------------------------------------------------------ 111 # LINKS ------------------------------------------------------------------
112 def _intelli_quote(self, name):
113 quote_triggers = ' "()' # XXX add more
114 quote_it = [True for c in quote_triggers if c in name]
115 if quote_it:
116 return wikiutil.quoteName(name)
117 else:
118 return name
112 119
113 def _replace_target(self, target): 120 def _replace_target(self, target):
114 target_and_anchor = target.split('#', 1) 121 target_and_anchor = target.split('#', 1)
115 if len(target_and_anchor) > 1: 122 if len(target_and_anchor) > 1:
116 target, anchor = target_and_anchor 123 target, anchor = target_and_anchor
119 else: 126 else:
120 target = self._replace(('PAGE', target)) 127 target = self._replace(('PAGE', target))
121 return target 128 return target
122 129
123 def interwiki(self, target_and_text, **kw): 130 def interwiki(self, target_and_text, **kw):
124 # TODO: maybe support [wiki:Page http://wherever/image.png] ?
125 scheme, rest = target_and_text.split(':', 1) 131 scheme, rest = target_and_text.split(':', 1)
126 wikiname, pagename, text = wikiutil.split_wiki(rest) 132 wikiname, pagename, text = wikiutil.split_wiki(rest)
127 #self.request.log("interwiki: split_wiki -> %s.%s.%s" % (wikiname,pagename,text)) 133 if wikiname in ('Self', self.request.cfg.interwikiname): # our wiki
128
129 wikiname_lower = wikiname.lower()
130 if wikiname_lower == 'self' or wikiname_lower == self.request.cfg.interwikiname: # [wiki:Self:LocalPage text] or [:LocalPage:text]
131 pagename = self._replace(('PAGE', pagename)) 134 pagename = self._replace(('PAGE', pagename))
132 if not text: 135 #pagename = pagename.replace('_', ' ') # better not touch this
133 return '[%s]' % wikiutil.quoteName(pagename) # ["LocalPage"] 136 pagename = wikiutil.url_unquote(pagename)
134 else: 137 return scheme, wikiname, pagename, text
135 return '[%s %s]' % (wikiutil.quoteName(pagename), text) # ["LocalPage" text]
136
137 # check for image URL, and possibly return IMG tag
138 if not kw.get('pretty_url', 0) and wikiutil.isPicture(pagename):
139 dummy, wikiurl, dummy, wikitag_bad = wikiutil.resolve_wiki(self.request, rest)
140 href = wikiutil.join_wiki(wikiurl, pagename)
141 #self.request.log("interwiki: join_wiki -> %s.%s.%s" % (wikiurl,pagename,href))
142 return target_and_text # self.formatter.image(src=href)
143
144 return target_and_text # wikiname, pagename, text
145 138
146 def attachment(self, target_and_text, **kw): 139 def attachment(self, target_and_text, **kw):
147 """ This gets called on attachment URLs """ 140 """ This gets called on attachment URLs """
148 _ = self._ 141 _ = self._
149 #self.request.log("attachment: target_and_text %s" % target_and_text)
150 scheme, fname, text = wikiutil.split_wiki(target_and_text) 142 scheme, fname, text = wikiutil.split_wiki(target_and_text)
151 pagename, fname = AttachFile.absoluteName(fname, self.pagename) 143 pagename, fname = AttachFile.absoluteName(fname, self.pagename)
152 from_this_page = pagename == self.pagename 144 from_this_page = pagename == self.pagename
153 fname = self._replace(('FILE', pagename, fname)) 145 fname = self._replace(('FILE', pagename, fname))
154 if '%20' in fname: 146 if '%20' in fname:
157 pagename = self._replace(('PAGE', pagename)) 149 pagename = self._replace(('PAGE', pagename))
158 if from_this_page: 150 if from_this_page:
159 name = fname 151 name = fname
160 else: 152 else:
161 name = "%s/%s" % (pagename, fname) 153 name = "%s/%s" % (pagename, fname)
162 if ' ' in name: 154 name = self._intelli_quote(name)
163 qname = wikiutil.quoteName(name)
164 else:
165 qname = name
166
167 if text: 155 if text:
168 text = ' ' + text 156 text = ' ' + text
169 return "%s:%s%s" % (scheme, qname, text) 157 return "%s:%s%s" % (scheme, name, text)
170 158
171 def _interwiki_repl(self, word): 159 def _interwiki_repl(self, word):
172 """Handle InterWiki links.""" 160 """Handle InterWiki links."""
173 wikitag, wikiurl, wikitail, wikitag_bad = wikiutil.resolve_wiki(self.request, word) 161 wikitag, wikiurl, wikitail, wikitag_bad = wikiutil.resolve_wiki(self.request, word)
174 if wikitag_bad: 162 if wikitag_bad:
175 return word 163 return word
176 else: 164 else:
177 result = self.interwiki("wiki:" + word) 165 scheme, wikiname, pagename, text = self.interwiki("wiki:" + word)
178 if result.startswith("wiki:"): 166 if wikiname == 'Self':
179 result = result[5:] 167 return '["%s"]' % pagename # optimize special case
180 return result 168 else:
169 pagename = self._intelli_quote(pagename)
170 return "%s:%s" % (wikiname, pagename)
181 171
182 def _url_repl(self, word): 172 def _url_repl(self, word):
183 """Handle literal URLs including inline images.""" 173 """Handle literal URLs including inline images."""
184 scheme = word.split(":", 1)[0] 174 scheme = word.split(":", 1)[0]
185 175
186 if scheme == "wiki": 176 if scheme == "wiki":
187 return self.interwiki(word) 177 scheme, wikiname, pagename, text = self.interwiki(word)
178 if wikiname == 'Self':
179 return '["%s"]' % pagename # optimize special case
180 else:
181 pagename = self._intelli_quote(pagename)
182 return "%s:%s:%s" % (scheme, wikiname, pagename)
188 183
189 if scheme in self.attachment_schemas: 184 if scheme in self.attachment_schemas:
190 return self.attachment(word) 185 return self.attachment(word)
191 186
192 if wikiutil.isPicture(word): 187 if wikiutil.isPicture(word):
210 target = wikiutil.quoteName(target) 205 target = wikiutil.quoteName(target)
211 else: # not quoted 206 else: # not quoted
212 # split on whitespace 207 # split on whitespace
213 target, linktext = word.split(None, 1) 208 target, linktext = word.split(None, 1)
214 target = self._replace_target(target) 209 target = self._replace_target(target)
215 if ' ' in target: 210 target = self._intelli_quote(target)
216 target = wikiutil.quoteName(target)
217 if linktext: 211 if linktext:
218 linktext = ' ' + linktext 212 linktext = ' ' + linktext
219 return '[%s%s]' % (target, linktext) 213 return '[%s%s]' % (target, linktext)
220 214
221 215
240 234
241 scheme_and_rest = word.split(":", 1) 235 scheme_and_rest = word.split(":", 1)
242 if len(scheme_and_rest) == 2: # scheme given 236 if len(scheme_and_rest) == 2: # scheme given
243 scheme, rest = scheme_and_rest 237 scheme, rest = scheme_and_rest
244 if scheme == "wiki": 238 if scheme == "wiki":
245 return self.interwiki(word, pretty_url=1) 239 scheme, wikiname, pagename, text = self.interwiki(word, pretty_url=1)
240 if wikiname == 'Self':
241 if text:
242 text = ' %s' % text
243 return '["%s"%s]' % (pagename, text)
244 else:
245 pagename = self._intelli_quote(pagename)
246 if text:
247 text = ' %s' % text
248 return "[%s:%s:%s%s]" % (scheme, wikiname, pagename, text)
246 if scheme in self.attachment_schemas: 249 if scheme in self.attachment_schemas:
247 return self.attachment(word, pretty_url=1) 250 return '[%s]' % self.attachment(word, pretty_url=1)
248 251
249 words = word.split(None, 1) 252 words = word.split(None, 1)
250 if len(words) == 1: 253 if len(words) == 1:
251 link, text = words[0], '' 254 link, text = words[0], ''
252 else: 255 else: