File: mkindex

package info (click to toggle)
freepwing 1.4.3-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 844 kB
  • ctags: 293
  • sloc: perl: 4,831; sh: 3,085; makefile: 191
file content (100 lines) | stat: -rwxr-xr-x 2,493 bytes parent folder | download
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
#                                                         -*- Perl -*-
# Copyright (C) 1999, 2000  Motoyuki Kasahara
#
# 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; either version 2, or (at your option)
# any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#

require 5.005;

use English;
use FileHandle;

sub iso2022jp_to_eucjp {
    my ($line) = shift;
    my ($f) = sub {my $l = shift; $l =~ tr/\041-\176/\241-\376/; return $l;};
    $line =~ s/\033\$B(([\041-\176]{2})*)\033\(B/&$f($1)/eg;
    return $line;
}

sub eucjp_to_iso2022jp {
    my ($line) = shift;
    my ($f) = sub {my $l = shift; $l =~ tr/\241-\376/\041-\176/; return $l;};
    $line =~ s/(([\241-\376]{2})*)/"\033\$B".&$f($1)."\033\(B"/eg;
    return $line;
}

if (@ARGV != 1) {
    die "usage: $0 HTML-file-name\n";
}

$filename = shift;
$handle = FileHandle->new();
if (!$handle->open($filename)) {
    die "$0: failed to open the file, $ERRNO: $filename\n";
}

for (;;) {
    $_ = $handle->getline();
    if (!defined($_)) {
	last;
    }
    $_ = iso2022jp_to_eucjp($_);

    if (/^<body>/) {
	last;
    } elsif (/^<title>/) {
	($title) = /^<title>(.*)<\/title>/;
	$title = eucjp_to_iso2022jp($title);
    }
}

print "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\">\n";
print "<html lang=\"ja\">\n";
print "<head>\n";
print "<meta http-equiv=\"Content-Type\" ";
print "content=\"text/html; charset=iso-2022-jp\">\n";
print "<link rel=\"stylesheet\" type=\"text/css\" href=\"freepwing.css\">\n";
print "<title>$title</title>\n";
print "</head>\n";
print "<body>\n";
print "<h1>$title</h1>\n";
print "<ul>\n";

$curlevel = 1;

for (;;) {
    $_ = $handle->getline();
    if (!defined($_)) {
	last;
    }
    chomp;
    $_ = iso2022jp_to_eucjp($_);

    if (($level) = /^<h([1-6])>/) {
	($tag, $head) = /<a name=\"([^\"]+)\">(.*)<\/a>/;
	while ($level < $curlevel) {
	    print "</ul>\n";
	    $curlevel--;
	}
	while ($curlevel < $level) {
	    print "<ul>\n";
	    $curlevel++;
	}
	$head = eucjp_to_iso2022jp($head);
	print "<li><a href=\"$filename#$tag\">$head</a>\n";
    }
}

for ($i = $curlevel; 1 <= $i; $i--) {
    print "</ul>\n";
}
print "</body>\n";
print "</html>\n";