| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
=head1 NAME |
|
2
|
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
msdoc - Greple module for access MS office docx/pptx/xlsx documents |
|
4
|
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
=head1 VERSION |
|
6
|
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
Version 1.04 |
|
8
|
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
=head1 SYNOPSIS |
|
10
|
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
greple -Mmsdoc |
|
12
|
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
=head1 DESCRIPTION |
|
14
|
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
This module makes it possible to search string in Microsoft |
|
16
|
|
|
|
|
|
|
docx/pptx/xlsx file. |
|
17
|
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
Microsoft document consists of multiple files archived in zip format. |
|
19
|
|
|
|
|
|
|
String information is stored in "word/document.xml", |
|
20
|
|
|
|
|
|
|
"ppt/slides/*.xml" or "xl/sharedStrings.xml". This module extracts |
|
21
|
|
|
|
|
|
|
these data and replaces the search target. |
|
22
|
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
By default, text part from XML data is extracted. This process is |
|
24
|
|
|
|
|
|
|
done by very simple method and may include redundant information. |
|
25
|
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
Strings are simply connected into paragraph for I<.docx> and I<.pptx> |
|
27
|
|
|
|
|
|
|
document. For I<.xlsx> document, single space is inserted between |
|
28
|
|
|
|
|
|
|
them. Use B<--separator> option to change this behavior. |
|
29
|
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
After every paragraph, single newline is inserted for I<.pptx> and |
|
31
|
|
|
|
|
|
|
I<.xlsx> file, and double newlines for I<.docx> file. Use |
|
32
|
|
|
|
|
|
|
B<--space> option to change. |
|
33
|
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
=head1 OPTIONS |
|
35
|
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
=over 7 |
|
37
|
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
=item B<--dump> |
|
39
|
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
Simply print all converted data. Additional pattern can be specified, |
|
41
|
|
|
|
|
|
|
and they will be highlighted inside whole text. |
|
42
|
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
$ greple -Mmsdoc --dump -e foo -e bar buz.docx |
|
44
|
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
=item B<--space>=I |
|
46
|
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
Specify number of newlines inserted after every paragraph. Any |
|
48
|
|
|
|
|
|
|
non-negative integer is allowed including zero. |
|
49
|
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
=item B<--separator>=I |
|
51
|
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
Specify the separator string placed between each component strings. |
|
53
|
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
=item B<--indent> |
|
55
|
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
Extract indented XML document, not a plain text. |
|
57
|
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
=item B<--indent-mark>=I |
|
59
|
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
Set indentation string. Default is C<| >. |
|
61
|
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
=back |
|
63
|
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
=head1 INSTALL |
|
65
|
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
cpanm App::Greple::msdoc |
|
67
|
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
=head1 SEE ALSO |
|
69
|
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
L |
|
71
|
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
L |
|
73
|
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
=head1 AUTHOR |
|
75
|
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
Kazumasa Utashiro |
|
77
|
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
=head1 LICENSE |
|
79
|
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
Copyright 2018-2020 Kazumasa Utashiro. |
|
81
|
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or modify |
|
83
|
|
|
|
|
|
|
it under the same terms as Perl itself. |
|
84
|
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
=cut |
|
86
|
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
package App::Greple::msdoc; |
|
88
|
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
our $VERSION = '1.04'; |
|
90
|
|
|
|
|
|
|
|
|
91
|
1
|
|
|
1
|
|
752
|
use strict; |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
29
|
|
|
92
|
1
|
|
|
1
|
|
5
|
use warnings; |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
25
|
|
|
93
|
1
|
|
|
1
|
|
10
|
use v5.14; |
|
|
1
|
|
|
|
|
4
|
|
|
94
|
1
|
|
|
1
|
|
4
|
use Carp; |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
57
|
|
|
95
|
1
|
|
|
1
|
|
619
|
use utf8; |
|
|
1
|
|
|
|
|
15
|
|
|
|
1
|
|
|
|
|
5
|
|
|
96
|
1
|
|
|
1
|
|
626
|
use Encode; |
|
|
1
|
|
|
|
|
10365
|
|
|
|
1
|
|
|
|
|
73
|
|
|
97
|
|
|
|
|
|
|
|
|
98
|
1
|
|
|
1
|
|
8
|
use Exporter 'import'; |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
64
|
|
|
99
|
|
|
|
|
|
|
our @EXPORT = (); |
|
100
|
|
|
|
|
|
|
our %EXPORT_TAGS = (); |
|
101
|
|
|
|
|
|
|
our @EXPORT_OK = (); |
|
102
|
|
|
|
|
|
|
|
|
103
|
1
|
|
|
1
|
|
448
|
use App::Greple::Common; |
|
|
1
|
|
|
|
|
378
|
|
|
|
1
|
|
|
|
|
114
|
|
|
104
|
1
|
|
|
1
|
|
621
|
use Data::Dumper; |
|
|
1
|
|
|
|
|
6173
|
|
|
|
1
|
|
|
|
|
332
|
|
|
105
|
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
our $indent_mark = "| "; |
|
107
|
|
|
|
|
|
|
our $opt_space = undef; |
|
108
|
|
|
|
|
|
|
our $opt_separator = undef; |
|
109
|
|
|
|
|
|
|
our $opt_type; |
|
110
|
|
|
|
|
|
|
our $default_format = 'text'; |
|
111
|
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
sub separate_xml { |
|
113
|
0
|
0
|
|
0
|
0
|
|
s{ (?<=>) ([^<]*) }{ $1 ? "\n$1\n" : "\n" }gex; |
|
|
0
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
} |
|
115
|
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
sub indent_xml { |
|
117
|
0
|
|
|
0
|
0
|
|
my %arg = @_; |
|
118
|
0
|
0
|
|
|
|
|
my $file = delete $arg{&FILELABEL} or die; |
|
119
|
|
|
|
|
|
|
|
|
120
|
0
|
|
|
|
|
|
my %nonewline = do { |
|
121
|
0
|
|
|
|
|
|
map { $_ => 1 } |
|
122
|
0
|
|
|
|
|
|
map { @{$_->[1]} } |
|
|
0
|
|
|
|
|
|
|
|
123
|
0
|
|
|
|
|
|
grep { $file =~ $_->[0] } ( |
|
|
0
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
[ qr/\.docx$/, [ qw(w:t w:delText w:instrText wp:posOffset) ] ], |
|
125
|
|
|
|
|
|
|
[ qr/\.pptx$/, [ qw(a:t) ] ], |
|
126
|
|
|
|
|
|
|
[ qr/\.xlsx$/, [ qw(t v f formula1) ] ], |
|
127
|
|
|
|
|
|
|
); |
|
128
|
|
|
|
|
|
|
}; |
|
129
|
|
|
|
|
|
|
|
|
130
|
0
|
|
|
|
|
|
my $level = 0; |
|
131
|
|
|
|
|
|
|
|
|
132
|
0
|
|
|
|
|
|
s{ |
|
133
|
|
|
|
|
|
|
(? |
|
134
|
|
|
|
|
|
|
(? |
|
135
|
|
|
|
|
|
|
< (?[\w:]+) [^>]* /> |
|
136
|
|
|
|
|
|
|
) |
|
137
|
|
|
|
|
|
|
| |
|
138
|
|
|
|
|
|
|
(? |
|
139
|
|
|
|
|
|
|
< (?[\w:]+) [^>]* (? |
|
140
|
|
|
|
|
|
|
) |
|
141
|
|
|
|
|
|
|
| |
|
142
|
|
|
|
|
|
|
(? |
|
143
|
|
|
|
|
|
|
< / (?[\w:]+) > |
|
144
|
|
|
|
|
|
|
) |
|
145
|
|
|
|
|
|
|
) |
|
146
|
|
|
|
|
|
|
}{ |
|
147
|
1
|
0
|
0
|
1
|
|
497
|
if (not $+{single} and $nonewline{$+{tag}}) { |
|
|
1
|
|
|
|
|
367
|
|
|
|
1
|
|
|
|
|
307
|
|
|
|
0
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
join("", $+{open} ? $indent_mark x $level : "", |
|
149
|
|
|
|
|
|
|
$+{mark}, |
|
150
|
0
|
0
|
|
|
|
|
$+{close} ? "\n" : ""); |
|
|
|
0
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
} |
|
152
|
|
|
|
|
|
|
else { |
|
153
|
0
|
0
|
|
|
|
|
$+{close} and $level--; |
|
154
|
0
|
0
|
|
|
|
|
($indent_mark x ($+{open} ? $level++ : $level)) . $+{mark} . "\n"; |
|
155
|
|
|
|
|
|
|
} |
|
156
|
|
|
|
|
|
|
}gex; |
|
157
|
|
|
|
|
|
|
} |
|
158
|
|
|
|
|
|
|
|
|
159
|
1
|
|
|
1
|
|
715
|
use Archive::Zip; |
|
|
1
|
|
|
|
|
74006
|
|
|
|
1
|
|
|
|
|
59
|
|
|
160
|
1
|
|
|
1
|
|
513
|
use App::optex::textconv::msdoc; |
|
|
1
|
|
|
|
|
2402
|
|
|
|
1
|
|
|
|
|
7
|
|
|
161
|
|
|
|
|
|
|
*to_text = \&App::optex::textconv::msdoc::to_text; |
|
162
|
|
|
|
|
|
|
*get_list = \&App::optex::textconv::msdoc::get_list; |
|
163
|
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
my %formatter = ( |
|
165
|
|
|
|
|
|
|
'indent-xml' => \&indent_xml, |
|
166
|
|
|
|
|
|
|
'separate-xml' => \&separate_xml, |
|
167
|
|
|
|
|
|
|
); |
|
168
|
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
sub extract_content { |
|
170
|
0
|
|
|
0
|
0
|
|
my %arg = @_; |
|
171
|
0
|
0
|
|
|
|
|
my $file = $arg{&FILELABEL} or die; |
|
172
|
0
|
0
|
|
|
|
|
my $type = ($file =~ /\.(docx|xlsx|pptx)$/)[0] or die; |
|
173
|
0
|
|
0
|
|
|
|
my $pid = open(STDIN, '-|') // croak "process fork failed: $!"; |
|
174
|
0
|
|
|
|
|
|
binmode STDIN, ':encoding(utf8)'; |
|
175
|
0
|
0
|
|
|
|
|
if ($pid) { |
|
176
|
0
|
|
|
|
|
|
return $pid; |
|
177
|
|
|
|
|
|
|
} |
|
178
|
0
|
|
0
|
|
|
|
my $format = $arg{format} // $default_format; |
|
179
|
0
|
0
|
|
|
|
|
if ($format eq 'text') { |
|
|
|
0
|
|
|
|
|
|
|
180
|
0
|
|
|
|
|
|
print decode 'utf8', to_text($file); |
|
181
|
0
|
|
|
|
|
|
exit; |
|
182
|
|
|
|
|
|
|
} elsif ($format =~ /xml$/) { |
|
183
|
0
|
|
|
|
|
|
my $zip = Archive::Zip->new($file); |
|
184
|
0
|
|
|
|
|
|
my @xml; |
|
185
|
0
|
|
|
|
|
|
for my $entry (get_list($zip, $type)) { |
|
186
|
0
|
0
|
|
|
|
|
my $member = $zip->memberNamed($entry) or next; |
|
187
|
0
|
0
|
|
|
|
|
my $xml = $member->contents or next; |
|
188
|
0
|
|
|
|
|
|
push @xml, $xml; |
|
189
|
|
|
|
|
|
|
} |
|
190
|
0
|
|
|
|
|
|
my $xml = decode 'utf8', join "\n", @xml; |
|
191
|
0
|
0
|
|
|
|
|
if (my $sub = $formatter{$format}) { |
|
192
|
0
|
|
|
|
|
|
$sub->(&FILELABEL => $file) for $xml; |
|
193
|
|
|
|
|
|
|
} |
|
194
|
0
|
|
|
|
|
|
print $xml; |
|
195
|
0
|
|
|
|
|
|
exit; |
|
196
|
|
|
|
|
|
|
} |
|
197
|
0
|
|
|
|
|
|
die; |
|
198
|
|
|
|
|
|
|
} |
|
199
|
|
|
|
|
|
|
|
|
200
|
|
|
|
|
|
|
1; |
|
201
|
|
|
|
|
|
|
|
|
202
|
|
|
|
|
|
|
__DATA__ |