-
Notifications
You must be signed in to change notification settings - Fork 1
/
bookmark-djvu-extract
executable file
·286 lines (228 loc) · 6.81 KB
/
bookmark-djvu-extract
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
#!/usr/bin/env perl
# LICENSE: GPLv3+
use 5.010;
use warnings;
use strict;
use utf8;
use Getopt::Long qw/:config no_ignore_case bundling/;
use File::Basename qw/basename/;
use Encode qw/decode/;
if (!eval 'use YAML::XS qw/Dump/; 1;') {
die "cannot find the YAML::XS Perl module.\n" .
"Try '\$ apt-get install libyaml-libyaml-perl'.\n";
}
binmode STDOUT, ':encoding(UTF-8)';
binmode STDIN, ':encoding(UTF-8)';
binmode STDERR, ':encoding(UTF-8)';
my $prog_name = basename $0;
my $bug_address = "www.github.com/amba/bookmark-djvu/issues";
sub print_usage {
say "Usage: $prog_name [OPTIONS] DJVU [-o BOOKMARKS_FILE]";
}
sub print_help {
print_usage ();
say "
extract the outline of DJVU.
The format of the generated BOOKMARKS-FILE is described in the README.
Options:
-o, --output=FILE write bookmarks to FILE
-s, --simple-format dump outline in simple format (default: YAML)
-h, --help print this help screen
-V, --version print program version
The default for BOOKMARKS-FILE is DJVU-FILENAME with the suffix changed to
- '.outline' for YAML mode (default).
- '.bm' for simple format.
Report bugs to $bug_address";
}
sub print_version {
say "bookmark-djvu 0.1
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law."
}
$_ = decode ('UTF-8', $_) for @ARGV;
my $output_filename;
my $use_simple_format = 0;
my %opt_hash = (
"output|o=s" => \$output_filename,
"simple-format|s" => \$use_simple_format,
"help|h" => sub {print_help (); exit 0},
"version|V" => sub {print_version (); exit 0},
);
unless (GetOptions (%opt_hash) && @ARGV) {
print_usage ();
exit 1;
}
my $djvu_filename = $ARGV[0];
if (! -f $djvu_filename) {
print_usage ();
die "error: djvu file '$djvu_filename' does not exist\n";
}
if (!$output_filename) {
my $suffix = $use_simple_format ? ".bm" : ".outline";
$output_filename =
get_default_filename ($djvu_filename, ".djvu", $suffix);
if (-e $output_filename) {
die "won't overwrite existing file '$output_filename'. " .
"Use the '-o FILE' option!";
}
}
open my $output_handle, ">", $output_filename
or die "cannot open file '$output_filename': $!";
# start extracting bookmarks with djvused in a pipe
open my $djvused_handle, "-|", ("djvused", $djvu_filename, "-u", "-e",
"print-outline") or die "cannot find djvused.\n" .
"Try '\$ apt-get install djvulibre-bin'.\n";
binmode $djvused_handle, ':encoding(UTF-8)';
my $outline_items = parse_djvused_output ($djvused_handle);
unless (@{$outline_items}) {
warn "no outline found\n";
exit 1;
}
close $djvused_handle
or die "djvused failed with exit status ", $? >> 8, "\n";
if ($use_simple_format) {
dump_to_simple_format ($output_handle, $outline_items);
}
else {
dump_to_yaml ($output_handle, $outline_items);
}
# arg: open file descriptor to djvused
# returns: arrayref of hashrefs, each describing level, title and page of an
# outline item.
sub parse_djvused_output {
my $djvused_handle = shift;
my $level = 0;
my $line = <$djvused_handle>;
if (!$line or $line !~ /^\(bookmarks$/) {
warn "no bookmarks found\n";
return [];
}
my $outline_items = [];
while ($line = <$djvused_handle>) {
chomp $line;
$line =~ /^\s*\("(?<title>.+)"$/
or die "line '$line' in djvused output does not match.
Please report to $bug_address\n";
my $title = djvused_unescape ($+{title});
$line = <$djvused_handle>;
$line =~ /\s*"#(?<page>[0-9]+)"(?<close>(\s*\))*)\s*$/
or die "line '$line' in djvused output does not match.
Please report to $bug_address\n";
push @$outline_items, {level => $level, title => $title,
page => $+{page}};
$level -= ($+{close} =~ tr/\)//) - 1;
}
return $outline_items;
}
sub dump_to_simple_format {
my $out_handle = shift;
my $outline_items = shift;
binmode $out_handle, ':encoding(UTF-8)';
for my $outline (@$outline_items) {
print {$out_handle} " "x($outline->{level}),
prepare_title ($outline->{title}),
, " $outline->{page}\n";
}
}
sub prepare_title {
my $title = shift;
if ($title =~ /\n/) {
warn "WARNING: removing newline in title '$title'.\n";
$title =~ s/\n/ /g;
}
if ($title =~ /(\.|\s)+$/) {
warn "WARNING: removing trailing dots and/or whitespace " .
"in title '$title'.\n";
$title =~ s/(\.|\s)+$//;
}
return $title;
}
sub dump_to_yaml {
my $out_handle = shift;
my $input_array = shift;
(my $array, undef) = get_recursive_array ($input_array, 0);
print {$out_handle} Dump ($array);
}
# recursively convert flat array into nested data structure
# args: input array ref, index to start
# returns: output array ref, following index
sub get_recursive_array {
my ($input_array, $index) = @_;
my $last = $#{$input_array};
my $base_level = $input_array->[$index]{level};
my $result_array = [];
while ($index <= $last) {
my $node = $input_array->[$index];
my $current_level = $node->{level};
last if $current_level < $base_level;
my $outline_item = {title => $node->{title}, page => $node->{page}};
my $next_index = $index + 1;
my $next_level = $input_array->[$next_index]{level};
if ($next_index <= $last && $next_level > $base_level) {
($outline_item->{kids}, $index) =
get_recursive_array ($input_array, $next_index);
}
else {
++$index;
}
push @{$result_array}, $outline_item;
}
return ($result_array, $index);
}
sub djvused_unescape {
# see in '$ man djvused': "DJVUSED FILE FORMATS - Strings"
# and perlrebackslash
my $string = shift;
$string =~ s/\\([0-7]{3}|.)/expand_escape_sequence ($1)/ge;
return $string;
}
sub expand_escape_sequence {
my ($string) = @_;
if ($string eq "\\" || $string eq '"') {
return $string;
}
elsif (length ($string) == 3) {
# octal escape sequence
return pack 'U', oct ($string);
}
elsif ($string eq "a") {
return "\a";
}
elsif ($string eq "b") {
return "\b";
}
elsif ($string eq "t") {
return "\t";
}
elsif ($string eq "n") {
return "\n";
}
elsif ($string eq "v") {
return "\x{0b}";
}
elsif ($string eq "f") {
return "\f";
}
elsif ($string eq "r") {
return "\r";
}
else {
die "unknown escape sequence '\\$string' in '$string'
please report to $bug_address";
}
}
sub get_default_filename {
my $filename = shift;
my $from = shift;
my $too = shift;
die "get_default_filename needs arg" unless defined ($too);
my $result_filename;
if ($filename =~ /\Q$from\E$/) {
($result_filename = $filename) =~ s/\Q$from\E/$too/;
}
else {
$result_filename = $filename . $too;
}
return $result_filename;
}