-
Notifications
You must be signed in to change notification settings - Fork 5
/
epubtohtml.pl
executable file
·217 lines (178 loc) · 6.45 KB
/
epubtohtml.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
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
#!/usr/bin/perl -W
#
# Converts an epub document into a document that can be natively read by a web-browser.
#
# Algorithm:
#
# * Unzip epub file
# * do text substitutions as necessary
# * create a navigation index
#
# Copyright 2011 Joshua Richardson, Chegg Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
use Getopt::Long;
use XML::Simple;
use Data::Dumper;
my($help, $outdirectory, $base_fn, $opfxml);
my($PROG) = $0 =~ m@/([^/]+)$@;
sub usage {
print STDERR "$0 usage:\n";
print STDERR "\t$PROG [options] <filename>\n";
print STDERR "\t-h|--help : print usage info\n";
print STDERR "\t-o|--out : output directory\n";
}
# Initialize
Getopt::Long::Configure ("bundling");
# Process commandline options
my($args_ok, $nonoption_args) = GetOptions ('help|h' => \$help, 'out|o=s' => \$outdirectory);
$nonoption_args = \@ARGV;
if (!$args_ok) { usage(); die "failed to process options"; }
if (!defined($nonoption_args)) { usage(); die "missing required args"; }
my($inputfilename, @unexpected_args) = @$nonoption_args;
if (!defined($inputfilename)) { usage(); die('not enough args'); }
if (@unexpected_args) { usage(); die('too many args'); }
if (defined($outdirectory) && -e $outdirectory && ! -d $outdirectory)
{ usage(); die("$outdirectory is not a directory"); }
if (defined($help)) { usage(); exit(0); }
if (!defined($outdirectory)) {
# Set the output directory == input directory/epubfilename without epub extension
($outdirectory, $filename_ext) = $inputfilename =~ m@(.+)\.([^.]+)$@;
if (!defined($filename_ext) || lc($filename_ext) ne 'epub')
{ die("\"$inputfilename\" does not have a recognized epub file extension."); }
}
($base_fn) = $inputfilename =~ m@([^./]+)\.[^.]+$@;
if (!defined($base_fn)) { die "Unable to get base of the filename"; }
# Unzip epub file
my($UNZIP_CMD) = "unzip -d '$outdirectory' '$inputfilename'";
system($UNZIP_CMD) && die("failed ($!): $UNZIP_CMD");
# Process epub files
opendir(DIR, $outdirectory) || die("Cannot open directory \"$outdirectory\"");
my(@epub_files) = grep(!m/^\./, readdir(DIR)); # filter out hidden files
closedir(DIR);
## Process html files
my(@html_files) = map("$outdirectory/$_", grep(m/\.x?html/i, @epub_files));
processHtmlFiles(\@html_files) && die("failed to process the html files");
## Create navigation
my($opfn) = grep(m/.opf$/, @epub_files);
if (!defined($opfn))
{ print STDERR "$PROG: WARN: No opf file to get meta-data; will not create index.\n"; exit 0; }
parseOPF($opfn) && do { print STDERR "$PROG: ERROR: Unable to parse $opfn.\n"; exit 1; };
(createIndexFile() || createFrames()) && print STDERR "$PROG: WARN: Failed to create index file.\n";
sub createFrames {
my($first_page_id) = $opfxml->{'spine'}->{'itemref'}->[0]->{'idref'};
if (!defined($first_page_id))
{ print STDERR "$PROG: ERROR: Failed to find first content page in OPF spine, $opfn\n"; return 1; }
my($first_page) = $opfxml->{'manifest'}->{'item'}->{$first_page_id}->{'href'};
my($debug) = $opfxml->{'manifest'}->{'item'};
print Dumper($debug, $first_page_id);
if (!defined($first_page))
{ print STDERR "$PROG: ERROR: Failed to find named content page in OPF, $opfn\n"; return 1; }
my $data = <<END
<!DOCTYPE HTML>
<HTML>
<HEAD>
<TITLE></TITLE>
<META http-equiv="Content-Type" content="text/html; charset=UTF-8">
<META name="generator" content="epubtohtml 1.0">
</HEAD>
<FRAMESET cols="100,*">
<FRAME name="navigation" src="index_idx.html">
<FRAME name="content" src="$first_page">
</FRAMESET>
</HTML>
END
;
my($idx_fn) = "$outdirectory/index.html";
if (-e $idx_fn) {
print STDERR "$PROG: NOTICE: $idx_fn exists, using ";
$idx_fn = "$outdirectory/index-epubtohtml.html";
print STDERR "$idx_fn instead.\n";
}
open(IDX_F, ">$idx_fn") || die($!);
print IDX_F $data;
close(IDX_F) || die($!);
return 0;
}
sub parseOPF {
my($opfn) = @_;
# Parse OPF
$opfxml = XMLin("$outdirectory/$opfn");
if (!defined($opfxml)) { print STDERR "$PROG: WARN: Failed to open/parse $opfn: $!\n"; return 1; }
#print Dumper($opfxml);
return 0;
}
sub createIndexFile {
my($body) = '';
# Parse OPF
my $opfxml = XMLin("$outdirectory/$opfn");
if (!defined($opfxml)) { print STDERR "$PROG: WARN: Failed to open/parse $opfn: $!\n"; return 1; }
#print Dumper($opfxml);
# Create html body
$body .= "<ul>\n";
my($top_level) = $opfxml->{'spine'}->{'itemref'};
if (!defined($top_level))
{ print STDERR "$PROG: WARN: Unable to get spine. Malformed OPF, $opfn?\n"; return 1; }
foreach my $item (@$top_level) {
my($text, $link) = $item->{'idref'};
$link = $opfxml->{'manifest'}->{'item'}->{$text}->{'href'};
if (!defined($link)) {
print STDERR
"$PROG: WARN: Unable to find item $text in manifest of $opfn. Omitting $text from index.\n";
next;
}
$body .= "\t<li><a href=\"$link\" target=\"content\">$text</a></li>\n";
}
$body .= "</ul>\n";
# Create index html
my($data) = <<END;
<!DOCTYPE HTML">
<html>
<head>
<title>Index for $opfxml->{'metadata'}->{'dc:title'}</title>
<style type="text/css">
body ul { padding: 0 }
</style>
</head>
<body>
$body
</body>
</html>
END
my($idx_fn) = "$outdirectory/index_idx.html";
if (-e "$outdirectory/index_idx.html")
{ print STDERR "$PROG: WARN: $idx_fn already exists. Unable to create index file.\n"; return 1; }
open(IDX_F, ">$outdirectory/index_idx.html") || die($!);
print IDX_F $data;
close(IDX_F) || die($!);
return 0;
}
sub processHtmlFile {
my($filename) = @_;
open(F, "<$filename") ||
do { print STDERR "$PROG: WARN: failed to open $filename for reading.\n"; return 1; };
my($data) = join('', <F>);
close(F) || do { print STDERR "$PROG: WARN: failed to close $filename.\n"; return 2; };
$data =~ s@text/x-oeb1-css@text/css@gi;
open(F, ">$filename") ||
do { print STDERR "$PROG: WARN: failed to open $filename for writing.\n"; return 3; };
print F $data;
close(F) || do { print STDERR "$PROG: WARN: failed to close $filename.\n"; return 4; };
return 0;
}
sub processHtmlFiles {
my($files) = @_;
my($result) = 0;
map($result += processHtmlFile($_), @$files);
return $result;
}