[Moin-user] PATCH - Include and TableOfContents to go well with each other

Fujio Nobori toh at fuji-climb.org
Thu Apr 8 03:51:01 EDT 2004


Hello,

Here is a patch (against moin-1.2--patch-279 from tla) for
Include and TableOfContents to go well with each other.

I found that:

 1. TableOfContents doesn't work right for pages included by
 Include macro.

 1. The section numbers of Included pages are wrong if you
 set section-numbers to 'on'.

 1. The links of table of contents are wrong if there are
 the same titles (headings) in different pages included by
 Include macro.

 1. Include macro doesn't work right if level parameter is
 not set and from parameter is set.  For example:
    [[Include(PageName, , , from="^----$")]]

This patch should fix these issues.

I would appriciate any comment.

Thank you very much in advance.

-- 
Fujio Nobori                                     il|li
    email: toh at fuji-climb.org                   q|@.@|p
                                              m. ( o ) .m
                                             ~~~~~~~~~~~~~
-------------- next part --------------
--- orig/MoinMoin/formatter/text_html.py
+++ mod/MoinMoin/formatter/text_html.py
@@ -31,6 +31,7 @@
         self._in_code = 0
         self._base_depth = 0
         self._show_section_numbers = None
+        self._is_included = False
 
         if not hasattr(request, '_fmt_hd_counters'):
             request._fmt_hd_counters = []
@@ -197,7 +198,11 @@
         # remember depth of first heading, and adapt counting depth accordingly
         if not self._base_depth:
             self._base_depth = depth
-        count_depth = max(depth - (self._base_depth - 1), 1)
+
+        if not self._is_included:
+            count_depth = max(depth - (self._base_depth - 1), 1)
+        else:
+            count_depth = self._base_depth
 
         # check numbering, possibly changing the default
         if self._show_section_numbers is None:


--- orig/MoinMoin/macro/Include.py
+++ mod/MoinMoin/macro/Include.py
@@ -19,7 +19,7 @@
 
 _sysmsg = '<p><strong class="%s">%s</strong></p>'
 _arg_heading = r'(?P<heading>,)\s*(|(?P<hquote>[\'"])(?P<htext>.+?)(?P=hquote))'
-_arg_level = r',\s*(?P<level>\d+)'
+_arg_level = r',\s*(?P<level>\d*)'
 _arg_from = r'(,\s*from=(?P<fquote>[\'"])(?P<from>.+?)(?P=fquote))?'
 _arg_to = r'(,\s*to=(?P<tquote>[\'"])(?P<to>.+?)(?P=tquote))?'
 _arg_sort = r'(,\s*sort=(?P<sort>(ascending|descending)))?'
@@ -45,7 +45,7 @@
 
 Dependencies = ["pages"] # included page
 
-def execute(macro, text, args_re=re.compile(_args_re_pattern)):
+def execute(macro, text, args_re=re.compile(_args_re_pattern), called_by_toc=0):
     _ = macro.request.getText
 
     # return immediately if getting links for the current page
@@ -160,6 +160,10 @@
         ##result.append("*** f=%s t=%s ***" % (from_re, to_re))
         ##result.append("*** f=%d t=%d ***" % (from_pos, to_pos))
 
