changeset 2604:27f06531a91b

1.6 converter: improve content conversion, give request obj to converter
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Sun, 05 Aug 2007 00:42:20 +0200
parents c61c10e3fcde
children a12f848eee3c
files MoinMoin/script/migration/_conv160.py MoinMoin/script/migration/_conv160_wiki.py MoinMoin/script/migration/_tests/test_conv160_wiki.py
diffstat 3 files changed, 22 insertions(+), 16 deletions(-) [+]
line wrap: on
line diff
--- a/MoinMoin/script/migration/_conv160.py	Sun Aug 05 00:05:34 2007 +0200
+++ b/MoinMoin/script/migration/_conv160.py	Sun Aug 05 00:42:20 2007 +0200
@@ -64,14 +64,14 @@
 
 from _conv160_wiki import convert_wiki
 
-def markup_converter(pagename, text, renames):
+def markup_converter(request, 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(pagename, text, renames)
+    text = convert_wiki(request, pagename, text, renames)
     return text
 
 
@@ -189,7 +189,7 @@
     def write(self, data, rev_dir, rev=None):
         if rev is None:
             rev = self.rev
-        data = markup_converter(self.pagename, data, self.renames)
+        data = markup_converter(self.request, self.pagename, data, self.renames)
         fname = opj(rev_dir, '%08d' % rev)
         data = data.encode(config.charset)
         f = file(fname, "wb")
--- a/MoinMoin/script/migration/_conv160_wiki.py	Sun Aug 05 00:05:34 2007 +0200
+++ b/MoinMoin/script/migration/_conv160_wiki.py	Sun Aug 05 00:42:20 2007 +0200
@@ -32,8 +32,8 @@
 from MoinMoin.action import AttachFile
 
 class Converter(Parser):
-    def __init__(self, request, pagename, raw, renames):
-        self.request = request
+    def __init__(self, pagename, raw, renames):
+        self.request = None
         self.pagename = pagename
         self.raw = raw
         self.renames = renames
@@ -126,7 +126,8 @@
         wikiname, pagename, text = wikiutil.split_wiki(rest)
         #self.request.log("interwiki: split_wiki -> %s.%s.%s" % (wikiname,pagename,text))
 
-        if wikiname.lower() == 'self': # [wiki:Self:LocalPage text] or [:LocalPage:text]
+        wikiname_lower = wikiname.lower()
+        if wikiname_lower == 'self' or wikiname_lower == self.request.cfg.interwikiname: # [wiki:Self:LocalPage text] or [:LocalPage:text]
             pagename = self._replace(('PAGE', pagename))
             if not text:
                 return '[%s]' % wikiutil.quoteName(pagename) # ["LocalPage"]
@@ -169,8 +170,14 @@
 
     def _interwiki_repl(self, word):
         """Handle InterWiki links."""
-        # XXX if we have access to the cfg, we can limit this to really existings interwiki identifiers
-        return self.interwiki("wiki:" + word)
+        wikitag, wikiurl, wikitail, wikitag_bad = wikiutil.resolve_wiki(self.request, word)
+        if wikitag_bad:
+            return word
+        else:
+            result = self.interwiki("wiki:" + word)
+            if result.startswith("wiki:"):
+                result = result[5:]
+            return result
 
     def _url_repl(self, word):
         """Handle literal URLs including inline images."""
@@ -289,10 +296,11 @@
 
         return ""
 
-    def convert(self):
+    def convert(self, request):
         """ For each line, scan through looking for magic
             strings, outputting verbatim any intervening text.
         """
+        self.request = request
         # prepare regex patterns
         rules = self.formatting_rules.replace('\n', '|')
         if 1: # self.cfg.bang_meta:
@@ -346,17 +354,14 @@
             formatted_line = self.scan(scanning_re, line)
             self.request.write(formatted_line + '\r\n')
 
-def convert_wiki(pagename, intext, renames):
+def convert_wiki(request, pagename, intext, renames):
     """ Convert content written in wiki markup """
-    import StringIO
-    request = StringIO.StringIO()
     noeol = False
     if not intext.endswith('\r\n'):
         intext += '\r\n'
         noeol = True
-    p = Converter(request, pagename, intext, renames)
-    p.convert()
-    res = request.getvalue()
+    p = Converter(pagename, intext, renames)
+    res = request.redirectedOutput(p.convert, request)
     if noeol:
         res = res[:-2]
     return res
--- a/MoinMoin/script/migration/_tests/test_conv160_wiki.py	Sun Aug 05 00:05:34 2007 +0200
+++ b/MoinMoin/script/migration/_tests/test_conv160_wiki.py	Sun Aug 05 00:42:20 2007 +0200
@@ -21,6 +21,7 @@
         # "nothing changed" checks
         ('', {}, ''),
         ('CamelCase', {}, 'CamelCase'),
+        ('MoinMaster:CamelCase', {}, 'MoinMaster:CamelCase'),
         ('some_text', {}, 'some_text'),
         ('["some_text"]', {}, '["some_text"]'),
         ('some_page', rename_some_page, 'some_page'), # not a link
@@ -55,5 +56,5 @@
         ('attachment:OtherPage/keep%20blank', rename_some_file, 'attachment:"OtherPage/keep blank"'),
     ]
     for data, renames, expected in tests:
-        assert convert_wiki(pagename, data, renames) == expected
+        assert convert_wiki(request, pagename, data, renames) == expected