changeset 2773:2bbf611f4f2c

add link text parameter to pagelinkmarkup
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Sun, 26 Aug 2007 16:30:04 +0200
parents 8ad0e79858ae
children 9d63b942987a
files MoinMoin/_tests/test_wikiutil.py MoinMoin/converter/text_html_text_moin_wiki.py MoinMoin/wikiutil.py
diffstat 3 files changed, 24 insertions(+), 14 deletions(-) [+]
line wrap: on
line diff
--- a/MoinMoin/_tests/test_wikiutil.py	Sun Aug 26 16:07:39 2007 +0200
+++ b/MoinMoin/_tests/test_wikiutil.py	Sun Aug 26 16:30:04 2007 +0200
@@ -715,17 +715,22 @@
 class TestPageLinkMarkup:
     def test_pagelinkmarkup(self):
         tests = [
-            # pagename, expected markup
-            ('SomePage', 'SomePage'),
-            ('Somepage', '[[Somepage]]'),
-            ('somepage', '[[somepage]]'),
-            ('Some Page', '[[Some Page]]'),
+            # pagename (no link text), expected markup
+            (('SomePage', ), 'SomePage'),
+            (('Somepage', ), '[[Somepage]]'),
+            (('somepage', ), '[[somepage]]'),
+            (('Some Page', ), '[[Some Page]]'),
+            # with link text
+            (('SomePage', 'SomePage'), 'SomePage'),
+            (('SomePage', 'some page'), '[[SomePage|some page]]'),
+            (('Some Page', 'Some Page'), '[[Some Page]]'),
+            (('Some Page', 'some Page'), '[[Some Page|some Page]]'),
         ]
-        for pagename, expected in tests:
-            yield self._check, pagename, expected
+        for params, expected in tests:
+            yield self._check, params, expected
 
-    def _check(self, pagename, expected):
-        assert expected == wikiutil.pagelinkmarkup(pagename)
+    def _check(self, params, expected):
+        assert expected == wikiutil.pagelinkmarkup(*params)
 
 class TestRelativeTools:
     tests = [
--- a/MoinMoin/converter/text_html_text_moin_wiki.py	Sun Aug 26 16:07:39 2007 +0200
+++ b/MoinMoin/converter/text_html_text_moin_wiki.py	Sun Aug 26 16:30:04 2007 +0200
@@ -1145,13 +1145,13 @@
                     self.text.append(wikiutil.pagelinkmarkup(text))
                 # labeled link
                 else:
-                    self.text.append('[[%s|%s]]' % (pagename, text))
+                    self.text.append(wikiutil.pagelinkmarkup(pagename, text))
             # mailto link
             elif href.startswith("mailto:"):
                 if href[len("mailto:"):] == text:
                     self.text.extend([self.white_space, text, self.white_space])
                 else:
-                    self.text.append("[[%s|%s]]" % (href, text))
+                    self.text.append("[[%s|%s]]" % (href, text)) # XXX use a (renamed) pagelinkmarkup
             # simple link
             elif href.replace(" ", "%20") == text:
                 self.text.append("%s" % text)
--- a/MoinMoin/wikiutil.py	Sun Aug 26 16:07:39 2007 +0200
+++ b/MoinMoin/wikiutil.py	Sun Aug 26 16:30:04 2007 +0200
@@ -888,13 +888,18 @@
         return PARENT_PREFIX * go_up + '/'.join(pagename_frags)
 
 
-def pagelinkmarkup(pagename):
+def pagelinkmarkup(pagename, text=None):
     """ return markup that can be used as link to page <pagename> """
     from MoinMoin.parser.text_moin_wiki import Parser
-    if re.match(Parser.word_rule + "$", pagename, re.U|re.X):
+    if re.match(Parser.word_rule + "$", pagename, re.U|re.X) and \
+            (text is None or text == pagename):
         return pagename
     else:
-        return u'[[%s]]' % pagename
+        if text is None or text == pagename:
+            text = ''
+        else:
+            text = '|%s' % text
+        return u'[[%s%s]]' % (pagename, text)
 
 #############################################################################
 ### mimetype support