changeset 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 2e6227a50d27
children 7aac9e2ecb3a 117e1bee2444
files MoinMoin/script/migration/_conv160_wiki.py MoinMoin/script/migration/_tests/test_conv160_wiki.py
diffstat 2 files changed, 50 insertions(+), 37 deletions(-) [+]
line wrap: on
line diff
--- a/MoinMoin/script/migration/_conv160_wiki.py	Sun Aug 05 17:25:36 2007 +0200
+++ b/MoinMoin/script/migration/_conv160_wiki.py	Sun Aug 05 19:19:49 2007 +0200
@@ -109,6 +109,13 @@
         return origw
 
     # LINKS ------------------------------------------------------------------
+    def _intelli_quote(self, name):
+        quote_triggers = ' "()' # XXX add more
+        quote_it = [True for c in quote_triggers if c in name]
+        if quote_it:
+            return wikiutil.quoteName(name)
+        else:
+            return name
 
     def _replace_target(self, target):
         target_and_anchor = target.split('#', 1)
@@ -121,32 +128,17 @@
             return target
 
     def interwiki(self, target_and_text, **kw):
-        # TODO: maybe support [wiki:Page http://wherever/image.png] ?
         scheme, rest = target_and_text.split(':', 1)
         wikiname, pagename, text = wikiutil.split_wiki(rest)
-        #self.request.log("interwiki: split_wiki -> %s.%s.%s" % (wikiname,pagename,text))
-
-        wikiname_lower = wikiname.lower()
-        if wikiname_lower == 'self' or wikiname_lower == self.request.cfg.interwikiname: # [wiki:Self:LocalPage text] or [:LocalPage:text]
+        if wikiname in ('Self', self.request.cfg.interwikiname): # our wiki
             pagename = self._replace(('PAGE', pagename))
-            if not text:
-                return '[%s]' % wikiutil.quoteName(pagename) # ["LocalPage"]
-            else:
-                return '[%s %s]' % (wikiutil.quoteName(pagename), text) # ["LocalPage" text]
-
-        # check for image URL, and possibly return IMG tag
-        if not kw.get('pretty_url', 0) and wikiutil.isPicture(pagename):
-            dummy, wikiurl, dummy, wikitag_bad = wikiutil.resolve_wiki(self.request, rest)
-            href = wikiutil.join_wiki(wikiurl, pagename)
-            #self.request.log("interwiki: join_wiki -> %s.%s.%s" % (wikiurl,pagename,href))
-            return target_and_text # self.formatter.image(src=href)
-
-        return target_and_text # wikiname, pagename, text
+        #pagename = pagename.replace('_', ' ') # better not touch this
+        pagename = wikiutil.url_unquote(pagename)
+        return scheme, wikiname, pagename, text
 
     def attachment(self, target_and_text, **kw):
         """ This gets called on attachment URLs """
         _ = self._
-        #self.request.log("attachment: target_and_text %s" % target_and_text)
         scheme, fname, text = wikiutil.split_wiki(target_and_text)
         pagename, fname = AttachFile.absoluteName(fname, self.pagename)
         from_this_page = pagename == self.pagename
@@ -159,14 +151,10 @@
             name = fname
         else:
             name = "%s/%s" % (pagename, fname)
-        if ' ' in name:
-            qname = wikiutil.quoteName(name)
-        else:
-            qname = name
-
+        name = self._intelli_quote(name)
         if text:
             text = ' ' + text
-        return "%s:%s%s" % (scheme, qname, text)
+        return "%s:%s%s" % (scheme, name, text)
 
     def _interwiki_repl(self, word):
         """Handle InterWiki links."""
@@ -174,17 +162,24 @@
         if wikitag_bad:
             return word
         else:
-            result = self.interwiki("wiki:" + word)
-            if result.startswith("wiki:"):
-                result = result[5:]
-            return result
+            scheme, wikiname, pagename, text = self.interwiki("wiki:" + word)
+            if wikiname == 'Self':
+                return '["%s"]' % pagename # optimize special case
+            else:
+                pagename = self._intelli_quote(pagename)
+                return "%s:%s" % (wikiname, pagename)
 
     def _url_repl(self, word):
         """Handle literal URLs including inline images."""
         scheme = word.split(":", 1)[0]
 
         if scheme == "wiki":
-            return self.interwiki(word)
+            scheme, wikiname, pagename, text = self.interwiki(word)
+            if wikiname == 'Self':
+                return '["%s"]' % pagename # optimize special case
+            else:
+                pagename = self._intelli_quote(pagename)
+                return "%s:%s:%s" % (scheme, wikiname, pagename)
 
         if scheme in self.attachment_schemas:
             return self.attachment(word)
@@ -212,8 +207,7 @@
             # split on whitespace
             target, linktext = word.split(None, 1)
             target = self._replace_target(target)
-            if ' ' in target:
-                target = wikiutil.quoteName(target)
+            target = self._intelli_quote(target)
         if linktext:
             linktext = ' ' + linktext
         return '[%s%s]' % (target, linktext)
@@ -242,9 +236,18 @@
         if len(scheme_and_rest) == 2: # scheme given
             scheme, rest = scheme_and_rest
             if scheme == "wiki":
-                return self.interwiki(word, pretty_url=1)
+                scheme, wikiname, pagename, text = self.interwiki(word, pretty_url=1)
+                if wikiname == 'Self':
+                    if text:
+                        text = ' %s' % text
+                    return '["%s"%s]' % (pagename, text)
+                else:
+                    pagename = self._intelli_quote(pagename)
+                    if text:
+                        text = ' %s' % text
+                    return "[%s:%s:%s%s]" % (scheme, wikiname, pagename, text)
             if scheme in self.attachment_schemas:
-                return self.attachment(word, pretty_url=1)
+                return '[%s]' % self.attachment(word, pretty_url=1)
 
         words = word.split(None, 1)
         if len(words) == 1:
--- a/MoinMoin/script/migration/_tests/test_conv160_wiki.py	Sun Aug 05 17:25:36 2007 +0200
+++ b/MoinMoin/script/migration/_tests/test_conv160_wiki.py	Sun Aug 05 19:19:49 2007 +0200
@@ -5,10 +5,17 @@
     TODO:
     * add some ../some_page test
     * add some /some_page test
+    * add more quote_triggers
     * fix parser/converter anchor link handling
-    * emit a warning if we find some page name that was renamed as a macro argument
+    * emit a warning if we find some page name that was renamed as a macro argument?
     * shall we support camelcase renaming?
 
+    Limitations of this converter:
+    * converter does not touch "pre sections", thus markup examples in {{{ }}}
+      or ` ` will have to get handled manually.
+    * converter does not touch macro arguments, they will have to get handled
+      manually
+
     @copyright: 2007 MoinMoin:ThomasWaldmann
     @license: GNU GPL, see COPYING for details.
 """
@@ -43,6 +50,9 @@
         ('http://some_server/some_page', rename_some_page, 'http://some_server/some_page'), # external link
         ('[http://some_server/some_page]', rename_some_page, '[http://some_server/some_page]'), # external link
         ('[#some_page]', rename_some_page, '[#some_page]'), # link to anchor that has same name
+        ('[attachment:some_page.png]', rename_some_page, '[attachment:some_page.png]'), # att, not page
+        ('[attachment:some_page.png test picture]', rename_some_page, '[attachment:some_page.png test picture]'), # att, not page
+        ('[attachment:some_page.txt attachment:some_page.png]', rename_some_page, '[attachment:some_page.txt attachment:some_page.png]'),
 
         # page rename changes result
         ('["some_page"]', rename_some_page, '["some page"]'),
@@ -51,15 +61,15 @@
         ('[:some_page:some text]', rename_some_page, '["some page" some text]'),
         ('Self:some_page', rename_some_page, '["some page"]'),
         ('wiki:Self:some_page', rename_some_page, '["some page"]'),
+        ('[wiki:Self:some_page some text]', rename_some_page, '["some page" some text]'),
         # XXX FAILS ('wiki:Self:some_page#some_anchor', rename_some_page, '["some page"#some_anchor]'),
-        ('[wiki:Self:some_page]', rename_some_page, '["some page"]'),
-        ('[wiki:Self:some_page some text]', rename_some_page, '["some page" some text]'),
 
         # other markup changes we do
         ('[:other page]', {}, '["other page"]'),
         ('[:other page:]', {}, '["other page"]'),
         ('[:other page:other text]', {}, '["other page" other text]'),
         # FAILS ('Self:CamelCase', {}, 'CamelCase'),
+        ('[wiki:WikiPedia:Lynx_%28web_browser%29 Lynx]', {}, '[wiki:WikiPedia:"Lynx_(web_browser)" Lynx]'),
 
         # "nothing changed" checks
         ('attachment:OtherPage/with_underscore', rename_some_file, 'attachment:OtherPage/with_underscore'),