changeset 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 29657277dd37
children c61c10e3fcde
files MoinMoin/script/migration/_conv160.py MoinMoin/script/migration/_conv160_wiki.py MoinMoin/script/migration/_tests/test_conv160_wiki.py
diffstat 3 files changed, 108 insertions(+), 36 deletions(-) [+]
line wrap: on
line diff
--- a/MoinMoin/script/migration/_conv160.py	Sat Aug 04 17:35:24 2007 +0200
+++ b/MoinMoin/script/migration/_conv160.py	Sat Aug 04 21:28:22 2007 +0200
@@ -64,14 +64,14 @@
 
 from _conv160_wiki import convert_wiki
 
-def markup_converter(text, renames):
-    """ Convert the <text> content of some Page, using <renames> dict to rename
-        links correctly. Additionally, convert some changed markup.
+def markup_converter(pagename, text, renames):
+    """ Convert the <text> content of page <pagename>, using <renames> dict
+        to rename links correctly. Additionally, convert some changed markup.
     """
     if "#format wiki" not in text and "#format" in text:
         return text # this is not a wiki page, leave it as is
 
-    text = convert_wiki(text, renames)
+    text = convert_wiki(pagename, text, renames)
     return text
 
 
@@ -172,8 +172,9 @@
 
 class PageRev:
     """ a single revision of a page """
-    def __init__(self, request, rev_dir, rev):
+    def __init__(self, request, pagename, rev_dir, rev):
         self.request = request
+        self.pagename = pagename
         self.rev_dir = rev_dir
         self.rev = rev
 
@@ -188,7 +189,7 @@
     def write(self, data, rev_dir, rev=None):
         if rev is None:
             rev = self.rev
-        data = markup_converter(data, self.renames)
+        data = markup_converter(self.pagename, data, self.renames)
         fname = opj(rev_dir, '%08d' % rev)
         data = data.encode(config.charset)
         f = file(fname, "wb")
@@ -252,7 +253,7 @@
             self.revlist = revlist
             self.revisions = {}
             for rev in revlist:
-                self.revisions[rev] = PageRev(self.request, rev_dir, rev)
+                self.revisions[rev] = PageRev(self.request, self.name_old, rev_dir, rev)
         # read attachment filenames
         attach_dir = opj(page_dir, 'attachments')
         if os.path.exists(attach_dir):
--- a/MoinMoin/script/migration/_conv160_wiki.py	Sat Aug 04 17:35:24 2007 +0200
+++ b/MoinMoin/script/migration/_conv160_wiki.py	Sat Aug 04 21:28:22 2007 +0200
@@ -29,20 +29,25 @@
 i18n.wikiLanguages = lambda : []
 from MoinMoin import config, wikiutil
 from MoinMoin.parser.text_moin_wiki import Parser
+from MoinMoin.action import AttachFile
 
 class Converter(Parser):
-    def __init__(self, request, raw, renames):
+    def __init__(self, request, pagename, raw, renames):
         self.request = request
+        self.pagename = pagename
         self.raw = raw
         self.renames = renames
         self.in_pre = False
         self._ = None
 
-    def _replace(self, item_type, item_name):
+    def _replace(self, key):
         """ replace a item_name if it is in the renames dict """
-        key = (item_type, item_name)
+        if key[0] == 'PAGE':
+            item_name = key[1] # pagename
+        elif key[0] == 'FILE':
+            item_name = key[2] # filename, key[1] is pagename
         try:
-            return self.renames[key]
+            return self.renames[key] # new pagename or new filename
         except KeyError:
             return item_name
 
@@ -110,10 +115,10 @@
         target_and_anchor = target.split('#', 1)
         if len(target_and_anchor) > 1:
             target, anchor = target_and_anchor
-            target = self._replace('PAGE', target)
+            target = self._replace(('PAGE', target))
             return '%s#%s' % (target, anchor)
         else:
-            target = self._replace('PAGE', target)
+            target = self._replace(('PAGE', target))
             return target
 
     def interwiki(self, target_and_text, **kw):
@@ -125,7 +130,7 @@
         #self.request.log("interwiki: split_wiki -> %s.%s.%s" % (wikiname,pagename,text))
 
         if wikiname.lower() == 'self': # [wiki:Self:LocalPage text] or [:LocalPage:text]
-            return target_and_text # self._word_repl(pagename, text)
+            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):
@@ -141,22 +146,25 @@
         _ = self._
         #self.request.log("attachment: target_and_text %s" % target_and_text)
         scheme, fname, text = wikiutil.split_wiki(target_and_text)
-        if not text:
-            text = fname
-
-        if scheme == 'drawing':
-            return target_and_text # self.formatter.attachment_drawing(fname, text)
+        pagename, fname = AttachFile.absoluteName(fname, self.pagename)
+        from_this_page = pagename == self.pagename
+        fname = self._replace(('FILE', pagename, fname))
+        if '%20' in fname:
+            fname = fname.replace('%20', ' ')
+        fname = self._replace(('FILE', pagename, fname))
+        pagename = self._replace(('PAGE', pagename))
+        if from_this_page:
+            name = fname
+        else:
+            name = "%s/%s" % (pagename, fname)
+        if ' ' in name:
+            qname = wikiutil.quoteName(name)
+        else:
+            qname = name
 
-        # check for image, and possibly return IMG tag (images are always inlined)
-        if not kw.get('pretty_url', 0) and wikiutil.isPicture(fname):
-            return target_and_text # self.formatter.attachment_image(fname)
-
-        # inline the attachment
-        if scheme == 'inline':
-            return target_and_text # self.formatter.attachment_inlined(fname, text)
-
-        return target_and_text # self.formatter.attachment_link(fname, text)
-
+        if text:
+            text = ' ' + text
+        return "%s:%s%s" % (scheme, qname, text)
 
     def _url_repl(self, word):
         """Handle literal URLs including inline images."""
@@ -186,12 +194,16 @@
             # split on closing quote
             target, linktext = word[1:].split(first_char, 1)
             target = self._replace_target(target)
-            return '[%s%s%s%s]' % (first_char, target, first_char, linktext)
+            target = wikiutil.quoteName(target)
         else: # not quoted
             # split on whitespace
             target, linktext = word.split(None, 1)
-            target = target.replace("_", " ")
-            return '[%s%s%s]' % (target, word[len(target)], linktext)
+            target = self._replace_target(target)
+            if ' ' in target:
+                target = wikiutil.quoteName(target)
+        if linktext:
+            linktext = ' ' + linktext
+        return '[%s%s]' % (target, linktext)
 
 
     def _url_bracket_repl(self, word):
@@ -203,8 +215,15 @@
             words = word[1:].split(':', 1)
             words[0] = self._replace_target(words[0])
             if len(words) == 1:
-                return '[:%s]' % words[0]
-            return '[:%s:%s]' % (words[0], words[1])
+                link = words[0]
+                link = wikiutil.quoteName(link)
+                return '[%s]' % link # use freelink
+            else:
+                link, text = words
+                link = wikiutil.quoteName(link)
+                if text:
+                    text = ' ' + text
+                return '[%s%s]' % (link, text) # use freelink with text
 
         return '[%s]' % word
 
@@ -307,7 +326,7 @@
             formatted_line = self.scan(scanning_re, line)
             self.request.write(formatted_line + '\r\n')
 
-def convert_wiki(intext, renames):
+def convert_wiki(pagename, intext, renames):
     """ Convert content written in wiki markup """
     import StringIO
     request = StringIO.StringIO()
@@ -315,7 +334,7 @@
     if not intext.endswith('\r\n'):
         intext += '\r\n'
         noeol = True
-    p = Converter(request, intext, renames)
+    p = Converter(request, pagename, intext, renames)
     p.convert()
     res = request.getvalue()
     if noeol:
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MoinMoin/script/migration/_tests/test_conv160_wiki.py	Sat Aug 04 21:28:22 2007 +0200
@@ -0,0 +1,52 @@
+# -*- coding: iso-8859-1 -*-
+"""
+    MoinMoin - tests of wiki content conversion
+
+    @copyright: 2007 MoinMoin:ThomasWaldmann
+    @license: GNU GPL, see COPYING for details.
+"""
+
+from MoinMoin.script.migration._conv160_wiki import convert_wiki
+
+def test_wiki_conversion(request):
+    pagename = 'TestPage'
+    rename_some_page = {
+            ('PAGE', 'some_page'): 'some page',
+    }
+    rename_some_file = {
+            ('FILE', pagename, 'with_underscore'): 'without underscore',
+            ('FILE', pagename, 'with blank'): 'without_blank',
+    }
+    tests = [
+        # "nothing changed" checks
+        ('', {}, ''),
+        ('CamelCase', {}, 'CamelCase'),
+        ('some_text', {}, 'some_text'),
+        ('["some_text"]', {}, '["some_text"]'),
+        ('some_page', rename_some_page, 'some_page'), # not a link
+        # page rename changes result
+        ('["some_page"]', rename_some_page, '["some page"]'),
+        ('[:some_page]', rename_some_page, '["some page"]'),
+        ('[:some_page:]', rename_some_page, '["some page"]'),
+        ('[: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]'),
+
+        # "nothing changed" checks
+        ('attachment:OtherPage/with_underscore', rename_some_file, 'attachment:OtherPage/with_underscore'),
+
+        # file rename changes result
+        ('attachment:with_underscore', rename_some_file, 'attachment:"without underscore"'),
+        ('attachment:TestPage/with_underscore', rename_some_file, 'attachment:"without underscore"'), # remove superfluous pagename
+
+        # attachment syntax: kill %20
+        ('attachment:with%20blank', rename_some_file, 'attachment:without_blank'), # plus rename
+        ('attachment:keep%20blank', rename_some_file, 'attachment:"keep blank"'), # no rename
+        ('attachment:TestPage/keep%20blank', rename_some_file, 'attachment:"keep blank"'), # remove superfluous pagename
+        ('attachment:OtherPage/keep%20blank', rename_some_file, 'attachment:"OtherPage/keep blank"'),
+    ]
+    for data, renames, expected in tests:
+        assert convert_wiki(pagename, data, renames) == expected
+