forked from crawl/sequell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlearndb-html.pl
executable file
·138 lines (116 loc) · 2.86 KB
/
learndb-html.pl
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
#!/usr/bin/perl -w
# Script courtesy kilobyte on ##crawl.
# See original at http://angband.pl/crawl/henzell.
use HTTP::Date;
use strict;
use warnings;
my $timestamp = 0;
local $/;
my $title = "Henzell's learndb";
my %learndb;
my %redir;
my %link;
my $FULL_REDIRECT_PATTERN = qr/^see {([a-z0-9_\[\]!?@ -]+)}$/i;
sub addlink($$)
{
local $_=$_[0];
my $dest=$_[1];
#mixed _ and spaces?
${$redir{$dest}}{$_}=1;
$link{$_}=1;
tr{_}{ };
${$redir{$dest}}{$_}=1;
$link{$_}=1;
tr{ }{_};
${$redir{$dest}}{$_}=1;
$link{$_}=1;
}
for(split /\n/, `find dat/learndb/ -type f ! -name '*.html*'`)
{
open F, "<$_" or die "Can't read [$_]\n";
if (m:learndb/([a-z0-9_-]+)/(\d+):)
{
my $key = $1;
$key.="[$2]" if $2!='1';
my $val = <F>;
if ($val =~ $FULL_REDIRECT_PATTERN)
{
my $dest = $1;
$dest =~ s/\[1\]$//;
addlink($key, $dest);
}
else
{
addlink($key, $key);
}
$learndb{$key}=$val;
}
close F;
my @st;
$timestamp = $st[10] if @st=stat $_ and $st[10]>$timestamp;
}
my $embedded_css = do { local (@ARGV, $/) = 'learndb.css'; <> };
print <<EOF;
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>$title</title>
<style type="text/css">
$embedded_css
</style>
</head>
<body>
<h1>${title}</h1>
EOF
print "<p class='note'>Updated on ".time2str($timestamp)."\n";
print "<dl>\n";
sub canonical_link($)
{
my $link = lc(shift);
$link =~ s/\[1\]$//;
$link =~ tr/ /_/;
$link
}
sub htmlize($$$)
{
my ($entry, $multiple, $prefix) = @_;
for ($entry) {
s/&/&/g;
s/</</g;
s/>/>/g;
s{(http://[!#\$%&'*+,-./0-9:;=?\@A-Z^_a-z|~]+)}{<a href="$1">$1</a>}g;
my $key;
tr/\x00-\x1f//d;
s|{([a-zA-Z0-9_\[\]!?@ -]+)}| $link{canonical_link($1)} ? "<a href=\"#".canonical_link($1)."\">$1</a>" : "{$1}"|ge;
}
$multiple ? "<li>$prefix<span>$entry</span></li>" : "$prefix$entry"
}
for my $key (sort keys %learndb)
{
next if $key =~ /\]$/;
my $has_multiple = $learndb{"$key\[2]"};
next if !$has_multiple && $learndb{$key} =~ $FULL_REDIRECT_PATTERN;
print "<dt>";
print "<a name=\"$_\"></a>" for(sort keys %{$redir{$key}});
(my $clean_key = $key) =~ tr{_}{ };
print "$clean_key\n";
print " <dd>";
print "<ol>" if $has_multiple;
print htmlize($learndb{$key}, $has_multiple, ''), "\n";
my $i=1;
while($learndb{$key."[".++$i."]"})
{
my $text = $learndb{$key."[$i]"};
my $prefix = '';
$prefix .= "<a name=\"$_\"></a>" for(sort keys %{$redir{$key."[$i]"}});
print htmlize($text, $has_multiple, $prefix), "\n";
}
print "</ol>" if $has_multiple;
}
print <<EOF;
</dl>
</body>
</html>
EOF