+        if called_by_toc:
+            result.append(inc_page.get_raw_body())
+            continue
+
         # edit icon
         edit_icon = inc_page.link_to(macro.request,
             macro.request.theme.make_icon("edit"),
@@ -177,8 +181,10 @@
             if print_mode:
                 result.append(macro.formatter.heading(level, heading))
             else:
+                import sha
                 result.append(macro.formatter.heading(level,
                     inc_page.link_to(macro.request, heading, css_class="include-heading-link"),
+                    id="head-"+sha.new(inc_name + heading).hexdigest(),
                     icons=edit_icon.replace('<img ', '<img align="right" ')))
 
         # set or increment include marker
@@ -189,6 +195,7 @@
         strfile = cStringIO.StringIO()
         macro.request.redirect(strfile)
         try:
+            inc_page.formatter._is_included = True
             inc_page.send_page(macro.request, content_only=1, content_id="Include_%s" % wikiutil.quoteWikiname(inc_page.page_name) )
             result.append(strfile.getvalue())
         finally:


--- orig/MoinMoin/macro/TableOfContents.py
+++ mod/MoinMoin/macro/TableOfContents.py
@@ -10,11 +10,64 @@
 
 # Imports
 import re, sha
+from MoinMoin import wikiutil
 
 Dependencies = ["page"]
 
-def execute(macro, args):
+def parse_line(line, macro, pagename):
+    global result
+    global baseindent
+    global indent
+    global titles
+    global mindepth
+    global maxdepth
+
     heading = re.compile(r"^\s*(?P<hmarker>=+)\s(.*)\s(?P=hmarker)$")
+    # FIXME this also finds "headlines" in {{{ code sections }}}:
+    match = heading.search(line)
+    if not match: return
+    title_text = match.group(2)
+    titles.setdefault(pagename + title_text, 0)
+    titles[pagename + title_text] += 1
+
+    # Get new indent level
+    newindent = len(match.group(1))
+    if newindent > maxdepth: return
+    if newindent < mindepth: return
+    if not indent:
+        baseindent = newindent - 1
+        indent = baseindent
+
+    # Close lists
+    for i in range(0,indent-newindent):
+        result.append(macro.formatter.number_list(0))
+
+    # Open Lists
+    for i in range(0,newindent-indent):
+        result.append(macro.formatter.number_list(1))
+
+    # Add the heading
+    unique_id = ''
+    if titles[pagename + title_text] > 1:
+        unique_id = '-%d' % titles[pagename + title_text]
+
+    result.append(macro.formatter.listitem(1))
+    result.append(macro.formatter.anchorlink(
+        "head-" + sha.new(pagename + title_text).hexdigest() + unique_id, title_text))
+    result.append(macro.formatter.listitem(0))
+    
+    # Set new indent level
+    indent = newindent
+
+def execute(macro, args):
+    global result
+    global baseindent
+    global indent
+    global titles
+    global mindepth
+    global maxdepth
+
+    include = re.compile(r"^\[\[Include\((.*)\)\]\]")
     result = []
     baseindent = 0
     indent = 0
@@ -31,44 +84,43 @@
     except (ValueError, TypeError):
         maxdepth = 99
 
+    pagename = macro.formatter.page.page_name
     for line in macro.parser.lines:
         # Filter out the headings
         lineno = lineno + 1
-        # FIXME this also finds "headlines" in {{{ code sections }}}:
-        match = heading.match(line)
-        if not match: continue
-        title_text = match.group(2)
-        titles.setdefault(title_text, 0)
-        titles[title_text] += 1
-
-        # Get new indent level
-        newindent = len(match.group(1))
-        if newindent > maxdepth: continue
-        if newindent < mindepth: continue
-        if not indent:
-            baseindent = newindent - 1
-            indent = baseindent
-
-        # Close lists
-        for i in range(0,indent-newindent):
-            result.append(macro.formatter.number_list(0))
-
-        # Open Lists
-        for i in range(0,newindent-indent):
-            result.append(macro.formatter.number_list(1))
-
-        # Add the heading
-        unique_id = ''
-        if titles[title_text] > 1:
-            unique_id = '-%d' % titles[title_text]
-
-        result.append(macro.formatter.listitem(1))
-        result.append(macro.formatter.anchorlink(
-            "head-" + sha.new(title_text).hexdigest() + unique_id, title_text))
-        result.append(macro.formatter.listitem(0))
-        
-        # Set new indent level
-        indent = newindent
+        match = include.match(line)
+        if match:
+            # this is an [[Include()]] line.
+            # now parse the included page and do the work on it.
+
+            ## get heading and level from Include() line.
+            args = r'^(?P<name>[^,]+),\s*(|(?P<hquote>[\'"])(?P<htext>.+?)(?P=hquote))' \
+                 + r',\s*(?P<level>\d*)'
+
+            tmp = re.search(args, match.group(1))
+            if tmp and tmp.group("name"):
+                inc_pagename = tmp.group("name")
+            else:
+                # no pagename?  ignore it
+                continue
+            if tmp.group("htext"):
+                heading = tmp.group("htext")
+                if tmp.group("level"):
+                    level = int(tmp.group("level"))
+                else:
+                    level = 1
+                tmp_line = "%s %s %s" % ("=" * level, heading, "=" * level)
+                inc_page_lines = [tmp_line]
+            else:
+                inc_page_lines = []
+
+            include_macro = wikiutil.importPlugin('macro', "Include")
+            inc_page_lines = inc_page_lines \
+                + include_macro(macro, match.group(1), called_by_toc=1).split("\n")
+            for inc_page_line in inc_page_lines:
+                parse_line(inc_page_line, macro, inc_pagename)
+        else:
+            parse_line(line, macro, pagename)
 
     # Close pending lists
     for i in range(baseindent, indent):


--- orig/MoinMoin/parser/wiki.py
+++ mod/MoinMoin/parser/wiki.py
@@ -731,15 +731,16 @@
             level = level+1
         depth = min(5,level)
 
+        pagename = self.formatter.page.page_name
         title_text = h[level:-level].strip()
-        self.titles.setdefault(title_text, 0)
-        self.titles[title_text] += 1
+        self.titles.setdefault(pagename + title_text, 0)
+        self.titles[pagename + title_text] += 1
 
         unique_id = ''
-        if self.titles[title_text] > 1:
-            unique_id = '-%d' % self.titles[title_text]
+        if self.titles[pagename + title_text] > 1:
+            unique_id = '-%d' % self.titles[pagename + title_text]
 
-        return self.formatter.heading(depth, self.highlight_text(title_text), icons=icons, id="head-"+sha.new(title_text).hexdigest()+unique_id)
+        return self.formatter.heading(depth, self.highlight_text(title_text), icons=icons, id="head-"+sha.new(pagename + title_text).hexdigest()+unique_id)
 
 
     def _processor_repl(self, word):




More information about the Moin-user mailing list