changeset 2705:4a4d84273ae1

new function RelPageName to calculate a relative pagename from 2 absolute pagenames
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Tue, 21 Aug 2007 18:40:59 +0200
parents c6b5c2d813b6
children d2d4e74978bd
files MoinMoin/_tests/test_wikiutil.py MoinMoin/wikiutil.py
diffstat 2 files changed, 58 insertions(+), 17 deletions(-) [+]
line wrap: on
line diff
--- a/MoinMoin/_tests/test_wikiutil.py	Tue Aug 21 15:45:24 2007 +0200
+++ b/MoinMoin/_tests/test_wikiutil.py	Tue Aug 21 18:40:59 2007 +0200
@@ -734,24 +734,32 @@
         assert expected == encoded
 
 class TestRelativeTools:
+    tests = [
+        # test                      expected output
+        # CHILD_PREFIX
+        (('MainPage', '/SubPage1'), 'MainPage/SubPage1'),
+        (('MainPage', '/SubPage1/SubPage2'), 'MainPage/SubPage1/SubPage2'),
+        (('MainPage/SubPage1', '/SubPage2/SubPage3'), 'MainPage/SubPage1/SubPage2/SubPage3'),
+        (('', '/OtherMainPage'), 'OtherMainPage'), # strange
+        # PARENT_PREFIX
+        (('MainPage/SubPage', '../SisterPage'), 'MainPage/SisterPage'),
+        (('MainPage/SubPage1/SubPage2', '../SisterPage'), 'MainPage/SubPage1/SisterPage'),
+        (('MainPage/SubPage1/SubPage2', '../../SisterPage'), 'MainPage/SisterPage'),
+        (('MainPage', '../SisterPage'), 'SisterPage'), # strange
+    ]
     def test_abs_pagename(self):
-        tests = [
-            # test                      expected output
-            # CHILD_PREFIX
-            (('MainPage', '/SubPage1'), 'MainPage/SubPage1'),
-            (('MainPage', '/SubPage1/SubPage2'), 'MainPage/SubPage1/SubPage2'),
-            (('MainPage/SubPage1', '/SubPage2/SubPage3'), 'MainPage/SubPage1/SubPage2/SubPage3'),
-            (('', '/OtherMainPage'), 'OtherMainPage'), # strange
-            # PARENT_PREFIX
-            (('MainPage/SubPage', '../SisterPage'), 'MainPage/SisterPage'),
-            (('MainPage/SubPage1/SubPage2', '../SisterPage'), 'MainPage/SubPage1/SisterPage'),
-            (('MainPage/SubPage1/SubPage2', '../../SisterPage'), 'MainPage/SisterPage'),
-            (('MainPage', '../SisterPage'), 'SisterPage'), # strange
-        ]
-        for test, expected in tests:
-            yield self._check_abs_pagename, test, expected
+        for (current_page, relative_page), absolute_page in self.tests:
+            yield self._check_abs_pagename, current_page, relative_page, absolute_page
 
-    def _check_abs_pagename(self, test, expected):
-        assert expected == wikiutil.AbsPageName(None, *test)
+    def _check_abs_pagename(self, current_page, relative_page, absolute_page):
+        assert absolute_page == wikiutil.AbsPageName(None, current_page, relative_page)
+
+    def test_rel_pagename(self):
+        for (current_page, relative_page), absolute_page in self.tests:
+            yield self._check_rel_pagename, current_page, absolute_page, relative_page
+
+    def _check_rel_pagename(self, current_page, absolute_page, relative_page):
+        assert relative_page == wikiutil.RelPageName(None, current_page, absolute_page)
+
 
 coverage_modules = ['MoinMoin.wikiutil']
--- a/MoinMoin/wikiutil.py	Tue Aug 21 15:45:24 2007 +0200
+++ b/MoinMoin/wikiutil.py	Tue Aug 21 18:40:59 2007 +0200
@@ -851,6 +851,39 @@
             pagename = pagename[CHILD_PREFIX_LEN:]
     return pagename
 
+def RelPageName(request, context, pagename):
+    """
+    Return the relative pagename for some context.
+
+    @param context: name of the page where "pagename" appears on
+    @param pagename: the absolute page name
+    @rtype: string
+    @return: the relative page name
+    """
+    if context == '':
+        # special case, context is some "virtual root" page with name == ''
+        # every page is a subpage of this virtual root
+        return CHILD_PREFIX + pagename
+    elif pagename.startswith(context + CHILD_PREFIX):
+        # simple child
+        return pagename[len(context):]
+    else:
+        # some kind of sister/aunt
+        context_frags = context.split('/')   # A, B, C, D, E
+        pagename_frags = pagename.split('/') # A, B, C, F
+        # first throw away common parents:
+        common = 0
+        for cf, pf in zip(context_frags, pagename_frags):
+            if cf == pf:
+                common += 1
+            else:
+                break
+        context_frags = context_frags[common:] # D, E
+        pagename_frags = pagename_frags[common:] # F
+        go_up = len(context_frags)
+        return PARENT_PREFIX * go_up + '/'.join(pagename_frags)
+
+
 def pagelinkmarkup(pagename):
     """ return markup that can be used as link to page <pagename> """
     from MoinMoin.parser.text_moin_wiki import Parser