[Home]WikiPatches/TableOfContentsForUnnumberedHeadings

UseModWiki | WikiPatches | RecentChanges | Preferences

The /TableOfContents patch was added to 1.0, but it only works for numbered headings. I wanted to be able to use the feature in topics where I don't number the headings. The patch below accomplishes this. Feel free to improve on it!

-- DavidBright

I absolutely agree: TOC should work an any headings. This restriction just makes no sense to me. Please, Team, include this change in the next version. -- KarstenSievert

Patch

The patch is in unified diff format.

--- ../usemod10/wiki.pl	2003-09-11 07:21:02.000000000 -0500
+++ wiki.pl	2003-09-23 09:08:56.000000000 -0500
@@ -1681,7 +1715,7 @@
     s/('*)'''(.*?)'''/$1<strong>$2<\/strong>/g;
     s/''(.*?)''/<em>$1<\/em>/g;
     if ($UseHeadings) {
-      s/(^|\n)\s*(\=+)\s+([^\n]+)\s+\=+/&WikiHeading($1, $2, $3)/geo;
+      s/(^|\n)\s*(\=+)\s*(#)?\s+([^\n]+)\s+\=+/&WikiHeading($1, $2, $4, $3)/geo;
     }
     if ($TableMode) {
       s/((\|\|)+)/"<\/TD><TD COLSPAN=\"" . (length($1)\/2) . "\">"/ge;
@@ -2093,7 +2127,7 @@
 }
 
 sub WikiHeadingNumber {
-    my ($depth, $text) = @_;
+    my ($depth, $text, $useNumber) = @_;
     my ($anchor, $number);
 
     return '' unless --$depth > 0;  # Don't number H1s because it looks stupid
@@ -2124,17 +2158,26 @@
     $anchor =~ s/_$//;
     # Last ditch effort
     $anchor = '_' . (join '_', @HeadingNumbers) unless $anchor;
-    $TableOfContents .= $number . &ScriptLink("$OpenPageName#$anchor",$text)
-                        . "</dd>\n<dt> </dt><dd>";
+    if ($useNumber) {
+      $TableOfContents .= $number . &ScriptLink("$OpenPageName#$anchor",$text)
+                          . "</dd>\n<dt> </dt><dd>";
+    } else {
+      $TableOfContents .= &ScriptLink("$OpenPageName#$anchor",$text)
+                          . "</dd>\n<dt> </dt><dd>";
+    }
     return &StoreHref(" name=\"$anchor\"") . $number;
 }
 
 sub WikiHeading {
-  my ($pre, $depth, $text) = @_;
+  my ($pre, $depth, $text, $useNumber) = @_;
 
   $depth = length($depth);
   $depth = 6  if ($depth > 6);
-  $text =~ s/^\s*#\s+/&WikiHeadingNumber($depth,$')/e; # $' == $POSTMATCH
+  if ($useNumber) {
+    $text =~ &WikiHeadingNumber($depth,$text, 1);
+  } else {
+    $text =~ &WikiHeadingNumber($depth,$text, 0);
+  }
   return $pre . "<H$depth>$text</H$depth>\n";
 }
 


Oct 14 2003 : i just applied this patch successfully (no errors during patch application), it seems to work even without numbered headers, but
... it seems not to insert the <A NAME="#whatever"> tag ... so the whole toc is pretty useless finally.. Anyone had this problem too ? Or has something bad happened with my setup ?


I had the same problem. I would love to have this one too... Drat. --MarkButler


Actually, this patch doesn't work at all. In fact, it reduces the amount of functionality because it also renders ToCs? created from number headings useless. It's a good idea, but this patch makes things worse. -- GordonWorley (6 August 2004)

Try an alternate token for un-numbered headings (##) like so:

In sub WikiHeading?:

  $text =~ s/^\s*#\s+/&WikiHeadingNumber($depth,$')/e; # $' == $POSTMATCH
+ $text =~ s/^\s*##\s+/&WikiHeadingNumber($depth,$',1)/e; # $' == $POSTMATCH

In sub WikiHeadingNumber?:

- my ($depth, $text) = @_;
+ my ($depth, $text, $nonum) = @_;

...

- $number = (join '.', @HeadingNumbers) . '. ';
+ $number = (join '.', @HeadingNumbers) . '. ' unless $nonum;
This also supports both # and ## in the same document for a mix of the two.

-- JohnDoe?

Bloody briliant! Works like a charm. -- PEZ


a little difference to DavidBright's. Which is a MediaWiki like toc, numbered in TOC and add anchors in text.

--- wiki.pl.orig	2005-10-31 01:13:56.156250000 +0800
+++ wiki.pl	2005-10-31 01:22:31.843750000 +0800
@@ -1681,7 +1681,7 @@
     s/('*)'''(.*?)'''/$1<strong>$2<\/strong>/g;
     s/''(.*?)''/<em>$1<\/em>/g;
     if ($UseHeadings) {
-      s/(^|\n)\s*(\=+)\s+([^\n]+)\s+\=+/&WikiHeading($1, $2, $3)/geo;
+      s/(^|\n)\s*(\=+)\s*(#)?\s+([^\n]+)\s+\=+/&WikiHeading($1, $2, $4, $3)/geo;
     }
     if ($TableMode) {
       s/((\|\|)+)/"<\/TD><TD COLSPAN=\"" . (length($1)\/2) . "\">"/ge;
@@ -2093,7 +2093,7 @@
 }
 
 sub WikiHeadingNumber {
-    my ($depth, $text) = @_;
+    my ($depth, $text, $useNumber) = @_;
     my ($anchor, $number);
 
     return '' unless --$depth > 0;  # Don't number H1s because it looks stupid
@@ -2126,15 +2126,23 @@
     $anchor = '_' . (join '_', @HeadingNumbers) unless $anchor;
     $TableOfContents .= $number . &ScriptLink("$OpenPageName#$anchor",$text)
                         . "</dd>\n<dt> </dt><dd>";
-    return &StoreHref(" name=\"$anchor\"") . $number;
+    if ($useNumber) {
+      return &StoreHref(" name=\"$anchor\"") . $number;
+    } else {
+      return &StoreHref(" name=\"$anchor\"");
+    }
 }
 
 sub WikiHeading {
-  my ($pre, $depth, $text) = @_;
+  my ($pre, $depth, $text, $useNumber) = @_;
 
   $depth = length($depth);
   $depth = 6  if ($depth > 6);
-  $text =~ s/^\s*#\s+/&WikiHeadingNumber($depth,$')/e; # $' == $POSTMATCH
+  if ($useNumber) {
+    $text = &WikiHeadingNumber($depth,$text, 1) . $text;
+  } else {
+    $text = &WikiHeadingNumber($depth,$text, 0) . $text;
+  }
   return $pre . "<H$depth>$text</H$depth>\n";
 }

-- Johnson


UseModWiki | WikiPatches | RecentChanges | Preferences
Edit text of this page | View other revisions | Search MetaWiki
Last edited May 4, 2008 10:44 pm by KarstenSievert (diff)
Search: