changeset 2778:c8dd346f146e

parser: anchor, bang_meta, relative pagelink fixes, more tests
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Tue, 28 Aug 2007 03:12:46 +0200
parents 8e87182648f4
children c9dd12befda7
files MoinMoin/parser/_tests/test_text_moin_wiki.py MoinMoin/parser/text_moin_wiki.py
diffstat 2 files changed, 41 insertions(+), 30 deletions(-) [+]
line wrap: on
line diff
--- a/MoinMoin/parser/_tests/test_text_moin_wiki.py	Tue Aug 28 01:34:46 2007 +0200
+++ b/MoinMoin/parser/_tests/test_text_moin_wiki.py	Tue Aug 28 03:12:46 2007 +0200
@@ -18,6 +18,7 @@
 from MoinMoin.parser.text_moin_wiki import Parser as WikiParser
 from MoinMoin.formatter.text_html import Formatter as HtmlFormatter
 
+PAGENAME = u'ThisPageDoesNotExistsAndWillNeverBeReally'
 
 class ParserTestCase(object):
     """ Helper class that provide a parsing method """
@@ -30,7 +31,7 @@
         request = self.request
         assert body is not None
         request.reset()
-        page = Page(request, 'ThisPageDoesNotExistsAndWillNeverBeReally')
+        page = Page(request, PAGENAME)
         page.hilite_re = None
         page.set_raw_body(body)
         formatter = HtmlFormatter(request)
@@ -534,9 +535,13 @@
     _tests = [
         # test,           expected
         ('SomeNonExistentPage', '<a class="nonexistent" href="./SomeNonExistentPage">SomeNonExistentPage</a>'),
+        ('SomeNonExistentPage#anchor', '<a class="nonexistent" href="./SomeNonExistentPage#anchor">SomeNonExistentPage#anchor</a>'),
         ('[[something]]', '<a class="nonexistent" href="./something">something</a>'),
         ('[[some thing]]', '<a class="nonexistent" href="./some%20thing">some thing</a>'),
         ('[[something|some text]]', '<a class="nonexistent" href="./something">some text</a>'),
+        ('[[../something]]', '<a class="nonexistent" href="./something">../something</a>'),
+        ('[[/something]]', '<a class="nonexistent" href="./%s/something">/something</a>' % PAGENAME),
+        ('[[something#anchor]]', '<a class="nonexistent" href="./something#anchor">something#anchor</a>'),
         ('MoinMoin:something', '<a class="interwiki" href="http://moinmoin.wikiwikiweb.de/something" title="MoinMoin">something</a>'),
         ('[[MoinMoin:something|some text]]', '<a class="interwiki" href="http://moinmoin.wikiwikiweb.de/something" title="MoinMoin">some text</a>'),
         ('[[MoinMoin:with space]]', '<a class="interwiki" href="http://moinmoin.wikiwikiweb.de/with%20space" title="MoinMoin">with space</a>'),
--- a/MoinMoin/parser/text_moin_wiki.py	Tue Aug 28 01:34:46 2007 +0200
+++ b/MoinMoin/parser/text_moin_wiki.py	Tue Aug 28 03:12:46 2007 +0200
@@ -62,7 +62,7 @@
         (?P<word_bang>\!)?  # configurable: avoid getting CamelCase rendered as link
         (?P<word_name>
          (?:
-          (?P<word_parent_prefix>%(parent)s)*  # there might be either ../ parent prefix(es)
+          (%(parent)s)*  # there might be either ../ parent prefix(es)
           |
           ((?<!%(child)s)%(child)s)?  # or maybe a single / child prefix (but not if we already had it before)
          )
@@ -70,6 +70,10 @@
           ((?<!%(child)s)%(child)s)?  # there might be / child prefix (but not if we already had it before)
           (?:[%(u)s][%(l)s]+){2,}  # at least 2 upper>lower transitions make CamelCase
          )+  # we can have MainPage/SubPage/SubSubPage ...
+         (?:
+          \#  # anchor separator          TODO check if this does not make trouble at places where word_rule is used
+          (?P<word_anchor>\S+)  # some anchor name
+         )?
         )
         (?:
          (?![%(u)s%(l)s/])  # require anything not upper/lower/slash following
@@ -523,31 +527,33 @@
 
     def _word_repl(self, word, groups):
         """Handle WikiNames."""
-        bang = groups.get('word_bang')
-        if bang:
-            # self.cfg.bang_meta:
-            # handle !NotWikiNames
-            return self.formatter.nowikiword(word)
-        orig_word = word
+        bang = ''
+        bang_present = groups.get('word_bang')
+        if bang_present:
+            if self.cfg.bang_meta:
+                # handle !NotWikiNames
+                return self.formatter.nowikiword(word)
+            else:
+                bang = self.formatter.text('!')
         name = groups.get('word_name')
-        parent_prefix = groups.get('word_parent_prefix')
         current_page = self.formatter.page.page_name
-        name = wikiutil.AbsPageName(current_page, name)
+        abs_name = wikiutil.AbsPageName(current_page, name)
         # if a simple, self-referencing link, emit it as plain text
-        if name == current_page:
-            return self.formatter.text(orig_word)
+        if abs_name == current_page:
+            return self.formatter.text(word)
         else:
             # handle anchors
-            parts = rsplit(name, "#", 1)
-            anchor = ""
-            if len(parts) == 2:
-                name, anchor = parts
-            return (self.formatter.pagelink(1, name, anchor=anchor) +
-                    self.formatter.text(orig_word) +
-                    self.formatter.pagelink(0, name))
+            try:
+                abs_name, anchor = rsplit(abs_name, "#", 1)
+            except ValueError:
+                anchor = ""
+            return (bang +
+                    self.formatter.pagelink(1, abs_name, anchor=anchor) +
+                    self.formatter.text(word) +
+                    self.formatter.pagelink(0, abs_name))
     _word_bang_repl = _word_repl
-    _word_parent_prefix_repl = _word_repl
     _word_name_repl = _word_repl
+    _word_anchor_repl = _word_repl
 
     def _url_repl(self, word, groups):
         """Handle literal URLs."""
@@ -693,20 +699,20 @@
         mt = self.link_target_re.match(target)
         if mt:
             if mt.group('page_name'):
-                page_name = mt.group('page_name')
-                # handle relative links
-                if page_name.startswith(self.CHILD_PREFIX):
-                    page_name = self.formatter.page.page_name + '/' + page_name[self.CHILD_PREFIX_LEN:] # XXX use func
+                page_name_and_anchor = mt.group('page_name')
                 # handle anchors
                 try:
-                    page_name, anchor = rsplit(page_name, "#", 1)
+                    page_name, anchor = rsplit(page_name_and_anchor, "#", 1)
                 except ValueError:
-                    anchor = ""
+                    page_name, anchor = page_name_and_anchor, ""
+                current_page = self.formatter.page.page_name
                 if not page_name:
-                    page_name = self.formatter.page.page_name
-                return (self.formatter.pagelink(1, page_name, anchor=anchor) +
-                        self._link_description(desc, target, page_name) +
-                        self.formatter.pagelink(0, page_name))
+                    page_name = current_page
+                # handle relative links
+                abs_page_name = wikiutil.AbsPageName(current_page, page_name)
+                return (self.formatter.pagelink(1, abs_page_name, anchor=anchor) +
+                        self._link_description(desc, target, page_name_and_anchor) +
+                        self.formatter.pagelink(0, abs_page_name))
 
             elif mt.group('extern_addr'):
                 scheme = mt.group('extern_scheme')