changeset 2814:7bde33d15531

1.6 converter: convert ImageLink to new link/transclusion syntax
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Sun, 09 Sep 2007 22:19:45 +0200
parents 82a39e6078d7
children 104590a28e54
files MoinMoin/script/migration/_conv160_wiki.py MoinMoin/script/migration/_tests/test_conv160_wiki.py
diffstat 2 files changed, 47 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/MoinMoin/script/migration/_conv160_wiki.py	Sun Sep 09 13:04:33 2007 +0200
+++ b/MoinMoin/script/migration/_conv160_wiki.py	Sun Sep 09 22:19:45 2007 +0200
@@ -150,10 +150,46 @@
 
     def _macro_repl(self, word):
         # we use [[...]] for links now, macros will be <<...>>
-        stripped_old = word[2:-2]
-        decorated_new = "<<%s>>" % stripped_old
+        macro_rule = ur"""
+            \[\[
+            (?P<macro_name>\w+)
+            (\((?P<macro_args>.*?)\))?
+            \]\]
+        """
+        word = unicode(word) # XXX why is word not unicode before???
+        m = re.match(macro_rule, word, re.X|re.U)
+        macro_name = m.group('macro_name')
+        macro_args = m.group('macro_args')
+        if macro_name == 'ImageLink':
+            fixed, kw, trailing = wikiutil.parse_quoted_separated(macro_args)
+            image, target = (fixed + ['', ''])[:2]
+            if '://' not in image:
+                # if it is not a URL, it is meant as attachment
+                image = u'attachment:%s' % image
+            if not target:
+                target = image
+            image_attrs = []
+            alt = kw.get('alt') or ''
+            width = kw.get('width')
+            if width is not None:
+                image_attrs.append(u"width=%s" % width)
+            height = kw.get('height')
+            if height is not None:
+                image_attrs.append(u"height=%s" % height)
+            image_attrs = u", ".join(image_attrs)
+            if image_attrs:
+                image_attrs = u'|' + image_attrs
+            if alt or image_attrs:
+                alt = u'|' + alt
+            result = u'[[%s|{{%s%s%s}}]]' % (target, image, alt, image_attrs)
+        else:
+            if macro_args:
+                macro_args = u"(%s)" % macro_args
+            else:
+                macro_args = u''
+            result = u"<<%s%s>>" % (macro_name, macro_args)
         # XXX later check whether some to be renamed pagename is used as macro param
-        return decorated_new
+        return result
 
     def _word_repl(self, word, text=None):
         """Handle WikiNames."""
--- a/MoinMoin/script/migration/_tests/test_conv160_wiki.py	Sun Sep 09 13:04:33 2007 +0200
+++ b/MoinMoin/script/migration/_tests/test_conv160_wiki.py	Sun Sep 09 22:19:45 2007 +0200
@@ -110,6 +110,14 @@
 
             # macros
             ('[[BR]]', {}, '<<BR>>'),
+            ('[[FullSearch(wtf)]]', {}, '<<FullSearch(wtf)>>'),
+            ('[[ImageLink(test.png)]]', {}, '[[attachment:test.png|{{attachment:test.png}}]]'),
+            ('[[ImageLink(test.png,OtherPage)]]', {}, '[[OtherPage|{{attachment:test.png}}]]'),
+            ('[[ImageLink(test.png,OtherPage,width=123,height=456)]]', {}, '[[OtherPage|{{attachment:test.png||width=123, height=456}}]]'),
+            ('[[ImageLink(test.png,OtherPage,width=123,height=456,alt=alttext)]]', {}, '[[OtherPage|{{attachment:test.png|alttext|width=123, height=456}}]]'),
+            ('[[ImageLink(test.png,OtherPage,width=123,height=456,alt=alt text with blanks)]]', {}, '[[OtherPage|{{attachment:test.png|alt text with blanks|width=123, height=456}}]]'),
+            ('[[ImageLink(http://server/test.png,OtherPage,width=123,height=456)]]', {}, '[[OtherPage|{{http://server/test.png||width=123, height=456}}]]'),
+            ('[[ImageLink(http://server/test.png,http://server/,width=123)]]', {}, '[[http://server/|{{http://server/test.png||width=123}}]]'),
 
         ]
         for data, renames, expected in tests: