changeset 4560:050428d1c044

Fix processing of anchors (fragment identifiers), see details below Fixed creole and wiki parser, other parsers might need similar fixes. IDs that are valid fragment identifiers should see no changes. IDs with blanks, non-ASCII chars etc. are now sanitized in the same way for links as well as for link targets, so the user editing a page won't have to bother with it. [[#foo bar]] will link to <<Anchor(foo bar)>> or = foo bar =. Creole heading ID creation was changed to be the same as for moin wiki markup headings (no sha1 any more, but trying to be readable). Creole anchor setting now uses the anchordef formatter function, not rawHTML. TitleIndex and WordIndex now also use IDs sanitized in that way internally.
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Sun, 08 Feb 2009 04:13:22 +0100
parents 1f5b1e0423fb
children 57ab9fdceac0
files MoinMoin/Page.py MoinMoin/formatter/text_html.py MoinMoin/macro/__init__.py MoinMoin/parser/text_creole.py
diffstat 4 files changed, 14 insertions(+), 14 deletions(-) [+]
line wrap: on
line diff
--- a/MoinMoin/Page.py	Sat Feb 07 19:27:16 2009 +0100
+++ b/MoinMoin/Page.py	Sun Feb 08 04:13:22 2009 +0100
@@ -755,7 +755,10 @@
 
         # Add anchor
         if anchor:
-            url = "%s#%s" % (url, wikiutil.url_quote_plus(anchor))
+            fmt = getattr(self, 'formatter', request.html_formatter)
+            if fmt:
+                anchor = fmt.sanitize_to_id(anchor)
+            url = "%s#%s" % (url, anchor)
 
         if not relative:
             url = '%s/%s' % (request.getScriptname(), url)
--- a/MoinMoin/formatter/text_html.py	Sat Feb 07 19:27:16 2009 +0100
+++ b/MoinMoin/formatter/text_html.py	Sun Feb 08 04:13:22 2009 +0100
@@ -502,7 +502,7 @@
                     href = '%s%s%s' % (href, separator, wikiutil.makeQueryString(querystr))
                 anchor = kw.get('anchor')
                 if anchor:
-                    href = '%s#%s' % (href, anchor)
+                    href = '%s#%s' % (href, self.sanitize_to_id(anchor))
                 if wikitag_bad:
                     html_class = 'badinterwiki'
                 else:
--- a/MoinMoin/macro/__init__.py	Sat Feb 07 19:27:16 2009 +0100
+++ b/MoinMoin/macro/__init__.py	Sun Feb 08 04:13:22 2009 +0100
@@ -222,8 +222,9 @@
         for word in all_words:
             letter = wikiutil.getUnicodeIndexGroup(word)
             if letter != current_letter:
-                cssid = "idx" + wikiutil.quoteWikinameURL(letter).replace('%', '')
-                output.append(fmt.heading(1, 2, id=cssid)) # fmt.anchordef didn't work
+                anchor = "idx-%s" % letter
+                output.append(fmt.anchordef(anchor))
+                output.append(fmt.heading(1, 2))
                 output.append(fmt.text(letter.replace('~', 'Others')))
                 output.append(fmt.heading(0, 2))
                 current_letter = letter
@@ -249,8 +250,8 @@
         def _make_index_key(index_letters):
             index_letters.sort()
             def letter_link(ch):
-                cssid = "idx" + wikiutil.quoteWikinameURL(ch).replace('%', '')
-                return fmt.anchorlink(1, cssid) + fmt.text(ch.replace('~', 'Others')) + fmt.anchorlink(0)
+                anchor = "idx-%s" % ch
+                return fmt.anchorlink(1, anchor) + fmt.text(ch.replace('~', 'Others')) + fmt.anchorlink(0)
             links = [letter_link(letter) for letter in index_letters]
             return ' | '.join(links)
 
--- a/MoinMoin/parser/text_creole.py	Sat Feb 07 19:27:16 2009 +0100
+++ b/MoinMoin/parser/text_creole.py	Sun Feb 08 04:13:22 2009 +0100
@@ -227,14 +227,10 @@
 #        return self.formatter.smiley(node.content)
 
     def header_emit(self, node):
-        from MoinMoin.support.python_compatibility import hash_new
-
-        pntt = '%s%s%d' % (self.formatter.page.page_name,
-            self.get_text(node), node.level)
-        ident = "head-%s" % hash_new('sha1', pntt.encode(config.charset)).hexdigest()
+        text = self.get_text(node)
         return ''.join([
-            self.formatter.heading(1, node.level, id=ident),
-            self.formatter.text(node.content or ''),
+            self.formatter.heading(1, node.level, id=text),
+            self.formatter.text(text),
             self.formatter.heading(0, node.level),
         ])
 
@@ -327,7 +323,7 @@
                 # inserted anchors
                 url = wikiutil.url_unquote(target, want_unicode=True)
                 if target.startswith('#'):
-                    return self.formatter.rawHTML(u'<a name="%s"></a>' % url[1:])
+                    return self.formatter.anchordef(url[1:])
                 # default to images
                 return self.formatter.attachment_image(
                     url, alt=text, html_class='image')