Net/Z3950/FOLIO/PostProcess/Transform.pm

filter: Statementstmt 98.6% 71 / 72 Branchbran 87.5% 7 / 8 Conditioncond 100% 2 / 2 MC/DCmcdc 100% 1 / 1 Subroutinesub 100% 10 / 10 Podpod 0.0% 0 / 5 totaltotal 92.8% 91 / 98 CC 7 SCAR 7.0
CC
7
Cov
98%
applyRule
3.1
transform
3.0
applyRegsub
2.0
CRAP 2.0
« Net/Z3950/FOLIO/ResultSet.pm ↑ Summary Net/Z3950/FOLIO/SurrogateDiagnostic.pm »
1 package Net::Z3950::FOLIO::PostProcess::Transform;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 ▶use Unicode::Diacritic::Strip 'strip_diacritics';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9  
10 30 ▶sub transform {
Subroutine transform: called CC=3 SCAR=11.0
Pod undocumented
11 30 my($cfg, $value, $getFieldFromRecord) = @_;
12  
13 30 ▶ if (ref $cfg eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $cfg eq 'HASH') { } 19 11 30
14 # Single transformation
15 19 return applyRule($cfg, $value, $getFieldFromRecord);
16 } else {
17 # List of transformations
18 11 foreach my $rule (@$cfg) {
19 14 $value = applyRule($rule, $value, $getFieldFromRecord);
20 }
21 11 return $value;
22 }
23 }
24  
25  
26 35 ▶sub applyRule {
Subroutine applyRule: called CC=3 SCAR=11.2
Pod undocumented
27 35 my($rule, $value, $getFieldFromRecord) = @_;
28  
29 35 my $op = $rule->{op};
30 35 ▶ if ($op eq 'stripDiacritics') {
Branch: 2 branches
BranchTrueFalseTotal
if ($op eq 'stripDiacritics') { } 3 32 35
elsif ($op eq 'regsub') { } 32 0 32
31 3 return applyStripDiacritics($rule, $value);
32 } elsif ($op eq 'regsub') {
33 32 return applyRegsub($rule, $value, $getFieldFromRecord);
34 } else {
35 0 die "unknown post-processing op '$op'";
36 }
37 }
38  
39  
40 32 ▶sub applyStripDiacritics {
Subroutine applyStripDiacritics: called CC=1 SCAR=0.0
Pod undocumented
41 32 my($_rule, $value) = @_;
42  
43 32 my $result = $value;
44  
45 32 $result = strip_diacritics($result);
46  
47 # Special cases required in ZF-31, but apparently not implemented by strip_diacritics
48 32 $result =~ s/ß/ss/g;
49 32 $result =~ s/ẞ/SS/g;
50 32 $result =~ s/þ/th/g;
51 32 $result =~ s/Þ/TH/g;
52 32 $result =~ s/Đ/D/g;
53 32 $result =~ s/ð/d/g;
54 32 $result =~ s/Æ/AE/g;
55 32 $result =~ s/æ/ae/g;
56 32 $result =~ s/Œ/OE/g;
57 32 $result =~ s/œ/oe/g;
58 32 $result =~ s/Ł/L/g;
59 32 $result =~ s/ł/l/g;
60 32 $result =~ s/t︠s︡/ts/g;
61 32 $result =~ s/i︠u︡/iu/g;
62 32 $result =~ s/i︠a︡/ia/g;
63 # Half a dozen different Unicode characters that get abused as apostrophes
64 32 $result =~ s/ʹ/'/g;
65 32 $result =~ s/ʻ/'/g;
66 32 $result =~ s/ʼ/'/g;
67 32 $result =~ s/ʽ/'/g;
68 32 $result =~ s/ʾ/'/g;
69 32 $result =~ s/ʿ/'/g;
70  
71  
72 # warn "stripping diacritics: '$value' -> '$result'";
73 32 return $result;
74 }
75  
76  
77 42 ▶sub applyRegsub {
Subroutine applyRegsub: called CC=2 SCAR=6.9
Pod undocumented
78 42 my($rule, $value, $getFieldFromRecord) = @_;
79  
80 42 my $pattern = $rule->{pattern};
81 42 my $rawReplacement = $rule->{replacement};
82 42 ▶ my $flags = $rule->{flags} || "";
Condition100%2/2
$rule->{'flags'} || ''
l!l
2121
MC/DC100%1/1
$rule->{'flags'} || ''
$rule->{'flags'}
Truth table100%2/2
A
A= $rule->{'flags'}
Aresult
11
00
83 42 my $res = $value;
84  
85 42 my $replacement = substituteReplacement($rawReplacement, $getFieldFromRecord);
86  
87 # See advice on this next part at https://perlmonks.org/?node_id=11124218
88 # In this approach, we construct some Perl code and evaluate it.
89 # This may leave some security holes, but we trust the people who write config files
90 42 $pattern =~ s;/;\\/;g;
91 42 $replacement =~ s;/;\\/;g;
92 42 eval "\$res =~ s/$pattern/$replacement/$flags";
93 # warn "regsub '$value' by s/$pattern/$replacement/$flags -> '$res'";
94 42 return $res;
95 }
96  
97  
98 42 ▶sub substituteReplacement {
Subroutine substituteReplacement: called CC=2 SCAR=6.9
Pod undocumented
99 42 my($raw, $getFieldFromRecord) = @_;
100  
101 42 my $res = '';
102 42 ▶ while ($raw =~ /(.*?)\%\{(.*?)\}(.*)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($raw =~ /(.*?)\%\{(.*?)\}(.*)/) 24 42 66
103 24 my($pre, $fieldname, $post) = ($1, $2, $3);
104 # warn "pre='$pre', fieldname='$fieldname', post='$post'";
105 24 $res .= $pre . &$getFieldFromRecord($fieldname);
106 24 $raw = $post;
107 }
108 42 $res .= $raw;
109  
110 42 return $res;
111 }
112  
113  
114 2 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
115 2 our @EXPORT_OK = qw(transform applyRule applyStripDiacritics applyRegsub);
116  
117  
118 2 1;
« Net/Z3950/FOLIO/ResultSet.pm ↑ Summary Net/Z3950/FOLIO/SurrogateDiagnostic.pm »