-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhtmltoc
executable file
·155 lines (133 loc) · 4.22 KB
/
htmltoc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#!/usr/bin/perl
# Generate Table Of Contents from headings in (x)HTML
# Copyright (C) 2008-2010 Bruno BEAUFILS <[email protected]>
#
# Code borrowed from IkiWiki::Plugin:toc before modification
#
# It replaces the content of the <div> identified by `toc` with a
# table of contents generated from headings found in the HTML file
# specified, or standard input if none specified.
#
# This software comes with ABSOLUTELY NO WARRANTY.
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation in its version 2.
# See the README or COPYING file for details.
use warnings;
use strict;
use HTML::Parser;
use Getopt::Long;
# Global default setting
my $levels=5;
# Check for command-line switches
Getopt::Long::Configure('pass_through');
GetOptions('levels=i'=>\$levels) or die "usage: $0 [--levels LEVELS] [FILE]";
# Defining content to deal with
my $content;
if ($#ARGV >= 0)
{
open(STDIN, "< $ARGV[0]") || die("$0: can't open datafile: $!");
}
while (<STDIN>)
{
$content .= $_;
}
my $page = "";
my $index = "";
my %anchors;
my $curlevel;
my $startlevel = 0;
my $liststarted = 0;
my $indent = sub { "\t" x $curlevel };
my $oldanchor;
my $oldstart;
# Parsing the file
my $p = HTML::Parser->new(api_version => 3);
$p->handler(start => \&start, "tagname, text");
$p->handler(default => sub { $page .= join("", @_); }, "text");
$p->parse($content);
$p->eof;
# Generating the toc
while ($startlevel && $curlevel >= $startlevel) {
$index .= &$indent."</li>\n" if $curlevel;
$curlevel --;
$index .= &$indent."</ol>\n";
}
# Replacing in toc in content
$page =~ s@(<div id=[\"\']toc[\"\']>).*(</div>)@$1\n$index\n$2@mg;
# Output the result
print "$page";
1;
##############################################################################
sub start {
my $tagname=shift;
my $text=shift;
if ($tagname =~ /^h(\d+)$/i) {
my $level=$1;
my $anchor="index".++$anchors{$level}."h$level";
$oldanchor=0;
$page.="$text<a name=\"$anchor\"></a>";
# Take the first header level seen as the topmost level,
# even if there are higher levels seen later on.
if (! $startlevel) {
$startlevel=$level;
$curlevel=$startlevel-1;
}
elsif ($level < $startlevel) {
$level=$startlevel;
}
return if $level - $startlevel >= $levels;
if ($level > $curlevel) {
while ($level > $curlevel + 1) {
$index.=&$indent."<ol>\n";
$curlevel++;
$index.=&$indent."<li class=\"L$curlevel\">\n";
}
$index.=&$indent."<ol>\n";
$curlevel=$level;
$liststarted=1;
}
elsif ($level < $curlevel) {
while ($level < $curlevel) {
$index.=&$indent."</li>\n" if $curlevel;
$curlevel--;
$index.=&$indent."</ol>\n";
}
$liststarted=0;
}
$index.=&$indent."</li>\n" unless $liststarted;
$liststarted=0;
$index.=&$indent."<li class=\"L$curlevel\">".
"<a href=\"#$anchor\">";
$p->handler(start => sub {
my $tagname=shift;
if ($tagname eq 'a' ){ # FIXME: il faudrait s'assurer qu'il y a un attribut name
$oldanchor = 1;
}
}, "tagname, text");
$p->handler(text => sub {
if ($oldanchor != 1) {
$page.=join("", @_);
$index.=join("", @_);
}
}, "text");
$p->handler(end => sub {
my $tagname=shift;
if ($tagname =~ /^h(\d+)$/i) {
$p->handler(start => \&start);
$p->handler(text => undef);
$p->handler(end => undef);
$index.="</a>";
$page.=join("", @_);
} elsif ($tagname eq 'a') {
$oldanchor = 0;
} else {
$page.=join("", @_);
}
}, "tagname, text");
}
else {
$page.=$text;
}
}
##############################################################################