changeset 2566:d5ef57193ccb

(johill, xorAxAx) make formatter responsible for making heading IDs unique
author Johannes Berg <johannes AT sipsolutions DOT net>
date Wed, 25 Jul 2007 14:56:52 +0200
parents 7c400e439160
children b403a24f7c8f
files MoinMoin/formatter/text_html.py MoinMoin/macro/Include.py MoinMoin/macro/TableOfContents.py MoinMoin/parser/text_moin_wiki.py MoinMoin/request/__init__.py
diffstat 5 files changed, 27 insertions(+), 34 deletions(-) [+]
line wrap: on
line diff
--- a/MoinMoin/formatter/text_html.py	Wed Jul 25 14:37:08 2007 +0200
+++ b/MoinMoin/formatter/text_html.py	Wed Jul 25 14:56:52 2007 +0200
@@ -947,7 +947,7 @@
         """
         _ = self.request.getText
         res = []
-        ci = self.request.makeUniqueID('CA-%s_%03d' % (code_id, self._code_area_num))
+        ci = self.request.make_unique_id('CA-%s_%03d' % (code_id, self._code_area_num))
         if on:
             # Open a code area
             self._in_code_area = 1
@@ -1207,6 +1207,10 @@
             number = '.'.join([str(x) for x in self.request._fmt_hd_counters[self._show_section_numbers-1:]])
             if number: number += ". "
 
+        # make ID unique
+        if 'id' in kw:
+            kw['id'] = self.request.make_unique_id(kw['id'])
+
         # Add space before heading, easier to check source code
         result = '\n' + self._open('h%d' % heading_depth, **kw)
 
--- a/MoinMoin/macro/Include.py	Wed Jul 25 14:37:08 2007 +0200
+++ b/MoinMoin/macro/Include.py	Wed Jul 25 14:56:52 2007 +0200
@@ -18,10 +18,11 @@
 
 generates_headings = True
 
-import re, StringIO
-from MoinMoin import wikiutil
+import re, StringIO, sha
+from MoinMoin import wikiutil, config
 from MoinMoin.Page import Page
 
+
 _sysmsg = '<p><strong class="%s">%s</strong></p>'
 
 ## keep in sync with TableOfContents macro!
@@ -190,17 +191,8 @@
                               macro.formatter.text(heading) +
                               macro.formatter.heading(0, level))
             else:
-                import sha
-                from MoinMoin import config
-                # this heading id might produce duplicate ids,
-                # if the same page is included multiple times
-                # Encode stuf we feed into sha module.
                 pntt = (inc_name + heading).encode(config.charset)
                 hid = "head-" + sha.new(pntt).hexdigest()
-                request._page_headings.setdefault(pntt, 0)
-                request._page_headings[pntt] += 1
-                if request._page_headings[pntt] > 1:
-                    hid += '-%d' % (request._page_headings[pntt], )
                 link = inc_page.link_to(request, heading, css_class="include-heading-link")
                 result.extend([
                     macro.formatter.heading(1, level, id=hid),
@@ -216,7 +208,7 @@
         strfile = StringIO.StringIO()
         request.redirect(strfile)
         try:
-            cid = request.makeUniqueID("Include_%s" % wikiutil.quoteWikinameURL(inc_page.page_name))
+            cid = request.make_unique_id("Include_%s" % wikiutil.quoteWikinameURL(inc_page.page_name))
             inc_page.send_page(content_only=1, content_id=cid,
                                omit_footnotes=True)
             result.append(strfile.getvalue())
--- a/MoinMoin/macro/TableOfContents.py	Wed Jul 25 14:37:08 2007 +0200
+++ b/MoinMoin/macro/TableOfContents.py	Wed Jul 25 14:56:52 2007 +0200
@@ -26,7 +26,9 @@
         return text
 
     def heading(self, on, depth, **kw):
-        id = kw.get('id', '')
+        id = kw.get('id', None)
+        if not id is None:
+            id = self.request.make_unique_id(kw['id'])
         self.in_heading = on
         if on:
             self.collected_headings.append([depth, id, u''])
@@ -120,8 +122,7 @@
                                             count_hit=False,
                                             omit_footnotes=True)
 
-    # workaround for include macro
-    macro.request._page_headings = {}
+    macro.request.reset_unique_ids()
 
     _ = macro.request.getText
 
--- a/MoinMoin/parser/text_moin_wiki.py	Wed Jul 25 14:37:08 2007 +0200
+++ b/MoinMoin/parser/text_moin_wiki.py	Wed Jul 25 14:56:52 2007 +0200
@@ -157,7 +157,6 @@
         self.no_862 = False
         self.in_table = 0
         self.inhibit_p = 0 # if set, do not auto-create a <p>aragraph
-        self.titles = request._page_headings
 
         # holds the nesting level (in chars) of open lists
         self.list_indents = []
@@ -767,17 +766,15 @@
         # when included the same page multiple times
         title_text = h[level:-level].strip()
         pntt = self.formatter.page.page_name + title_text
-        self.titles.setdefault(pntt, 0)
-        self.titles[pntt] += 1
 
-        unique_id = ''
-        if self.titles[pntt] > 1:
-            unique_id = '-%d' % self.titles[pntt]
-        result = self._closeP()
-        result += self.formatter.heading(1, depth, id="head-"+sha.new(pntt.encode(config.charset)).hexdigest()+unique_id)
+        id = 'head-' + sha.new(pntt.encode(config.charset)).hexdigest()
 
-        return (result + self.formatter.text(title_text) +
-                self.formatter.heading(0, depth))
+        return ''.join([
+            self._closeP(),
+            self.formatter.heading(1, depth, id=id),
+            self.formatter.text(title_text),
+            self.formatter.heading(0, depth),
+        ])
 
     def _parser_repl(self, word):
         """Handle parsed code displays."""
--- a/MoinMoin/request/__init__.py	Wed Jul 25 14:37:08 2007 +0200
+++ b/MoinMoin/request/__init__.py	Wed Jul 25 14:56:52 2007 +0200
@@ -152,8 +152,6 @@
         # Pages meta data that we collect in one request
         self.pages = {}
 
-        self._page_headings = {}
-
         self.user_headers = []
         self.cacheable = 0 # may this output get cached by http proxies/caches?
         self.http_caching_disabled = 0 # see disableHttpCaching()
@@ -695,10 +693,6 @@
 
         # caches unique ids
         self._page_ids = {}
-        # keeps track of pagename/heading combinations
-        # parsers should use this dict and not a local one, so that
-        # macros like TableOfContents in combination with Include can work
-        self._page_headings = {}
 
         if hasattr(self, "_fmt_hd_counters"):
             del self._fmt_hd_counters
@@ -1386,10 +1380,12 @@
         from MoinMoin import failure
         failure.handle(self, err)
 
-    def makeUniqueID(self, base):
+    def make_unique_id(self, base):
         """
         Generates a unique ID using a given base name. Appends a running count to the base.
 
+        Needs to stay deterministic!
+
         @param base: the base of the id
         @type base: unicode
 
@@ -1402,7 +1398,10 @@
         self._page_ids[base] = count
         if count == 0:
             return base
-        return u'%s_%04d' % (base, count)
+        return u'%s-%d' % (base, count)
+
+    def reset_unique_ids(self):
+        self._page_ids = {}
 
     def httpDate(self, when=None, rfc='1123'):
         """ Returns http date string, according to rfc2068