Attachment 'nice_headings-1.6.diff'

Download

   1 # HG changeset patch
   2 # User anarcat@titine.anarcat.ath.cx
   3 # Date 1178839159 14400
   4 # Node ID 7de937813f1a07b3ff98f7de4b68092780ab7e11
   5 # Parent  dc9a3809af61aa74bdb4861f1ab7d02f8b730c0e
   6 factor out the heading uniqueness code into wikiutil
   7 
   8 rework the code so that ascii charsets are readable (and not SHA-1 encrypted)
   9 
  10 non-ascii charsets will receive incremental headings
  11 
  12 all tests show that heading ids are still unique after this, and this actually fixes a bug in the Include macro where the generated heading had a duplicate id
  13 
  14 Ref: MoinMoin:FeatureRequests/NicerHeadingIds
  15 
  16 diff -r dc9a3809af61 -r 7de937813f1a MoinMoin/macro/Include.py
  17 --- a/MoinMoin/macro/Include.py	Mon May 07 22:50:51 2007 +0200
  18 +++ b/MoinMoin/macro/Include.py	Thu May 10 19:19:19 2007 -0400
  19 @@ -188,19 +188,8 @@ def execute(macro, text, args_re=re.comp
  20                                macro.formatter.text(heading) +
  21                                macro.formatter.heading(0, level))
  22              else:
  23 -                import sha
  24 -                from MoinMoin import config
  25 -                # this heading id might produce duplicate ids,
  26 -                # if the same page is included multiple times
  27 -                # Encode stuf we feed into sha module.
  28 -                pntt = (inc_name + heading).encode(config.charset)
  29 -                hid = "head-" + sha.new(pntt).hexdigest()
  30 -                request._page_headings.setdefault(pntt, 0)
  31 -                request._page_headings[pntt] += 1
  32 -                if request._page_headings[pntt] > 1:
  33 -                    hid += '-%d' % (request._page_headings[pntt], )
  34                  result.append(
  35 -                    macro.formatter.heading(1, level, id=hid) +
  36 +                    macro.formatter.heading(1, level, id=wikiutil.unique_heading_id(request._page_headings, heading)) +
  37                      inc_page.link_to(request, heading, css_class="include-heading-link") +
  38                      macro.formatter.heading(0, level)
  39                  )
  40 diff -r dc9a3809af61 -r 7de937813f1a MoinMoin/macro/TableOfContents.py
  41 --- a/MoinMoin/macro/TableOfContents.py	Mon May 07 22:50:51 2007 +0200
  42 +++ b/MoinMoin/macro/TableOfContents.py	Thu May 10 19:19:19 2007 -0400
  43 @@ -8,7 +8,7 @@
  44      @license: GNU GPL, see COPYING for details.
  45  """
  46  
  47 -import re, sha
  48 +import re
  49  from MoinMoin import config, wikiutil
  50  
  51  #Dependencies = ["page"]
  52 @@ -126,9 +126,6 @@ class TableOfContents:
  53          if not match:
  54              return
  55          title_text = match.group('htext').strip()
  56 -        pntt = pagename + title_text
  57 -        self.titles.setdefault(pntt, 0)
  58 -        self.titles[pntt] += 1
  59  
  60          # Get new indent level
  61          newindent = len(match.group('hmarker'))
  62 @@ -148,11 +145,6 @@ class TableOfContents:
  63              self.result.append(self.macro.formatter.number_list(1))
  64              self.result.append(self.macro.formatter.listitem(1))
  65  
  66 -        # Add the heading
  67 -        unique_id = ''
  68 -        if self.titles[pntt] > 1:
  69 -            unique_id = '-%d' % (self.titles[pntt],)
  70 -
  71          # close last listitem if same level
  72          if self.indent == newindent:
  73              self.result.append(self.macro.formatter.listitem(0))
  74 @@ -160,7 +152,7 @@ class TableOfContents:
  75          if self.indent >= newindent:
  76              self.result.append(self.macro.formatter.listitem(1))
  77          self.result.append(self.macro.formatter.anchorlink(1,
  78 -            "head-" + sha.new(pntt.encode(config.charset)).hexdigest() + unique_id) +
  79 +                           wikiutil.unique_heading_id(self.titles, title_text)) +
  80                             self.macro.formatter.text(title_text) +
  81                             self.macro.formatter.anchorlink(0))
  82  
  83 diff -r dc9a3809af61 -r 7de937813f1a MoinMoin/parser/text_moin_wiki.py
  84 --- a/MoinMoin/parser/text_moin_wiki.py	Mon May 07 22:50:51 2007 +0200
  85 +++ b/MoinMoin/parser/text_moin_wiki.py	Thu May 10 19:19:19 2007 -0400
  86 @@ -777,8 +777,6 @@ class Parser:
  87  
  88      def _heading_repl(self, word):
  89          """Handle section headings."""
  90 -        import sha
  91 -
  92          h = word.strip()
  93          level = 1
  94          while h[level:level+1] == '=':
  95 @@ -788,15 +786,8 @@ class Parser:
  96          # FIXME: needed for Included pages but might still result in unpredictable results
  97          # when included the same page multiple times
  98          title_text = h[level:-level].strip()
  99 -        pntt = self.formatter.page.page_name + title_text
 100 -        self.titles.setdefault(pntt, 0)
 101 -        self.titles[pntt] += 1
 102 -
 103 -        unique_id = ''
 104 -        if self.titles[pntt] > 1:
 105 -            unique_id = '-%d' % self.titles[pntt]
 106          result = self._closeP()
 107 -        result += self.formatter.heading(1, depth, id="head-"+sha.new(pntt.encode(config.charset)).hexdigest()+unique_id)
 108 +        result += self.formatter.heading(1, depth, id=wikiutil.unique_heading_id(self.request._page_headings, title_text))
 109  
 110          return (result + self.formatter.text(title_text) +
 111                  self.formatter.heading(0, depth))
 112 diff -r dc9a3809af61 -r 7de937813f1a MoinMoin/wikiutil.py
 113 --- a/MoinMoin/wikiutil.py	Mon May 07 22:50:51 2007 +0200
 114 +++ b/MoinMoin/wikiutil.py	Thu May 10 19:19:19 2007 -0400
 115 @@ -271,6 +271,25 @@ def make_breakable(text, maxlen):
 116          else:
 117              newtext.append(part)
 118      return " ".join(newtext)
 119 +
 120 +def unique_heading_id(headings, text):
 121 +    """ generate an ID for a heading that is unique to this request, human-readable and HTML-compliant
 122 +    """
 123 +    import unicodedata
 124 +    # ID and NAME tokens must begin with a letter ([A-Za-z]) and may be
 125 +    # followed by any number of letters, digits ([0-9]), hyphens ("-"),
 126 +    # underscores ("_"), colons (":"), and periods (".").
 127 +    # http://www.w3.org/TR/html4/types.html
 128 +    pntt = re.sub('[^-A-Za-z0-9_:.]+', '-', unicodedata.normalize('NFKD', text).encode('ascii', 'ignore')).lower()
 129 +    hid = "head-" + pntt # basic heading structure
 130 +    # count the number of times this heading is found in this request
 131 +    headings.setdefault(pntt, 0)
 132 +    headings[pntt] += 1
 133 +    # spcial case: if the text is strictly non-ascii, add a number anyways so it looks nicer
 134 +    if headings[pntt] > 1 or pntt == "-":  
 135 +        hid += '-%d' % (headings[pntt], ) # increment the heading id, to avoid duplicates
 136 +    return re.sub('--+', '-', hid) # necessary because the last line might have added another duplicate -
 137 +
 138  
 139  ########################################################################
 140  ### Storage

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.
  • [get | view] (2007-05-11 00:38:08, 6.3 KB) [[attachment:nice_headings-1.6.diff]]
  • [get | view] (2007-09-20 04:24:08, 1.6 KB) [[attachment:nice_headings-1.7.diff]]
  • [get | view] (2007-05-10 22:53:28, 5.6 KB) [[attachment:nice_headings.diff]]
 All files | Selected Files: delete move to page copy to page

You are not allowed to attach a file to this page.