--- moin-1.6.3/MoinMoin/macro/Include.py	2008-02-20 06:46:33.000000000 +0900
+++ pkg/x86_64/lib/python2.4/site-packages/MoinMoin/macro/Include.py	2008-05-27 12:15:42.507439000 +0900
@@ -32,12 +32,15 @@
 
 _title_re = r"^(?P<heading>\s*(?P<hmarker>=+)\s.*\s(?P=hmarker))$"
 
+_heading_re = r'<h(?P<level>\d) id="(?P<id>[^"]+?)">(?P<title>.+?)</h(?P=level)>'
+
 def extract_titles(body, title_re):
     titles = []
     for title, _ in title_re.findall(body):
@@ -169,10 +180,6 @@
         ##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
-
         if not hasattr(request, "_Include_backto"):
             request._Include_backto = this_page.page_name
 
@@ -219,7 +226,7 @@
             result.append(strfile.getvalue())
         finally:
             request.redirect()
-
+            
         # decrement or remove include marker
         if this_page._macroInclude_pagelist[inc_name] > 1:
             this_page._macroInclude_pagelist[inc_name] = \
@@ -237,6 +244,12 @@
             ])
         # XXX page.link_to is wrong now, it escapes the edit_icon html as it escapes normal text
 
+    if called_by_toc:
+        # find all headings in the HTML result and return them as a list of tuples (level, id, text)
+        heading_re=re.compile(_heading_re)
+        match = heading_re.findall( ''.join(result) )
+        return match
+
     # return include text
     return ''.join(result)
 
--- moin-1.6.3/MoinMoin/macro/TableOfContents.py	2008-03-29 06:15:00.000000000 +0900
+++ pkg/x86_64/lib/python2.4/site-packages/MoinMoin/macro/TableOfContents.py	2008-05-27 12:10:51.572445000 +0900
@@ -14,21 +14,7 @@
 #Dependencies = ["page"]
 Dependencies = ["time"] # works around MoinMoinBugs/TableOfContentsLacksLinks
 
-# from macro Include (keep in sync!)
-_arg_heading = r'(?P<heading>,)\s*(|(?P<hquote>[\'"])(?P<htext>.+?)(?P=hquote))'
-_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)))?'
-_arg_items = r'(,\s*items=(?P<items>\d+))?'
-_arg_skipitems = r'(,\s*skipitems=(?P<skipitems>\d+))?'
-_arg_titlesonly = r'(,\s*(?P<titlesonly>titlesonly))?'
-_arg_editlink = r'(,\s*(?P<editlink>editlink))?'
-_args_re_pattern = r'^(?P<name>[^,]+)(%s(%s)?%s%s%s%s%s%s%s)?$' % (
-    _arg_heading, _arg_level, _arg_from, _arg_to, _arg_sort, _arg_items,
-    _arg_skipitems, _arg_titlesonly, _arg_editlink)
-
-# from Include, too, but with extra htext group around header text
+# from Include macro, but with extra htext group around header text
 _title_re = r"^(?P<heading>(?P<hmarker>=+)\s(?P<htext>.*)\s(?P=hmarker))$"
 
 class TableOfContents:
@@ -41,7 +27,6 @@
         self._ = self.macro.request.getText
 
         self.inc_re = re.compile(r"^\<\<Include\((.*)\)\>\>")
-        self.arg_re = re.compile(_args_re_pattern)
         self.head_re = re.compile(_title_re) # single lines only
         self.pre_re = re.compile(r'\{\{\{.+?\}\}\}', re.S)
 
@@ -67,7 +52,7 @@
         if self.include_macro is None:
             self.include_macro = wikiutil.importPlugin(self.macro.request.cfg,
                                                        'macro', "Include")
-        return self.pre_re.sub('', self.include_macro(*args, **kwargs)).split('\n')
+        return self.include_macro(*args, **kwargs)
 
     def run(self):
         _ = self._
@@ -92,31 +77,16 @@
             match = self.inc_re.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.
-                tmp = self.arg_re.match(match.group(1))
-                if tmp and tmp.group("name"):
-                    inc_pagename = tmp.group("name")
-                else:
-                    # no pagename?  ignore it
-                    continue
-                if tmp.group("heading") and tmp.group("hquote"):
-                    if tmp.group("htext"):
-                        heading = tmp.group("htext")
-                    else:
-                        heading = inc_pagename
-                    if tmp.group("level"):
-                        level = int(tmp.group("level"))
-                    else:
-                        level = 1
-                    inc_page_lines = ["%s %s %s" % ("=" * level, heading, "=" * level)]
-                else:
-                    inc_page_lines = []
-
-                inc_page_lines = inc_page_lines + self.IncludeMacro(self.macro, match.group(1), called_by_toc=1)
-
-                self.process_lines(inc_page_lines, inc_pagename)
+                #
+                # save and restore request._page_headings, otherwise all heading ids
+                # will receive counters due to multiple IncludeMacro call
+                saved_page_headings = self.macro.request._page_headings.copy()
+                # get headings from the included page
+                inc_result = self.IncludeMacro(self.macro, match.group(1), called_by_toc=1)
+                self.macro.request._page_headings = saved_page_headings
+                # add the headings to the TOC
+                for h in inc_result:
+                    self.add_toc_line( int(h[0]), h[1], h[2] )
             else:
                 self.parse_line(line, pagename)
 
@@ -130,8 +100,14 @@
         self.titles.setdefault(pntt, 0)
         self.titles[pntt] += 1
 
-        # Get new indent level
-        newindent = len(match.group('hmarker'))
+        unique_id = ''
+        if self.titles[pntt] > 1:
+            unique_id = '-%d' % (self.titles[pntt],)
+            
+        self.add_toc_line( len(match.group('hmarker')), "head-" + sha.new(pntt.encode(config.charset)).hexdigest() + unique_id, title_text )
+
+
+    def add_toc_line(self, newindent, anchor, title_text):
         if newindent > self.maxdepth or newindent < self.mindepth:
             return
         if not self.indent:
@@ -148,20 +124,15 @@
             self.result.append(self.macro.formatter.number_list(1))
             self.result.append(self.macro.formatter.listitem(1))
 
-        # Add the heading
-        unique_id = ''
-        if self.titles[pntt] > 1:
-            unique_id = '-%d' % (self.titles[pntt],)
-
         # close last listitem if same level
         if self.indent == newindent:
             self.result.append(self.macro.formatter.listitem(0))
 
+        # Add the heading
         if self.indent >= newindent:
             self.result.append(self.macro.formatter.listitem(1))
-        self.result.append(self.macro.formatter.anchorlink(1,
-            "head-" + sha.new(pntt.encode(config.charset)).hexdigest() + unique_id) +
+        self.result.append(self.macro.formatter.anchorlink( 1, anchor ) +
                            self.macro.formatter.text(title_text) +
                            self.macro.formatter.anchorlink(0))
 
         # Set new indent level
