Skip to content

Commit

Permalink
Simplify tocgen.
Browse files Browse the repository at this point in the history
Now that the headers are all sane, there is no noeed for so much jiggery pokery.
  • Loading branch information
theory committed Oct 21, 2011
1 parent 39d1878 commit f6aafa5
Showing 1 changed file with 17 additions and 31 deletions.
48 changes: 17 additions & 31 deletions tocgen
@@ -1,53 +1,39 @@
#!/usr/bin/env perl -n -pi

our $prevn;
our $previd;
our %seen;

BEGIN {
$prevn = 0;
$previd = '';
print STDERR "<h1>Contents</h1>\n<ul>\n";
}

if (m{
<h([123])\s+id=" # 1. hn
(?:
L[.]code[.+]([^.]+)(?:[^"]+) # 2. funcname
| ([^"]+) # 3. id
)
<h([123])\s+id=" # 1. hn
([^"]+) # 3. id
">
( # 4. header
(<code>[^(]+)?.+? # 5. label
( # 4. header
(<code>[^(]+)?.+? # 5. label
)
</h\1>
}x) {
# Clean up the ID a bit.
my ($hn, $func, $id, $val, $label) = ($1, $2, $3, $4, $5);
$id = $func || $id;
$id =~ s{L?[.]code[.]}{}g;
$id =~ s{[.]{2,}}{.}g;
my $num = $seen{$id}++ || '';
my ($hn, $id, $val, $label) = ($1, $2, $3, $4);

if ($previd ne $id) {
$previd = $id;
if ($prevn) {
if ($hn != $prevn) {
print STDERR $hn > $prevn
? $hn - $prevn > 1
? "<ul><li><ul>" : "<ul>\n" : $prevn - $hn > 1
? "</li></ul></li></ul></li>\n" : "</li></ul></li>\n";
$prevn = $hn;
} else {
print STDERR "</li>\n"
}
} else {
if ($prevn) {
if ($hn != $prevn) {
print STDERR $hn > $prevn
? $hn - $prevn > 1
? "<ul><li><ul>" : "<ul>\n" : $prevn - $hn > 1
? "</li></ul></li></ul></li>\n" : "</li></ul></li>\n";
$prevn = $hn;
} else {
print STDERR "</li>\n"
}
print STDERR qq{<li><a href="#$id$num">} . ($5 ? "$5()</code>" : $val) . "</a>";
} else {
$prevn = $hn;
}
print STDERR qq{<li><a href="#$id">$val</a>};

$_ = qq{<h$hn id="$id$num">$val</h$hn>\n};
$_ = qq{<h$hn id="$id">$val</h$hn>\n};
}

END { print STDERR "</li>\n</ul>\n" }

0 comments on commit f6aafa5

Please sign in to comment.