[Home]WikiPatches/NumberedHeadings

UseModWiki | WikiPatches | RecentChanges | Preferences

This feature has been added to 1.0. --CliffordAdams
This patch will allow you to automatically give === headings === section numbers using very simple syntax.

== # heading2 =
=== # heading3 ==
==== # heading4 ===

would emit

1. heading2

1.1. heading3

1.1.1. heading4

Note that H1s aren't numbered because that looks really lame.

For the originating context, see MeatBall:NamedAnchorsInWiki. You may also want to apply the /TableOfContents patch after this.

-- SunirShah


Patch

Add @HeadingNumbers to the global variable list, we patch WikiHeading? ala:</nowiki>

 sub WikiHeadingNumber {
   my ($depth) = @_;
   return '' unless --$depth > 0;   # H1s aren't numbered because it looks stupid.
   push @HeadingNumbers, 1 while scalar @HeadingNumbers < ($depth-1);
   push @HeadingNumbers, 0 if scalar @HeadingNumbers < $depth;
   pop @HeadingNumbers while scalar @HeadingNumbers > $depth;
   $HeadingNumbers[$#HeadingNumbers]++;
   my $anchor = join '_', @HeadingNumbers;
   return &StoreHref(" name=\"$anchor\"") . (join '.', @HeadingNumbers) . '. ';
 }

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

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

Test

== # ...1 ==

== # ...2 ==

=== # ...2.1 ===

=== # ...2.2 ===

== # ...3 ==

==== # ...3.1.1 ====

=== # 3.2 ===

======= # 3.2.1.1.1.1 =======

==== # 3.2.2 ====

=== # 3.3 ===

UseModWiki | WikiPatches | RecentChanges | Preferences
Edit text of this page | View other revisions | Search MetaWiki
Last edited January 30, 2005 5:24 pm by MarkusLude (diff)
Search: