File: html-split

package info (click to toggle)
eb 4.4.3-14.4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,060 kB
  • sloc: ansic: 22,725; sh: 10,130; perl: 1,114; makefile: 706
file content (238 lines) | stat: -rwxr-xr-x 6,014 bytes parent folder | download | duplicates (10)
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
#! /usr/bin/perl
#
# Copyright (c) 2005-2006  Motoyuki Kasahara
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
# 3. Neither the name of the project nor the names of its contributors
#    may be used to endorse or promote products derived from this software
#    without specific prior written permission.
# 
# THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
#

#
# html-split -- split an HTML file.
#
# Usage:
#     html-split [option...] input-file
#
# `html-split' splits an HTML file with heading tags (<h1>...<h6>).
# Suppose that `input-file' is `foo.html', HTML files splitted by
# `html-split' are `foo-0.html', `foo-1.html', and so on.
#
# Options:
#     -Z                    do not add `-0' to the first split.
#     -l LEVEL              split with this heading level
#                           (default: h2)
#     -p PREFIX             prefix of splitted HTML files.
#     -s SUFFIX             suffix of splitted HTML files.
#                           (default: html)
#     -w WIDTH              minimum width of split number.
#                           (default: 1)
#     -t TOC                fragment name of `Table of Contents'.
#                           (default: toc)
#

require 5.005;
use Getopt::Std;
use File::Basename;

#
# Usage
#
my $usage = "Usage: $0 [option...] input-file\n";

#
# Variables
#
my $in_file;
my $out_prefix;
my $out_suffix;
my $counter_width = 1;
my $split_level = 2;
my $toc_tag = 'toc';
my $supress_zero_flag = 0;

my @toc = ();
my @indice = ();
my @preamble = ();
my $toc_page = 0;

#
# Parse command line arguments.
#
my %options;
getopts('Zl:w:p:s:t:', \%options) or die $usage;
die $usage if (@ARGV != 1);

$in_file = $ARGV[0];
if (defined($options{l})) {
    $options{l} =~ s/^h//;
    $split_level = $options{l};
}
$counter_width = $options{w} if (defined($options{w}));
$supress_zero_flag = defined($options{Z});
$toc_tag = $options{t} if (defined($options{t}));

if (defined($options{p})) {
    $out_prefix = $options{p};
} else {
    $out_prefix = basename($in_file, '.htm', '.html');
}

if (defined($options{s})) {
    $out_suffix = $options{s};
} elsif ($in_file =~ m|\.htm$|) {
    $out_suffix = 'htm';
} else {
    $out_suffix = 'html';
}

#
# Read an HTML file.
#
if (!open(IN_FILE, "< $in_file")) {
    die "$0: failed to open the file, $!: $in_file\n";
}

my $toc_found = 0;
my $page = 0;

while (<IN_FILE>) {
    last if (m|^<body>|);
    push(@preamble, $_);
}

while (<IN_FILE>) {
    chomp;
    last if (m|^</body>|);
    if (m|^<h([1-6])>| && $1 <= $split_level) {
	$page++ if (@toc > 0);
	push(@toc, $_);
    } 
    if (m|<a name=\"([^\"]+)\">|) {
	my $tag = $1;
	if ($tag eq $toc_tag) {
	    $toc_page = $page;
	    $toc_found = 1;
	}
	push(@indice, {'tag' => $tag, 'page' => $page});
    }
}

close(IN_FILE);

if (!$toc_found) {
    die "$0: <a name=\"$toc_tag\"> not found\n";
}

#
# Generate splitted HTML files.
#
if (!open(IN_FILE, "< $in_file")) {
    die "$0: failed to open the file, $!: $in_file\n";
}

while (<IN_FILE>) {
    last if (m|^<body>|);
}

for (my $page = 0; $page < @toc; $page++) {
    my $bar = '';
    if ($page > 0) {
	$bar .= sprintf("[<a href=\"%s\"></a>] ",
			splitted_file_name($page - 1));
    }

    if ($page + 1 < @toc) {
	$bar .= sprintf("[<a href=\"%s\"></a>] ",
			splitted_file_name($page + 1));
    }

    $bar .= sprintf("[<a href=\"%s\#%s\">ܼ</a>] ",
		    splitted_file_name($toc_page), $toc_tag);

    my $out_file = splitted_file_name($page);
    if (!open(OUT_FILE, "> $out_file")) {
	die "$0: failed to open the file, $!: $out_file\n";
    }

    foreach my $j (@preamble) {
	print OUT_FILE $j;
    }

    print OUT_FILE "<body>\n";
    print OUT_FILE "<p>\n", $bar, "\n</p>\n<hr>\n";
    print OUT_FILE $toc[$page], "\n";

    for (;;) {
	$_ = <IN_FILE>;
	chomp;
	if (!defined($_) || m|^</body>|) {
	    1 while (<IN_FILE>);
	    last;
	}
	elsif (m|^<h([1-6])>| && $1 <= $split_level) {
	    next if ($page == 0 && $_ eq $toc[$page]);
	    last;
	}
	1 while (s|<a href="\#([^\"]+)">|&rewrite_href($1)|e);
	print OUT_FILE $_, "\n";
    }

    print OUT_FILE "<hr>\n<p>\n", $bar, "\n</p>\n";
    print OUT_FILE "</body>\n";
    print OUT_FILE "</html>\n";

    close(OUT_FILE);
}

close(IN_FILE);

#
# Return n'th splitted file name.
#
sub splitted_file_name ($) {
    my ($n) = @_;

    if ($n == 0 && $supress_zero_flag) {
	return sprintf("%s.%s", $out_prefix, $out_suffix);
    } else {
	return sprintf("%s-%0${counter_width}d.%s",
		       $out_prefix, $n, $out_suffix);
    }
}

#
# Rewrite <a href="...">.
#
sub rewrite_href ($) {
    my ($tag) = @_;

    for (my $i = 0; $i < @indice; $i++) {
	if ($indice[$i]->{tag} eq $tag) {
	    return sprintf("<a href=\"%s\#%s\">", 
			   splitted_file_name($indice[$i]->{page}), $tag);
	}
    }

    warn "$0: unknown tag \`$tag'\n";
    return "<a href=\"$tag\">";
}