wiki.pl: Port some fixes from upstream
[Orgmuse.git] / modules / toc-headers.pl
blobf81b24f6f87b5e333813b597fca7d1e8df7976bb
1 # Copyright (C) 2004 Alex Schroeder <alex@emacswiki.org>
2 # Copyright (C) 2006 Igor Afanasyev <afan@mail.ru>
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation; either version 2 of the License, or
7 # (at your option) any later version.
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the
16 # Free Software Foundation, Inc.
17 # 59 Temple Place, Suite 330
18 # Boston, MA 02111-1307 USA
20 # This is a simplified mix of headers.pl and toc.pl to work together.
21 # It is based on headers.pl 1.12 and toc.pl 1.30.
23 $ModulesDescription .= '<p><a href="http://git.savannah.gnu.org/cgit/oddmuse.git/tree/modules/toc-headers.pl">toc-headers.pl</a></p>';
25 use vars qw($MinTocSize $OrderedLists);
27 push(@MyRules, \&HeadersRule);
29 my $MinTocSize = 4; # show toc only if the number of headings is greater or equal to this value
30 my $OrderedLists = 0; # 1 if use <ol> instead of <ul>
32 my $TocCounter = 0; # private
33 my $TocShown = 0; # private
35 sub HeadersRule {
36 my $html = undef;
38 if (!$TocShown) {
39 $html = CloseHtmlEnvironments() . TocHeadings() . AddHtmlEnvironment('p');
40 $TocShown = 1;
43 if ($bol && (m/\G((.+?)[ \t]*\n(---+|===+)[ \t]*\n)/gc)) {
44 $html .= CloseHtmlEnvironments();
45 $TocCounter++;
46 $html .= "<a name=\"#$TocCounter\"></a>";
47 if (substr($3,0,1) eq '=') {
48 $html .= $q->h2($2);
49 } else {
50 $html .= $q->h3($2);
52 $html .= AddHtmlEnvironment('p');
55 return $html;
58 sub TocHeadings {
59 my $oldpos = pos; # make this sub not destroy the value of pos
60 my $page = $Page{text}; # work on the page that is currently open!
61 # ignore all the stuff that gets processed anyway
62 foreach my $tag ('nowiki', 'pre', 'code') {
63 $page =~ s|<$tag>(.*\n)*?</$tag>||gi;
65 my $Headings = "<h2>" . T('Contents') . "</h2>";
66 my $HeadingsLevel = undef;
67 my $HeadingsLevelStart = undef;
68 my $count = 1;
69 my $tag = $OrderedLists ? 'ol' : 'ul';
71 while ($page =~ m/((.+?)[ \t]*\n(---+|===+)[ \t]*\n)/g) {
72 my $depth = (substr($3,0,1) eq '=') ? 2 : 3;
73 my $text = $2;
74 next unless $text;
75 my $link = "$count"; #1, #2, etc. links seem to work fine
76 $text = QuoteHtml($text);
77 if (not defined $HeadingsLevelStart) {
78 # $HeadingsLevel is set to $depth - 1 so that we get an opening
79 # of the list. We need $HeadingsLevelStart to close all open
80 # tags at the end.
81 $HeadingsLevel = $depth - 1;
82 $HeadingsLevelStart = $depth - 1;
84 $count++;
85 # if the first subheading is has depth 2, then
86 # $HeadingsLevelStart is 1, and later subheadings may not be
87 # at level 1 or below.
88 $depth = $HeadingsLevelStart + 1 if $depth <= $HeadingsLevelStart;
89 # the order of the three expressions is important!
90 while ($HeadingsLevel > $depth) {
91 $Headings .= "</li></$tag>";
92 $HeadingsLevel--;
94 if ($HeadingsLevel == $depth) {
95 $Headings .= '</li><li>';
97 while ($HeadingsLevel < $depth) {
98 $Headings .= "<$tag class=\"h$depth\"><li>";
99 $HeadingsLevel++;
101 $Headings .= "<a href=\"#$link\">$text</a>";
103 while ($HeadingsLevel > $HeadingsLevelStart) {
104 $Headings .= "</li></$tag>";
105 $HeadingsLevel--;
107 pos = $oldpos;
108 return '' if $count <= $MinTocSize;
109 return $q->div({-class=>'toc'}, $Headings)
110 if $Headings;