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
|
#!/usr/bin/perl -w
=pod
=head1 NAME
unfb2 - convert FB2 (FictionBook) files to HTML or plain text
=head1 SYNOPSIS
unfb2 [-h] [-l] [-x] [-2] [filename.fb2]
=head1 DESCRIPTION
unfb2 converts a FB2 book to HTML or text. This is a "quick & dirty"
conversion. No attempt to save the metadata in the <description> of
the document is made. Binary objects (e.g. images) are not included in
the output.
By default, unfb2 converts <filename>.fb2 to <filename>.txt. With the
B<-h> option, the output is <filename>.html. The output file is always
in the current directory; if it already exists, unfb2 will refuse to
overwrite it without the B<-f> option.
When converting to text, the FB2 is first converted to HTML, then run
through one of three html-to-text converters, depending on the option
given (default is links).
=head1 OPTIONS
=over 4
=item B<--help>
You're reading it now.
=item B<--man>
Output the --help in troff format, suitable for use as a man page.
=item B<-f>
Forcibly overwrite the output file if it already exists.
=item B<-h>
Convert to HTML rather than text.
=item B<-l>
Convert HTML to text with this command:
links -html-margin 0 -dump $file.html > file.txt
This is actually the default, when no options are given.
=item B<-x>
Convert HTML to text this this command:
lynx -nomargins -dump $file.html > file.txt
=item B<-2>
Convert HTML to text this this command:
html2text $file.html > file.txt
=back
If you'd like to use a different HTML-to-text converter, just say -h
for HTML output and run your converter separately.
=head1 BUGS
Only UTF-8 encoding is supported. Not sure anyone ever uses anything
else with FictionBook, so it probably doesn't matter.
The conversion is pretty dumb, and doesn't attempt to handle the full
FB2 spec. links is good about ignoring unknown tags, so this works OK
for text conversions.
There's no support for compressed FB2 files. Extract it first.
=head1 AUTHOR
B. Watson <urchlay@slackware.uk>
=head1 LICENSE
unfb2 is released under the WTFPL: Do WTF you want with this.
=head1 SEE ALSO
links(1), lynx(1), html2text(1), convertlit(1), FBReader(1)
=cut
$VERSION=0.1;
# we're going to use shell redirection to execute a command, so
# best use temp filenames we *know* won't have shell metacharacters.
sub mktmpfile {
my $ext = shift || "tmp";
return "unfb2_tmp_" . int(rand(2**32)) . "." . $ext;
}
($SELF = $0) =~ s,.*/,,;
$outfmt = "txt";
$convcmd = "links -html-margin 0 -dump";
$overwrite = 0;
while(@ARGV && $ARGV[0] =~ /^-./) {
my $opt = shift;
for($opt) {
/^--h(elp)?/ && do { exec("perldoc $0"); };
/^--man$/ && do { exec("pod2man --stderr -s1 -c\"Urchlay's Miscellany\" -r$VERSION $0"); };
/^-f$/ && do { $overwrite = 1; next; };
/^-h(html)?$/ && do { $outfmt = "html"; next; };
/^-x$/ && do { $convcmd = "lynx -nomargins -dump"; next; };
/^-2$/ && do { $convcmd = "html2text"; next; };
/^-l$/ && next; # this was the default anyway
die("$SELF: unknown option $opt, try --help\n");
}
}
$infile = shift;
if((not defined $infile) || ($infile eq '-')) {
die "$SELF: missing input filename (can't read from stdin, sorry)\n";
}
($outfile = $infile) =~ s/\.([^.]*)$/.$outfmt/;
$outfile =~ s,.*/,,;
if($infile eq $outfile) {
die "$SELF: can't read and write from the same file ($infile)\n";
}
if((!$overwrite) && (-e $outfile)) {
die("$SELF: output file $outfile already exists, not overwriting\n");
}
open $in, "<:encoding(UTF-8)", $infile or die "$SELF: $infile: $!\n";
warn "$SELF: output file is $outfile\n";
if($outfmt eq "txt") {
$htmlfile = mktmpfile("html");
} else {
$htmlfile = $outfile;
}
open $out, ">:encoding(UTF-8)", $htmlfile or die "$SELF: $htmlfile: $!\n";
$in_body = $in_binary = $fb_ok = 0;
while(<$in>) {
$fb_ok++ if /<FictionBook/i;
if(!$in_body) {
next unless /<body\s*>/i;
print $out "<html><meta charset=\"UTF-8\"><body>";
$in_body = 1;
}
if(/<binary/) {
$in_binary = 1;
} elsif(/<\/binary/) {
$in_binary = 0;
}
s/<(|\/)section(.*?)>/<$1div$2>/ig; # TODO: something better?
s/<(|\/)?title(.*?)>/<$1center$2>/ig;
s/<empty-line(.*?)>/<br\/><br\/>/ig;
s/<\/FictionBook>/<\/html>/ig;
print $out $_ unless $in_binary;
}
close $out;
if($fb_ok != 1) {
warn "$SELF: input didn't look like valid FB2, output may be bogus\n";
}
if($outfmt eq "txt") {
$tmpfile = mktmpfile("txt");
system("$convcmd $htmlfile > $tmpfile");
unlink($htmlfile);
unlink($outfile);
link($tmpfile, $outfile) || die "$SELF: link($tmpfile, $outfile): $!\n";
unlink($tmpfile);
}
|