changeset 2567:b403a24f7c8f

(xorAxAx, johill) make saner looking anchors, no more sha hash
author Johannes Berg <johannes AT sipsolutions DOT net>
date Wed, 25 Jul 2007 15:34:01 +0200
parents d5ef57193ccb
children 80eac6fc152a
files MoinMoin/_tests/test_wikiutil.py MoinMoin/macro/Include.py MoinMoin/parser/text_moin_wiki.py MoinMoin/wikiutil.py
diffstat 4 files changed, 22 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/MoinMoin/_tests/test_wikiutil.py	Wed Jul 25 14:56:52 2007 +0200
+++ b/MoinMoin/_tests/test_wikiutil.py	Wed Jul 25 15:34:01 2007 +0200
@@ -717,4 +717,18 @@
         assert ief(self.request, has_complex, u'b=2.007', [2.007])
 
 
+class TestAnchorNames:
+    def test_anchor_name_encoding(self):
+        tests = [
+            # text                    expected output
+            (u'\xf6\xf6ll\xdf\xdf',   '.2BAPYA9g-ll.2BAN8A3w-'),
+            (u'level 2',              'level_2'),
+        ]
+        for text, expected in tests:
+            yield self._check, text, expected
+
+    def _check(self, text, expected):
+        encoded = wikiutil.anchor_name_from_text(text)
+        assert expected == encoded
+
 coverage_modules = ['MoinMoin.wikiutil']
--- a/MoinMoin/macro/Include.py	Wed Jul 25 14:56:52 2007 +0200
+++ b/MoinMoin/macro/Include.py	Wed Jul 25 15:34:01 2007 +0200
@@ -18,7 +18,7 @@
 
 generates_headings = True
 
-import re, StringIO, sha
+import re, StringIO
 from MoinMoin import wikiutil, config
 from MoinMoin.Page import Page
 
@@ -191,8 +191,7 @@
                               macro.formatter.text(heading) +
                               macro.formatter.heading(0, level))
             else:
-                pntt = (inc_name + heading).encode(config.charset)
-                hid = "head-" + sha.new(pntt).hexdigest()
+                hid = wikiutil.anchor_name_from_text(heading)
                 link = inc_page.link_to(request, heading, css_class="include-heading-link")
                 result.extend([
                     macro.formatter.heading(1, level, id=hid),
--- a/MoinMoin/parser/text_moin_wiki.py	Wed Jul 25 14:56:52 2007 +0200
+++ b/MoinMoin/parser/text_moin_wiki.py	Wed Jul 25 15:34:01 2007 +0200
@@ -11,6 +11,7 @@
 import re
 from MoinMoin import config, wikiutil, macro
 
+
 Dependencies = ['user'] # {{{#!wiki comment ... }}} has different output depending on the user's profile settings
 
 class Parser:
@@ -754,20 +755,14 @@
 
     def _heading_repl(self, word):
         """Handle section headings."""
-        import sha
-
         h = word.strip()
         level = 1
         while h[level:level+1] == '=':
             level += 1
         depth = min(5, level)
 
-        # FIXME: needed for Included pages but might still result in unpredictable results
-        # when included the same page multiple times
         title_text = h[level:-level].strip()
-        pntt = self.formatter.page.page_name + title_text
-
-        id = 'head-' + sha.new(pntt.encode(config.charset)).hexdigest()
+        id = wikiutil.anchor_name_from_text(title_text)
 
         return ''.join([
             self._closeP(),
--- a/MoinMoin/wikiutil.py	Wed Jul 25 14:56:52 2007 +0200
+++ b/MoinMoin/wikiutil.py	Wed Jul 25 15:34:01 2007 +0200
@@ -2099,6 +2099,10 @@
     lines = diff_text.diff(lines1, lines2, **kw)
     return lines
 
+def anchor_name_from_text(text):
+    quoted = urllib.quote_plus(text.encode('utf-7'))
+    return quoted.replace('%', '.').replace('+', '_')
+
 
 ########################################################################
 ### Tickets - used by RenamePage and DeletePage