changeset 2745:4c95862bf9ef

merge main
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Tue, 21 Aug 2007 18:57:06 +0200
parents e931f45fec3a (current diff) d2d4e74978bd (diff)
children b7b48f70ced4
files MoinMoin/_tests/test_wikiutil.py MoinMoin/formatter/text_html.py MoinMoin/parser/text_rst.py MoinMoin/script/migration/_conv160_wiki.py MoinMoin/wikiutil.py
diffstat 6 files changed, 63 insertions(+), 23 deletions(-) [+]
line wrap: on
line diff
--- a/MoinMoin/_tests/test_wikiutil.py	Tue Aug 21 16:35:54 2007 +0200
+++ b/MoinMoin/_tests/test_wikiutil.py	Tue Aug 21 18:57:06 2007 +0200
@@ -713,24 +713,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(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(current_page, absolute_page)
+
 
 coverage_modules = ['MoinMoin.wikiutil']
--- a/MoinMoin/formatter/text_html.py	Tue Aug 21 16:35:54 2007 +0200
+++ b/MoinMoin/formatter/text_html.py	Tue Aug 21 18:57:06 2007 +0200
@@ -488,7 +488,7 @@
                 wikitail, kw['anchor'] = wikitail.split('#', 1)
             wikitail = wikiutil.url_unquote(wikitail)
             try: # XXX this is the only place where we access self.page - do we need it? Crashes silently on actions!
-                pagename = wikiutil.AbsPageName(self.request, self.page.page_name, wikitail)
+                pagename = wikiutil.AbsPageName(self.page.page_name, wikitail)
             except:
                 pagename = wikitail
             return self.pagelink(on, pagename, **kw)
--- a/MoinMoin/macro/Include.py	Tue Aug 21 16:35:54 2007 +0200
+++ b/MoinMoin/macro/Include.py	Tue Aug 21 18:57:06 2007 +0200
@@ -73,7 +73,7 @@
         this_page._macroInclude_pagelist = {}
 
     # get list of pages to include
-    inc_name = wikiutil.AbsPageName(request, this_page.page_name, args.group('name'))
+    inc_name = wikiutil.AbsPageName(this_page.page_name, args.group('name'))
     pagelist = [inc_name]
     if inc_name.startswith("^"):
         try:
--- a/MoinMoin/parser/text_rst.py	Tue Aug 21 16:35:54 2007 +0200
+++ b/MoinMoin/parser/text_rst.py	Tue Aug 21 18:57:06 2007 +0200
@@ -380,7 +380,7 @@
                 anchor = ''
                 if '#' in refuri:
                     pagename, anchor = refuri.split('#', 1)
-                page = Page(self.request, wikiutil.AbsPageName(self.request, self.formatter.page.page_name, pagename))
+                page = Page(self.request, wikiutil.AbsPageName(self.formatter.page.page_name, pagename))
                 node['refuri'] = page.url(self.request, anchor=anchor)
                 if not page.exists():
                     node['classes'].append('nonexistent')
--- a/MoinMoin/script/migration/_conv160_wiki.py	Tue Aug 21 16:35:54 2007 +0200
+++ b/MoinMoin/script/migration/_conv160_wiki.py	Tue Aug 21 18:57:06 2007 +0200
@@ -61,7 +61,6 @@
         self.formatting_rules = self.formatting_rules % {'macronames': u'|'.join(macro.getNames(self.request.cfg))}
 
     # no change
-
     def return_word(self, word):
         return word
     _emph_repl = return_word
@@ -104,7 +103,7 @@
         """
         current_page = self.pagename
         item_type, page_name, file_name = (key + (None, ))[:3]
-        abs_page_name = wikiutil.AbsPageName(self.request, current_page, page_name)
+        abs_page_name = wikiutil.AbsPageName(current_page, page_name)
         if item_type == 'PAGE':
             item_name = page_name
             key = (item_type, abs_page_name)
--- a/MoinMoin/wikiutil.py	Tue Aug 21 16:35:54 2007 +0200
+++ b/MoinMoin/wikiutil.py	Tue Aug 21 18:57:06 2007 +0200
@@ -834,7 +834,7 @@
     return homewiki, username
 
 
-def AbsPageName(request, context, pagename):
+def AbsPageName(context, pagename):
     """
     Return the absolute pagename for a (possibly) relative pagename.
 
@@ -855,6 +855,39 @@
             pagename = pagename[CHILD_PREFIX_LEN:]
     return pagename
 
+def RelPageName(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