[Home]WikiPatches/TableOfContents

UseModWiki | WikiPatches | RecentChanges | Preferences


Since <toc> has been incorporated in 1.0, I added it as TextFormattingRules/TableOfContents [MeanRoy]
Building off of /NumberedHeadings, we can extend that patch to build a MeatBall:TableOfContents. For example, if you wrote the following MeatBall:WikiSyntax:

<toc>

== # Test ==

=== # Test (again) ===

== # SandBox SampleUndefinedPage ==

=== # I am ''italics'' and '''bold''' ===

==== # I am <b>bold</b> and <i>italics</i> ====

===     # I am a [http://example.com bracketed link]. ===

====  # https://www.usemod.org/meatball.gif ====

You would get something like

1. Test
1.1. Test (again)
2. SandBox SampleUndefinedPage
2.1. I am italics and bold
2.1.1. I am bold and italics
2.2. I am a [bracketed link].
2.2.1.

1. Test

1.1. Test (again)

2. SandBox SampleUndefinedPage?

2.1. I am italics and bold

2.1.1. I am bold and italics

2.2. I am a [bracketed link].

2.2.1.

for the MeatBall:TableOfContents, except each entry in the Table would be a link to its respective heading. -- SunirShah


Is there a bug in this code, or did I do something wrong? I was getting multiple copies of the table of contents when I hit the reload button on my browser until I reset $TableOfContents? in InitRequest -- JonStrayer

Patch

Make $TableOfContents a global as well as @HeadingNumbers if you haven't already, and change WikiHeading to be:

 sub WikiHeadingNumber {
     my ($depth,$text) = @_;
     my ($anchor, $number);
     return '' unless --$depth > 0;  # Don't number H1s because it looks stupid
 
     while (scalar @HeadingNumbers < ($depth-1)) {
         push @HeadingNumbers, 1;
         $TableOfContents .= '<dl><dt> </dt><dd>';
     }
     if (scalar @HeadingNumbers < $depth) {
         push @HeadingNumbers, 0;
         $TableOfContents .= '<dl><dt> </dt><dd>';
     }
     while (scalar @HeadingNumbers > $depth) {
         pop @HeadingNumbers;
         $TableOfContents .= "</dd></dl>\n\n";
     }
     $HeadingNumbers[$#HeadingNumbers]++;
     $number = (join '.', @HeadingNumbers) . '. ';
 
     # Remove embedded links. THIS IS FRAGILE!
     $text = &RestoreSavedText($text);
     $text =~ s/\<a\s[^\>]*?\>\?\<\/a\>//si; # No such page syntax
     $text =~ s/\<a\s[^\>]*?\>(.*?)\<\/a\>/$1/si;
 
     # Cook anchor by canonicalizing $text.
     $anchor = $text;
     $anchor =~ s/\<.*?\>//g;
     $anchor =~ s/\W/_/g;
     $anchor =~ s/__+/_/g;
     $anchor =~ s/^_//;
     $anchor =~ s/_$//;
     $anchor = '_' . (join '_', @HeadingNumbers) unless $anchor; # Last ditch effort
 
     $TableOfContents .= $number . &ScriptLink("$OpenPageName#$anchor",$text) . "</dd>\n<dt> </dt><dd>";
 
     return &StoreHref(" name=\"$anchor\"") . $number;
 }

 sub WikiHeading {
   my ($pre, $depth, $text) = @_;

   $depth = length($depth);
   $depth = 6  if ($depth > 6);
   $text =~ s/^#\s+/&WikiHeadingNumber($depth,$')/e; # $' == $POSTMATCH
   return $pre . "<H$depth>$text</H$depth>\n";
 }

 sub RestoreSavedText
 {
   my ($text) = @_;
   $text =~ s/$FS(\d+)$FS/$SaveUrl{$1}/ge;   # Restore saved text
   $text =~ s/$FS(\d+)$FS/$SaveUrl{$1}/ge;   # Restore nested saved text
   return $text;
 }

 sub WikiToHTML {
   ...
   $pageText = &WikiLinesToHtml($pageText);      # Line-oriented markup
   while (@HeadingNumbers) {
       pop @HeadingNumbers;
       $TableOfContents .= "</dd></dl>\n\n";
   }
   $pageText =~ s/&lt;toc&gt;/$TableOfContents/gi;
   return &RestoreSavedText($pageText);
 }

 sub GetDiffHTML {
   ...
   @HeadingNumbers = ();
   $TableOfContents = '';
   return $html;
 }

Note that WikiToHTML now returns after a call to &RestoreSavedText, which just moved the two lines that were previously there to their own function.

NextRelease


UseModWiki | WikiPatches | RecentChanges | Preferences
Edit text of this page | View other revisions | Search MetaWiki
Last edited November 6, 2023 9:07 pm by MarkusLude (diff)
Search: