textconv/ooxml/regex.pm

filter: Statementstmt 40.4% 38 / 94 Branchbran 0.0% 0 / 46 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 64.2% 9 / 14 Podpod 0.0% 0 / 3 totaltotal 29.9% 47 / 157 CC 26 SCAR 44.5
CC
26
Cov
27%
_xml2text
182.0
to_text
90.0
get_list
30.0
CRAP 85.9
↑ Summary textconv.pm »
1 package App::optex::textconv::ooxml::regex;
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 Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 1 ▶use App::optex v0.3;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use App::optex::textconv::Converter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 1 our @EXPORT_OK = qw(to_text get_list);
16  
17 1 our @CONVERTER = (
18 [ qr/\.doc[xm]$/ => \&to_text ],
19 [ qr/\.ppt[xm]$/ => \&to_text ],
20 [ qr/\.xls[xm]$/ => \&to_text ],
21 );
22  
23 0 ▶sub xml2text {
Subroutine xml2text: not called CC=2 SCAR=17.9
Pod undocumented
24 0 local $_ = shift;
25 0 my $type = shift;
26 0 my $xml_re = qr/<\?xml\b[^>]*\?>\s*/;
27 0 ▶ return $_ unless /$xml_re/;
Branch: 1 branch
BranchTrueFalseTotal
unless /$xml_re/u 0 0 0
28  
29 0 my @xml = grep { length } split /$xml_re/;
30 0 my @text = map { _xml2text($_, $type) } @xml;
31 0 join "\n", @text;
32 }
33  
34 1 my %param = (
35 docx => { space => 2, separator => "" },
36 docm => { space => 2, separator => "" },
37 xlsx => { space => 1, separator => "\t" },
38 xlsm => { space => 1, separator => "\t" },
39 pptx => { space => 1, separator => "" },
40 pptm => { space => 1, separator => "" },
41 );
42  
43 1 my $replace_reference = do {
44 1 my %hash = qw( amp & lt < gt > );
45 1 my @keys = keys %hash;
46 1 my $re = do { local $" = '|'; qr/&(@keys);/ };
47 0 ▶ sub { s/$re/$hash{$1}/g }
Subroutine __ANON__: not called CC=1 SCAR=6.9
48 1 };
49  
50 0 ▶sub _xml2text {
Subroutine _xml2text: not called CC=13 SCAR=52.0
51 0 local $_ = shift;
52 0 my $type = shift;
53 0 ▶ my $param = $param{$type} or die;
Branch: 1 branch
BranchTrueFalseTotal
my $param = $param{$type} or die 0 0 0
54  
55 0 my @p;
56 0 my $fn_id = "";
57 0 ▶ while (m{
Branch: 1 branch
BranchTrueFalseTotal
if (m[ (?<footnote> <w:footnote \s+ w:id="(?<fn_id>\d+)" ) | <(?<tag>[apw]:p|si)\b[^>]*>(?<para>.*?)</\g{tag}> ]gsux) 0 0 0
58 (?<footnote> <w:footnote \s+ w:id="(?<fn_id>\d+)" )
59 | <(?<tag>[apw]:p|si)\b[^>]*>(?<para>.*?)</\g{tag}>
60 }xsg)
61 {
62 0 ▶ if ($+{footnote}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($+{"footnote"}) 0 0 0
63 0 $fn_id = $+{fn_id};
64 0 next;
65 }
66 0 my $para = $+{para};
67 0 my @s;
68 0 ▶ while ($para =~ m{
Branch: 1 branch
BranchTrueFalseTotal
if ($para =~ m[ (?<fn_ref> <w:footnoteReference \s+ w:id="(?<fn_id>\d+)" ) | (?<footnote> <w:footnote \s+ w:id="(?<fn_id>\d+)" ) | (?<footnoteRef> <w:footnoteRef/> ) | (?<br> <[aw]:br/> ) | (?<tab> <w:tab/> | <w:tabs> ) | <(?<tag>(?:[apw]:)?t)\b[^>]*> (?<text>[^<]*?) </\g{tag}> ]gsux) 0 0 0
69 (?<fn_ref> <w:footnoteReference \s+ w:id="(?<fn_id>\d+)" )
70 | (?<footnote> <w:footnote \s+ w:id="(?<fn_id>\d+)" )
71 | (?<footnoteRef> <w:footnoteRef/> )
72 | (?<br> <[aw]:br/> )
73 | (?<tab> <w:tab/> | <w:tabs> )
74 | <(?<tag>(?:[apw]:)?t)\b[^>]*> (?<text>[^<]*?) </\g{tag}>
75 }xsg)
76 {
77 0 ▶ if ($+{fn_ref}) { push @s, "[^$+{fn_id}]" }
Branch: 6 branches
BranchTrueFalseTotal
if ($+{'fn_ref'}) { } 0 0 0
elsif ($+{'footnote'}) { } 0 0 0
elsif ($+{'footnoteRef'}) { } 0 0 0
elsif ($+{'br'}) { } 0 0 0
elsif ($+{'tab'}) { } 0 0 0
elsif ($+{'text'} ne '') { } 0 0 0
78 0 elsif ($+{footnote}) { $fn_id = $+{fn_id} }
79 0 elsif ($+{footnoteRef}) { push @s, "[^$fn_id]:" }
80 0 elsif ($+{br}) { push @s, "\n" }
81 0 elsif ($+{tab}) { push @s, " " }
82 0 elsif ($+{text} ne '') { push @s, $+{text} }
83 }
84 0 ▶ @s or next;
Branch: 1 branch
BranchTrueFalseTotal
@s or next 0 0 0
85 0 push @p, join($param->{separator}, @s) . ("\n" x $param->{space});
86 }
87 0 my $text = join '', @p;
88 0 $replace_reference->() for $text;
89 0 $text;
90 }
91  
92 1 ▶use Archive::Zip 1.37 qw( :ERROR_CODES :CONSTANTS );
Subroutine BEGIN: called CC=1 SCAR=0.0
93  
94 0 ▶sub to_text {
Subroutine to_text: not called CC=9 SCAR=45.0
Pod undocumented
95 0 my $file = shift;
96 0 ▶ my $type = ($file =~ /\.((?:doc|xls|ppt)[xm])$/)[0] or return;
Branch: 1 branch
BranchTrueFalseTotal
my $type = ($file =~ /\.((?:doc|xls|ppt)[xm])$/u)[0] or return 0 0 0
97 0 ▶ return '' if -z $file;
Branch: 1 branch
BranchTrueFalseTotal
if -z $file 0 0 0
98 0 ▶ my $zip = Archive::Zip->new($file) or die;
Branch: 1 branch
BranchTrueFalseTotal
my $zip = "Archive::Zip"->new($file) or die 0 0 0
99 0 my @contents;
100 0 for my $entry (get_list($zip, $type)) {
101 0 ▶ my $member = $zip->memberNamed($entry) or next;
Branch: 1 branch
BranchTrueFalseTotal
my $member = $zip->memberNamed($entry) or next 0 0 0
102 0 ▶ my $xml = $member->contents or next;
Branch: 1 branch
BranchTrueFalseTotal
my $xml = $member->contents or next 0 0 0
103 0 ▶ my $text = xml2text $xml, $type or next;
Branch: 1 branch
BranchTrueFalseTotal
my $text = xml2text($xml, $type) or next 0 0 0
104 0 ▶ $file = encode 'utf8', $file if utf8::is_utf8($file);
Branch: 1 branch
BranchTrueFalseTotal
if utf8::is_utf8($file) 0 0 0
105 0 push @contents, "[ \"$file\" $entry ]\n\n$text";
106 }
107 0 join "\n", @contents;
108 }
109  
110 0 ▶sub get_list {
Subroutine get_list: not called CC=5 SCAR=34.0
Pod undocumented
111 0 my($zip, $type) = @_;
112 0 ▶ if ($type =~ /^doc[xm]$/) {
Branch: 3 branches
BranchTrueFalseTotal
if ($type =~ /^doc[xm]$/u) { } 0 0 0
elsif ($type =~ /^xls[xm]$/u) { } 0 0 0
elsif ($type =~ /^ppt[xm]$/u) { } 0 0 0
113 0 map { "word/$_.xml" } qw(document endnotes footnotes);
114 }
115 elsif ($type =~ /^xls[xm]$/) {
116 0 map { "xl/$_.xml" } qw(sharedStrings);
117 }
118 elsif ($type =~ /^ppt[xm]$/) {
119 0 map { $_->[0] }
120 0 sort { $a->[1] <=> $b->[1] }
121 0 ▶ map { m{(ppt/slides/slide(\d+)\.xml)$} ? [ $1, $2 ] : () }
Branch: 1 branch
BranchTrueFalseTotal
m[(ppt/slides/slide(\d+)\.xml)$]u ? : 0 0 0
122 $zip->memberNames;
123 }
124 }
125  
126 1 1;
↑ Summary textconv.pm »