| 1 | =head1 NAME | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | msdoc - Greple module for access MS office docx/pptx/xlsx documents | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | =head1 VERSION | ||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | Version 1.07 | ||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | greple -Mmsdoc pattern example.docx | ||||||||||||||||||||||||||||
| 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<n> | ||||||||||||||||||||||||||||
| 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<string> | ||||||||||||||||||||||||||||
| 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-fold> | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | Indent and fold long lines. | ||||||||||||||||||||||||||||
| 61 | This option requires L<ansicolumn(1)> command installed. | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | =item B<--indent-mark>=I<string> | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | Set indentation string. Default is C<| >. | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | =back | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | =head1 INSTALL | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | =head2 CPANMINUS | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | cpanm App::Greple::msdoc | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | L<App::Greple>, | ||||||||||||||||||||||||||||
| 78 | L<https://github.com/kaz-utashiro/greple> | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | L<App::Greple::msdoc>, | ||||||||||||||||||||||||||||
| 81 | L<https://github.com/kaz-utashiro/greple-msdoc> | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | L<App::optex::textconv>, | ||||||||||||||||||||||||||||
| 84 | L<https://github.com/kaz-utashiro/optex-textconv> | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | L<App::ansicolumn>, | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | L<https://qiita.com/kaz-utashiro/items/30594c16ed6d931324f9> | ||||||||||||||||||||||||||||
| 89 | (in Japanese) | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | Kazumasa Utashiro | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | =head1 LICENSE | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | Copyright 2018-2024 Kazumasa Utashiro. | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | This library is free software; you can redistribute it and/or modify | ||||||||||||||||||||||||||||
| 100 | it under the same terms as Perl itself. | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | =cut | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | package App::Greple::msdoc; | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | 6 | our $VERSION = '1.07'; | |||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 6 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 109 | 6 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 110 | 6 | ▶ | use v5.14; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 111 | 6 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 112 | 6 | ▶ | use utf8; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 113 | 6 | ▶ | use Encode; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | 6 | ▶ | use Exporter 'import'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 116 | 6 | our @EXPORT = (); | |||||||||||||||||||||||||||
| 117 | 6 | our %EXPORT_TAGS = (); | |||||||||||||||||||||||||||
| 118 | 6 | our @EXPORT_OK = (); | |||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | 6 | ▶ | use App::Greple::Common qw(&FILELABEL); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 121 | 6 | ▶ | use Data::Dumper; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | 6 | our $indent_mark = "| "; | |||||||||||||||||||||||||||
| 124 | 6 | our $opt_space = undef; | |||||||||||||||||||||||||||
| 125 | 6 | our $opt_separator = undef; | |||||||||||||||||||||||||||
| 126 | 6 | our $opt_type; | |||||||||||||||||||||||||||
| 127 | 6 | our $default_format = 'text'; | |||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | 0 | ▶ | sub separate_xml { | ||||||||||||||||||||||||||
|
Subroutine
separate_xml: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 130 | 0 | ▶ | s{ (?<=>) ([^<]*) }{ $1 ? "\n$1\n" : "\n" }gex; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | 0 | ▶ | sub indent_xml { | ||||||||||||||||||||||||||
|
Subroutine
indent_xml: not called CC=8 SCAR=42.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 134 | 0 | my %arg = @_; | |||||||||||||||||||||||||||
| 135 | 0 | ▶ | my $file = delete $arg{&FILELABEL} or die; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | 0 | my %nonewline = do { | |||||||||||||||||||||||||||
| 138 | 0 | map { $_ => 1 } | |||||||||||||||||||||||||||
| 139 | 0 | map { @{$_->[1]} } | |||||||||||||||||||||||||||
| 140 | 0 | grep { $file =~ $_->[0] } ( | |||||||||||||||||||||||||||
| 141 | [ qr/\.doc[xm]$/, [ qw(w:t w:delText w:instrText wp:posOffset) ] ], | ||||||||||||||||||||||||||||
| 142 | [ qr/\.ppt[xm]$/, [ qw(a:t) ] ], | ||||||||||||||||||||||||||||
| 143 | [ qr/\.xls[xm]$/, [ qw(t v f formula1) ] ], | ||||||||||||||||||||||||||||
| 144 | ); | ||||||||||||||||||||||||||||
| 145 | }; | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | 0 | my $level = 0; | |||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | 0 | s{ | |||||||||||||||||||||||||||
| 150 | (?<mark> | ||||||||||||||||||||||||||||
| 151 | (?<single> | ||||||||||||||||||||||||||||
| 152 | < (?<tag>[\w:]+) [^>]* /> | ||||||||||||||||||||||||||||
| 153 | ) | ||||||||||||||||||||||||||||
| 154 | | | ||||||||||||||||||||||||||||
| 155 | (?<open> | ||||||||||||||||||||||||||||
| 156 | < (?<tag>[\w:]+) [^>]* (?<!/) > | ||||||||||||||||||||||||||||
| 157 | ) | ||||||||||||||||||||||||||||
| 158 | | | ||||||||||||||||||||||||||||
| 159 | (?<close> | ||||||||||||||||||||||||||||
| 160 | < / (?<tag>[\w:]+) > | ||||||||||||||||||||||||||||
| 161 | ) | ||||||||||||||||||||||||||||
| 162 | ) | ||||||||||||||||||||||||||||
| 163 | }{ | ||||||||||||||||||||||||||||
| 164 | 0 | ▶ | if (not $+{single} and $nonewline{$+{tag}}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not $+{"single"} and $nonewline{$+{"tag"}}
MC/DC0%0/2
not $+{"single"} and $nonewline{$+{"tag"}}
not $+{"single"}
$nonewline{$+{"tag"}}
Truth table0%0/3
not(A) and B
A= not $+{"single"}B= $nonewline{$+{"tag"}}
| |||||||||||||||||||||||||||||
| 165 | join("", $+{open} ? $indent_mark x $level : "", | ||||||||||||||||||||||||||||
| 166 | $+{mark}, | ||||||||||||||||||||||||||||
| 167 | 0 | ▶ | $+{close} ? "\n" : ""); | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||
| 169 | else { | ||||||||||||||||||||||||||||
| 170 | 0 | ▶ | $+{close} and $level--; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 171 | 0 | ▶ | ($indent_mark x ($+{open} ? $level++ : $level)) . $+{mark} . "\n"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 172 | } | ||||||||||||||||||||||||||||
| 173 | }gex; | ||||||||||||||||||||||||||||
| 174 | } | ||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | 6 | ▶ | use Archive::Zip; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 177 | 6 | ▶ | use App::optex::textconv::msdoc qw(to_text get_list); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | 6 | my %formatter = ( | |||||||||||||||||||||||||||
| 180 | 'indent-xml' => \&indent_xml, | ||||||||||||||||||||||||||||
| 181 | 'separate-xml' => \&separate_xml, | ||||||||||||||||||||||||||||
| 182 | ); | ||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | 4 | ▶ | sub extract_content { | ||||||||||||||||||||||||||
|
Subroutine
extract_content: called CC=13 SCAR=38.7
Pod
undocumented
| |||||||||||||||||||||||||||||
| 185 | 4 | my %arg = @_; | |||||||||||||||||||||||||||
| 186 | 4 | ▶ | my $file = $arg{&FILELABEL} or die; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 187 | 4 | ▶ | my $type = ($file =~ /\.((?:doc|xls|ppt)[xm])$/)[0] or die; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 188 | 4 | ▶ | my $pid = open(STDIN, '-|') // croak "process fork failed: $!"; | ||||||||||||||||||||||||||
|
Condition50%1/2
open(STDIN, "-|") // croak("process fork failed: $!")
MC/DC0%0/1
open(STDIN, "-|") // croak("process fork failed: $!")
open(STDIN, "-|")
Truth table50%1/2
A
A= open(STDIN, "-|")
| |||||||||||||||||||||||||||||
| 189 | 4 | binmode STDIN, ':encoding(utf8)'; | |||||||||||||||||||||||||||
| 190 | 4 | ▶ | if ($pid) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 191 | 2 | return $pid; | |||||||||||||||||||||||||||
| 192 | } | ||||||||||||||||||||||||||||
| 193 | 2 | ▶ | my $format = $arg{format} // $default_format; | ||||||||||||||||||||||||||
|
Condition33%1/3
$arg{"format"} // $App::Greple::msdoc::default_format
MC/DC0%0/2
$arg{"format"} // $App::Greple::msdoc::default_format
$arg{"format"}
$App::Greple::msdoc::default_format
Truth table33%1/3
A // B
A= $arg{"format"}B= $App::Greple::msdoc::default_format
| |||||||||||||||||||||||||||||
| 194 | 2 | ▶ | if ($format eq 'text') { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 195 | 2 | print decode 'utf8', to_text($file); | |||||||||||||||||||||||||||
| 196 | 2 | exit; | |||||||||||||||||||||||||||
| 197 | } elsif ($format =~ /xml$/) { | ||||||||||||||||||||||||||||
| 198 | 0 | my $zip = Archive::Zip->new($file); | |||||||||||||||||||||||||||
| 199 | 0 | my @xml; | |||||||||||||||||||||||||||
| 200 | 0 | for my $entry (get_list($zip, $type)) { | |||||||||||||||||||||||||||
| 201 | 0 | ▶ | my $member = $zip->memberNamed($entry) or next; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 202 | 0 | ▶ | my $xml = $member->contents or next; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 203 | 0 | push @xml, $xml; | |||||||||||||||||||||||||||
| 204 | } | ||||||||||||||||||||||||||||
| 205 | 0 | my $xml = decode 'utf8', join "\n", @xml; | |||||||||||||||||||||||||||
| 206 | 0 | ▶ | if (my $sub = $formatter{$format}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 207 | 0 | $sub->(&FILELABEL => $file) for $xml; | |||||||||||||||||||||||||||
| 208 | } | ||||||||||||||||||||||||||||
| 209 | 0 | print $xml; | |||||||||||||||||||||||||||
| 210 | 0 | exit; | |||||||||||||||||||||||||||
| 211 | } | ||||||||||||||||||||||||||||
| 212 | 0 | die; | |||||||||||||||||||||||||||
| 213 | } | ||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||
| 215 | 6 | 1; | |||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||
| 217 | __DATA__ | ||||||||||||||||||||||||||||