File Coverage

blib/lib/App/optex/msdoc.pm
Criterion Covered Total %
statement 20 37 54.0
branch 0 4 0.0
condition n/a
subroutine 7 12 58.3
pod 0 4 0.0
total 27 57 47.3


line stmt bran cond sub pod time code
1             package App::optex::msdoc;
2              
3 1     1   719 use 5.014;
  1         4  
4 1     1   4 use strict;
  1         2  
  1         19  
5 1     1   5 use warnings;
  1         1  
  1         49  
6              
7             our $VERSION = "0.05";
8              
9             =encoding utf-8
10              
11             =head1 NAME
12              
13             msdoc - module to replace MS document by its text contents
14              
15             =head1 VERSION
16              
17             Version 0.05
18              
19             =head1 SYNOPSIS
20              
21             optex command -Mmsdoc
22              
23             =head1 NOTICE
24              
25             There is more general successor version of this module.
26             Use L.
27              
28             =head1 DESCRIPTION
29              
30             This module replaces argument which terminate with I<.docx>, I
31             or I files by node representing its text information. File
32             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 -Mmsdoc OLD.docx NEW.docx
38              
39             If you have symbolic link named B to B, and following
40             setting in your F<~/.optex.d/diff.rc>:
41              
42             option default --msdoc
43             option --msdoc -Mmsdoc $
44              
45             Next command simply produces the same result.
46              
47             $ diff OLD.docx NEW.docx
48              
49             Text data is extracted by B command with B<-Mmsdoc> module,
50             and above command is almost equivalent to below bash command using
51             process substitution.
52              
53             $ diff <(greple -Mmsdoc --dump OLD.docx) \
54             <(greple -Mmsdoc --dump NEW.docx)
55              
56             =head1 ENVIRONMENT
57              
58             This version experimentally support other converter program. If the
59             environment variable B is set, it is used
60             instead of B. Choose one from B, B or
61             B.
62              
63             =head1 SEE ALSO
64              
65             L
66              
67             It is possible to use other data conversion program, like L or
68             L. Feel to free to modify this module. I'm reluctant to
69             use them, because they work quite leisurely.
70              
71             L
72              
73             =head1 LICENSE
74              
75             Copyright 2020 Kazumasa Utashiro.
76              
77             This library is free software; you can redistribute it and/or modify
78             it under the same terms as Perl itself.
79              
80             =head1 AUTHOR
81              
82             Kazumasa Utashiro
83              
84             =cut
85              
86             package App::optex::msdoc;
87              
88 1     1   621 use utf8;
  1         15  
  1         5  
89 1     1   621 use Encode;
  1         10465  
  1         71  
90 1     1   576 use Data::Dumper;
  1         6190  
  1         200  
91              
92             my($mod, $argv);
93             sub initialize {
94 0     0 0   ($mod, $argv) = @_;
95 0           msdoc();
96             }
97              
98             sub argv (&) {
99 0     0 0   my $sub = shift;
100 0           @$argv = $sub->(@$argv);
101             }
102              
103             my %converter = (
104             'greple' => "greple -Mmsdoc --dump \"%s\"",
105             'pandoc' => "pandoc -t plain \"%s\"",
106             'tika' => "tika --text \"%s\"",
107             );
108              
109             my $converter_default = 'greple';
110              
111             my $converter = $ENV{OPTEX_MSDOC_CONVERTER} || $converter_default;
112              
113             sub to_text {
114 0     0 0   my $file = shift;
115 0           my $format = $converter{$converter};
116 0           my $exec = sprintf $format, $file;
117 0           qx($exec);
118             }
119              
120 1     1   482 use App::optex::Tmpfile;
  1         10179  
  1         221  
121              
122             my @persist;
123              
124             sub msdoc {
125             argv {
126 0     0     for (@_) {
127 0 0         my($suffix) = /\.(docx|pptx|xlsx)$/x or next;
128 0 0         -f $_ or next;
129 0           my $tmp = new App::optex::Tmpfile;
130 0           $tmp->write(to_text($_))->rewind;
131 0           push @persist, $tmp;
132 0           $_ = $tmp->path;
133             }
134 0           @_;
135 0     0 0   };
136             }
137              
138             1;
139              
140             __DATA__