--- TableOfContents.old.py	2009-02-08 01:27:07.000000000 +0100
+++ TableOfContents.py	2009-03-27 02:28:16.000000000 +0100
@@ -149,11 +149,6 @@
 
     tocfm = TOCFormatter(macro.request)
     p = Page(macro.request, pname, formatter=tocfm, rev=macro.request.rev)
-
-    # this is so we get a correctly updated TOC if we just preview in the editor -
-    # the new content is not stored on disk yet, but available as macro.parser.raw:
-    p.set_raw_body(macro.parser.raw, modified=1)
-
     output = macro.request.redirectedOutput(p.send_page,
                                             content_only=True,
                                             count_hit=False,
@@ -162,66 +157,88 @@
     _ = macro.request.getText
 
     result = [
-        macro.formatter.div(1, css_class="table-of-contents"),
+        macro.formatter.div(1, css_class="table-of-contents", id="toc"),
         macro.formatter.paragraph(1, css_class="table-of-contents-heading"),
         macro.formatter.text(_('Contents')),
         macro.formatter.paragraph(0),
     ]
 
+    lastlvl = 0
+
+    # variables needed to provide with nice, leveled prefixes
+    levelnumbers = None
+    levelnumbers = {}
+    counter = 1
+    lowest_lvl = None
 
-    # find smallest used level and use that as the outer-most indentation,
-    # to fix pages like HelpOnMacros that only use h2 and lower levels.
-    lastlvl = 100
     for lvl, id, txt in macro.request._tocfm_collected_headings:
+
         if txt is None:
             incl_id = id
             continue
         if lvl > maxdepth or id is None:
             continue
-        if lvl < lastlvl:
-            lastlvl = lvl
 
-    # headings are 1-based, lastlvl needs to be one less so that one is closed
-    lastlvl -= 1
+        # determine the lowest level available
+        if lowest_lvl is None or lowest_lvl > lvl:
+                lowest_lvl = lvl
+
+        # determine number prefix for the TOC line
+        if lvl > lastlvl:
+                levelnumbers[lastlvl] = counter
+                counter = 0
+        elif lvl < lastlvl:
+                counter = levelnumbers[lvl]
+
+        counter = counter + 1
+        levelnumbers[lvl] = counter
+
+        line_number = ""
+        for i in range(lowest_lvl, lvl):
+                line_number = line_number + str(levelnumbers[i]) + "."
+        line_number = line_number + str(counter) + "."
 
-    for lvl, id, txt in macro.request._tocfm_collected_headings:
-        if txt is None:
-            incl_id = id
-            continue
-        if lvl > maxdepth or id is None:
-            continue
 
         # will be reset by pop_unique_ids below
         macro.request.include_id = incl_id
 
         need_li = lastlvl >= lvl
-        while lastlvl > lvl:
-            result.extend([
-                macro.formatter.listitem(0),
-                macro.formatter.number_list(0),
-            ])
-            lastlvl -= 1
+
+
+        # adjust lastlvl so that we can start lists from the lowest_lvl
+        if lastlvl < lvl and lastlvl < lowest_lvl: lastlvl = lvl - 1
+        if lastlvl > lvl and lastlvl < lowest_lvl: lastlvl = lvl + 1
+
+
+        # open and close corresponding lists
         while lastlvl < lvl:
             result.extend([
-                macro.formatter.number_list(1),
+                macro.formatter.bullet_list(1, type="none"),
                 macro.formatter.listitem(1),
             ])
             lastlvl += 1
+        while lastlvl > lvl:
+            result.extend([
+                macro.formatter.listitem(0),
+                macro.formatter.bullet_list(0),
+            ])
+            lastlvl -= 1
         if need_li:
             result.extend([
                 macro.formatter.listitem(0),
                 macro.formatter.listitem(1),
             ])
+
         result.extend([
             '\n',
             macro.formatter.anchorlink(1, id),
-            macro.formatter.text(txt),
+            macro.formatter.text(line_number + " " + txt),
             macro.formatter.anchorlink(0),
         ])
 
-    while lastlvl > 0:
+    while lastlvl > lvl:
         result.append(macro.formatter.listitem(0))
-        result.append(macro.formatter.number_list(0))
+        result.append(macro.formatter.bullet_list(0))
         lastlvl -= 1
 
     macro.request.pop_unique_ids()
