textconv.pm

filter: Statementstmt 48.9% 46 / 94 Branchbran 0.0% 0 / 24 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 59.0% 13 / 22 Podpod 0.0% 0 / 7 totaltotal 38.8% 59 / 152 CC 15 SCAR 30.9
CC
15
Cov
38%
textconv
68.5
load_module
12.0
load
12.0
CRAP 22.0
« textconv/ooxml/regex.pm ↑ Summary textconv/git.pm »
1 package App::optex::textconv;
2  
3 1 our $VERSION = '1.08';
4  
5 1 ▶use v5.14;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 ▶use App::optex v0.5.2;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 =encoding utf-8
12  
13 =head1 NAME
14  
15 textconv - optex module to replace document file by its text contents
16  
17 =head1 VERSION
18  
19 Version 1.08
20  
21 =head1 SYNOPSIS
22  
23 optex command -Mtextconv
24  
25 optex command -Mtc (alias module)
26  
27 optex command -Mtextconv::load=pandoc
28  
29 =head1 DESCRIPTION
30  
31 This module replaces several sort of filenames by node representing
32 its text information. File itself is not altered.
33  
34 For example, you can check the text difference between MS word files
35 like this:
36  
37 $ optex diff -Mtextconv OLD.docx NEW.docx
38  
39 If you have symbolic link named B<diff> to B<optex>, and following
40 setting in your F<~/.optex.d/diff.rc>:
41  
42 option default --textconv
43 option --textconv -Mtextconv $<move>
44  
45 Next command simply produces the same result.
46  
47 $ diff OLD.docx NEW.docx
48  
49 =head2 FILE FORMATS
50  
51 =over 7
52  
53 =item git
54  
55 L<git(1)> file object. Like C<HEAD^:README.md>.
56  
57 =item msdoc
58  
59 Microsoft office format files in XML (.docx, .pptx, .xlsx, .docm,
60 .pptm, .xlsm).
61 Use
62 L<App::optex::textconv::msdoc>,
63 L<App::optex::textconv::ooxml>,
64 L<App::optex::textconv::ooxml::regex>,
65 L<App::optex::textconv::ooxml::xslt>.
66  
67 =item doc
68  
69 Microsoft Word file.
70 Use L<Text::Extract::Word> module.
71  
72 =item xls
73  
74 Microsoft Excel file.
75 Use L<Spreadsheet::ParseExcel> module.
76  
77 =item pdf
78  
79 Use L<pdftotext(1)> command to covert PDF format.
80 See L<App::optex::textconv::pdf>.
81  
82 =item jpeg
83  
84 JPEG file is converted to its exif information (.jpeg, .jpg).
85  
86 =item http
87  
88 Name start with C<http://> or C<https://> is converted to text data
89 translated by L<w3c(1)> command.
90  
91 =item gpg
92  
93 Invoke L<gpg(1)> command to decrypt encrypted files with C<.gpg>
94 extention.
95  
96 =item pandoc
97  
98 Use L<pandoc|https://pandoc.org/> command to translate Microsoft
99 office document in XML format.
100 See L<App::optex::textconv::pandoc>.
101  
102 =item tika
103  
104 Use L<Apache Tika|https://tika.apache.org/> command to translate
105 Microsoft office document in XML and non-XML format.
106 See L<App::optex::textconv::tika>.
107  
108 =back
109  
110 =head1 MICROSOFT DOCUMENTS
111  
112 Microsoft office document in XML format (.docx, .pptx, .xlsx) is
113 converted to plain text by original code implemented in
114 L<App::optex::textconv::ooxml::regex> module. Algorithm used in this
115 module is extremely simple, and consequently runs fast.
116  
117 =begin COMMENT
118  
119 If related modules are available, L<App::optex::textconv::ooxml::xslt>
120 is used to covert XML using XSLT mechanism.
121  
122 =end COMMENT
123  
124 Two modules are included in this distribution to use other external
125 converter program, B<pandoc> and B<tika>, which implement much more
126 serious algorithm. They can be invoked by calling B<load> function
127 with module declaration like:
128  
129 optex -Mtextconv::load=pandoc
130  
131 optex -Mtextconv::load=tika
132  
133 =head1 INSTALL
134  
135 =head2 CPANM
136  
137 cpanm App::optex::textconv
138  
139 =head2 GIT
140  
141 These are sample configurations using L<App::optex::textconv> in git
142 environment.
143  
144 ~/.gitconfig
145 [diff "msdoc"]
146 textconv = optex -Mtextconv cat
147 [diff "pdf"]
148 textconv = optex -Mtextconv cat
149 [diff "jpg"]
150 textconv = optex -Mtextconv cat
151  
152 ~/.config/git/attributes
153 *.docx diff=msdoc
154 *.pptx diff=msdoc
155 *.xlmx diff=msdoc
156 *.pdf diff=pdf
157 *.jpg diff=jpg
158  
159 About other GIT related setting, see
160 L<https://github.com/kaz-utashiro/sdif-tools>.
161  
162 =head1 SEE ALSO
163  
164 L<https://github.com/kaz-utashiro/optex>
165  
166 L<https://github.com/kaz-utashiro/optex-textconv>
167  
168 L<https://qiita.com/kaz-utashiro/items/23fd825bd325240592c2>
169  
170 L<https://qiita.com/kaz-utashiro/items/7c11337468c63fdfdccb>
171  
172 L<https://github.com/kaz-utashiro/sdif-tools>
173  
174 =head1 AUTHOR
175  
176 Kazumasa Utashiro
177  
178 =head1 LICENSE
179  
180 Copyright 2019-2024 Kazumasa Utashiro.
181  
182 This library is free software; you can redistribute it and/or modify
183 it under the same terms as Perl itself.
184  
185 =cut
186  
187 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
188 1 ▶use List::Util 1.45 qw(first);
Subroutine BEGIN: called CC=1 SCAR=0.0
189  
190 1 our @CONVERTER;
191 1 ▶use App::optex::textconv::default;
Subroutine BEGIN: called CC=1 SCAR=0.0
192 1 ▶use App::optex::textconv::msdoc;
Subroutine BEGIN: called CC=1 SCAR=0.0
193 1 ▶use App::optex::textconv::git;
Subroutine BEGIN: called CC=1 SCAR=0.0
194  
195 1 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
196  
197 1 our @EXPORT = ();
198 1 our @EXPORT_OK = qw(initialize finalize load);
199 1 our %EXPORT_TAGS = ( alias => \@EXPORT_OK );
200  
201 1 my($mod, $argv);
202 0 ▶sub initialize {
Subroutine initialize: not called CC=1 SCAR=6.9
Pod undocumented
203 0 ($mod, $argv) = @_;
204 }
205  
206 0 ▶sub finalize {
Subroutine finalize: not called CC=1 SCAR=6.9
Pod undocumented
207 0 @$argv = textconv(@$argv);
208 }
209  
210 0 ▶sub converter {
Subroutine converter: not called CC=2 SCAR=17.9
Pod undocumented
211 0 my $filename = shift;
212 0 ▶ if (my $ent = first { $_->treat($filename) } @CONVERTER) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if (my $ent = &first(sub { $_->treat($filename); } , @App::optex::textconv::CONVERTER)) 0 0 0
213 0 return $ent;
214 }
215 0 undef;
216 }
217  
218 0 ▶sub exec_command {
Subroutine exec_command: not called CC=1 SCAR=6.9
Pod undocumented
219 0 my($format, $file) = @_;
220 0 my $exec = sprintf $format, $file;
221 0 qx($exec);
222 }
223  
224 0 ▶sub load_module {
Subroutine load_module: not called CC=3 SCAR=24.8
Pod undocumented
225 0 my $name = shift;
226 0 my $module = __PACKAGE__ . "::$name";
227 0 eval "use $module";
228 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
229 0 ▶ warn $@ unless $@ =~ /Can't locate/;
Branch: 1 branch
BranchTrueFalseTotal
unless $@ =~ /Can't locate/u 0 0 0
230 0 return 0;
231 }
232 0 $module;
233 }
234  
235 0 ▶sub load {
Subroutine load: not called CC=3 SCAR=24.8
Pod undocumented
236 0 ▶ while (my($mod, $val) = splice(@_, 0, 2)) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = splice(@_, 0, 2)) 0 0 0
237 0 ▶ load_module $mod if $val;
Branch: 1 branch
BranchTrueFalseTotal
if $val 0 0 0
238 }
239 }
240  
241 1 my @persist;
242  
243 0 ▶sub textconv {
Subroutine textconv: not called CC=10 SCAR=42.3
Pod undocumented
244 ARGV:
245 0 for (@_) {
246 0 my($suffix) = map { lc } /\.(\w+)$/x;
247 0 my $func = do {
248 0 ▶ if (my $c = converter $_) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $c = converter($_)) { } 0 0 0
elsif ($suffix) { } 0 0 0
249 0 my $textize = $c->textize;
250 0 ▶ if (ref $textize eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $textize eq 'CODE') { } 0 0 0
251 0 $textize;
252 }
253 else {
254 0 ▶ sub { exec_command $textize, $_ };
Subroutine __ANON__: not called CC=1 SCAR=6.9
255 }
256 }
257 elsif ($suffix) {
258 0 state %tried;
259 0 my $to_text = join '::', __PACKAGE__, $suffix, 'to_text';
260 0 ▶ if (defined &{$to_text}) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined &{$to_text;}) { } 0 0 0
elsif ($tried{$suffix}++) { } 0 0 0
261 0 $to_text;
262 } elsif ($tried{$suffix}++) {
263 0 next;
264 } else {
265 0 ▶ load_module $suffix or next;
Branch: 1 branch
BranchTrueFalseTotal
load_module($suffix) or next 0 0 0
266 0 redo;
267 }
268 } else {
269 0 next;
270 }
271 };
272 0 my $data = do {
273 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
274 1 ▶ use charnames ':full';
Subroutine BEGIN: called CC=1 SCAR=0.0
275 0 ▶ local $_ = &$func($_) // do {
Condition0%0/3
&$func($_) // do { warn "${_}: READ ERROR in textconv module.\n"; next }
l!l&&r!l&&!r
000
MC/DC0%0/2
&$func($_) // do { warn "${_}: READ ERROR in textconv module.\n"; next }
&$func($_) do { warn "${_}: READ ERROR in textconv module.\n"; next }
Truth table0%0/3
A // B
A= &$func($_)B= do { warn "${_}: READ ERROR in textconv module.\n"; next }
ABresult
1X1
011
000
276 0 warn "$_: READ ERROR in textconv module.\n";
277 0 next;
278 };
279 0 ▶ $_ = decode 'utf8', $_ unless utf8::is_utf8($_);
Branch: 1 branch
BranchTrueFalseTotal
unless utf8::is_utf8($_) 0 0 0
280 0 s/[\p{Private_Use}\p{Unassigned}]/\N{GETA MARK}/g;
281 0 encode 'utf8', $_;
282 };
283 1 ▶ use Command::Run::Tmpfile;
Subroutine BEGIN: called CC=1 SCAR=0.0
284 0 my $tmp = $persist[@persist] = Command::Run::Tmpfile->new(raw => 1);
285 0 $_ = $tmp->write($data)->rewind->path;
286 }
287 0 @_;
288 }
289  
290 1 1;
291  
292 __DATA__
« textconv/ooxml/regex.pm ↑ Summary textconv/git.pm »