Attachment 'nice_headings.diff'

Download

   1  macro/Include.py         |   11 +----------
   2  macro/TableOfContents.py |   12 ++----------
   3  parser/wiki.py           |   10 +---------
   4  wikiutil.py              |   19 +++++++++++++++++++
   5  4 files changed, 23 insertions(+), 29 deletions(-)
   6 --- wikiutil.py.orig	Thu May 10 16:35:47 2007
   7 +++ wikiutil.py	Thu May 10 18:44:33 2007
   8 @@ -274,6 +274,25 @@
   9              newtext.append(part)
  10      return " ".join(newtext)
  11  
  12 +def unique_heading_id(headings, text):
  13 +    """ generate an ID for a heading that is unique to this request, human-readable and HTML-compliant
  14 +    """
  15 +    import unicodedata
  16 +    # ID and NAME tokens must begin with a letter ([A-Za-z]) and may be
  17 +    # followed by any number of letters, digits ([0-9]), hyphens ("-"),
  18 +    # underscores ("_"), colons (":"), and periods (".").
  19 +    # http://www.w3.org/TR/html4/types.html
  20 +    pntt = re.sub('[^-A-Za-z0-9_:.]+', '-', unicodedata.normalize('NFKD', text).encode('ascii', 'ignore')).lower()
  21 +    hid = "head-" + pntt # basic heading structure
  22 +    # count the number of times this heading is found in this request
  23 +    headings.setdefault(pntt, 0)
  24 +    headings[pntt] += 1
  25 +    # spcial case: if the text is strictly non-ascii, add a number anyways so it looks nicer
  26 +    if headings[pntt] > 1 or pntt == "-":  
  27 +        hid += '-%d' % (headings[pntt], ) # increment the heading id, to avoid duplicates
  28 +    return re.sub('--+', '-', hid) # necessary because the last line might have added another duplicate -
  29 +
  30 +
  31  ########################################################################
  32  ### Storage
  33  ########################################################################
  34 --- parser/wiki.py.orig	Sat Sep 16 18:21:52 2006
  35 +++ parser/wiki.py	Thu May 10 18:42:59 2007
  36 @@ -744,8 +744,6 @@
  37  
  38      def _heading_repl(self, word):
  39          """Handle section headings."""
  40 -        import sha
  41 -
  42          h = word.strip()
  43          level = 1
  44          while h[level:level+1] == '=':
  45 @@ -756,15 +754,9 @@
  46          # TODO but it might still result in unpredictable results
  47          # when included the same page multiple times
  48          title_text = h[level:-level].strip()
  49 -        pntt = self.formatter.page.page_name + title_text
  50 -        self.titles.setdefault(pntt, 0)
  51 -        self.titles[pntt] += 1
  52  
  53 -        unique_id = ''
  54 -        if self.titles[pntt] > 1:
  55 -            unique_id = '-%d' % self.titles[pntt]
  56          result = self._closeP()
  57 -        result += self.formatter.heading(1, depth, id="head-"+sha.new(pntt.encode(config.charset)).hexdigest()+unique_id)
  58 +        result += self.formatter.heading(1, depth, id=wikiutil.unique_heading_id(self.request._page_headings, title_text))
  59                                       
  60          return (result + self.formatter.text(title_text) +
  61                  self.formatter.heading(0, depth))
  62 --- macro/Include.py.orig	Wed Apr 18 14:56:22 2007
  63 +++ macro/Include.py	Thu May 10 18:43:11 2007
  64 @@ -190,21 +190,12 @@
  65                                macro.formatter.text(heading) +
  66                                macro.formatter.heading(0, level))
  67              else:
  68 -                import sha
  69 -                from MoinMoin import config
  70                  # this heading id might produce duplicate ids,
  71                  # if the same page is included multiple times
  72 -                # Encode stuf we feed into sha module.
  73 -                pntt = (inc_name + heading).encode(config.charset)
  74 -                hid = "head-" + sha.new(pntt).hexdigest()
  75 -                request._page_headings.setdefault(pntt, 0)
  76 -                request._page_headings[pntt] += 1
  77 -                if request._page_headings[pntt] > 1:
  78 -                    hid += '-%d'%(request._page_headings[pntt],)
  79                  result.append(
  80                      #macro.formatter.heading(1, level, hid,
  81                      #    icons=edit_icon.replace('<img ', '<img align="right" ')) +
  82 -                    macro.formatter.heading(1, level, id=hid) +
  83 +                    macro.formatter.heading(1, level, id=wikiutil.unique_heading_id(request._page_headings, heading)) +
  84                      inc_page.link_to(request, heading, css_class="include-heading-link") +
  85                      macro.formatter.heading(0, level)
  86                  )
  87 --- macro/TableOfContents.py.orig	Fri Nov 10 17:02:52 2006
  88 +++ macro/TableOfContents.py	Thu May 10 18:43:32 2007
  89 @@ -8,7 +8,7 @@
  90      @license: GNU GPL, see COPYING for details.
  91  """
  92  
  93 -import re, sha
  94 +import re
  95  from MoinMoin import config, wikiutil
  96  
  97  #Dependencies = ["page"]
  98 @@ -125,9 +125,6 @@
  99          match = self.head_re.match(line)
 100          if not match: return
 101          title_text = match.group('htext').strip()
 102 -        pntt = pagename + title_text
 103 -        self.titles.setdefault(pntt, 0)
 104 -        self.titles[pntt] += 1
 105  
 106          # Get new indent level
 107          newindent = len(match.group('hmarker'))
 108 @@ -147,11 +144,6 @@
 109              self.result.append(self.macro.formatter.number_list(1))
 110              self.result.append(self.macro.formatter.listitem(1))
 111  
 112 -        # Add the heading
 113 -        unique_id = ''
 114 -        if self.titles[pntt] > 1:
 115 -            unique_id = '-%d' % (self.titles[pntt],)
 116 -
 117          # close last listitem if same level
 118          if self.indent == newindent:
 119              self.result.append(self.macro.formatter.listitem(0))
 120 @@ -159,7 +151,7 @@
 121          if self.indent >= newindent:
 122              self.result.append(self.macro.formatter.listitem(1))
 123          self.result.append(self.macro.formatter.anchorlink(1,
 124 -            "head-" + sha.new(pntt.encode(config.charset)).hexdigest() + unique_id) +
 125 +                           wikiutil.unique_heading_id(self.titles, title_text)) +
 126                             self.macro.formatter.text(title_text) +
 127                             self.macro.formatter.anchorlink(0))
 128  

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.