blib/lib/App/Greple/xlate/deepl.pm

filter: Statementstmt 59.2% 64 / 108 Branchbran 31.5% 12 / 38 Conditioncond 14.2% 3 / 21 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 70.5% 12 / 17 Podpod 0.0% 0 / 5 totaltotal 45.7% 91 / 199 CC 25 SCAR 27.7
CC
25
Cov
47%
clipboard
42.0
xlate
42.0
deepl
12.0
CRAP 16.0
« lib/App/Greple/xlate.pm ↑ Summary blib/lib/App/Greple/xlate/Cache.pm »
1 package App::Greple::xlate::deepl;
2  
3 2 our $VERSION = "2.03";
4  
5 2 ▶use v5.14;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 2 ▶use List::Util qw(sum);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Command::Run;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 2 ▶use App::Greple::xlate qw(%opt &opt);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use App::Greple::xlate::Lang qw(%LANGNAME);
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 2 ▶our $lang_from //= 'ORIGINAL';
Condition50%1/2
our $lang_from //= "ORIGINAL"
l!l
02
MC/DC0%0/1
our $lang_from //= "ORIGINAL"
our $lang_from
Truth table50%1/2
A
A= our $lang_from
Aresult
11
00
17 2 ▶our $lang_to //= 'JA';
Condition50%1/2
our $lang_to //= "JA"
l!l
02
MC/DC0%0/1
our $lang_to //= "JA"
our $lang_to
Truth table50%1/2
A
A= our $lang_to
Aresult
11
00
18 2 our $auth_key;
19 2 ▶our $method //= 'deepl';
Condition50%1/2
our $method //= "deepl"
l!l
02
MC/DC0%0/1
our $method //= "deepl"
our $method
Truth table50%1/2
A
A= our $method
Aresult
11
00
20  
21 2 my %param = (
22 deepl => { max => 128 * 1024, sub => \&deepl },
23 clipboard => { max => 5000, sub => \&clipboard },
24 );
25  
26 2 ▶sub deepl_command {
Subroutine deepl_command: called CC=6 SCAR=18.5
Pod undocumented
27 2 my @tags = @_;
28 2 my $glossary = $App::Greple::xlate::glossary;
29 2 my @c = ('deepl', 'text');
30 2 push @c, ('--to', $lang_to);
31 2 ▶ push @c, ('--from', $lang_from) if $lang_from ne 'ORIGINAL';
Branch: 1 branch
BranchTrueFalseTotal
if $App::Greple::xlate::deepl::lang_from ne "ORIGINAL" 0 2 2
32 2 ▶ push @c, ('--auth-key', $auth_key) if $auth_key;
Branch: 1 branch
BranchTrueFalseTotal
if $App::Greple::xlate::deepl::auth_key 0 2 2
33 2 ▶ push @c, ('--glossary-id', $glossary) if $glossary;
Branch: 1 branch
BranchTrueFalseTotal
if $glossary 0 2 2
34 2 ▶ if (@tags) {
Branch: 1 branch
BranchTrueFalseTotal
if (@tags) 1 1 2
35 # Marker placeholders are self-closing XML tags. Tell DeepL to
36 # preserve their tag names and id attributes instead of treating
37 # them as ordinary translatable text.
38 1 push @c, '--tag-handling', 'xml',
39 '--non-splitting-tags', join(',', @tags);
40 }
41 2 ▶ if (my @contexts = @{$opt{contexts}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@contexts) = @{$App::Greple::xlate::deepl::opt{"contexts"};}) 0 2 2
42 0 push @c, '--context' => join "\n", @contexts;
43 }
44 2 @c;
45 }
46  
47 1 ▶sub _xml_wrap {
Subroutine _xml_wrap: called CC=1 SCAR=0.0
48 1 my $text = shift;
49 1 $text =~ s/&/&/g;
50 1 $text =~ s/</&lt;/g;
51 1 $text =~ s/>/&gt;/g;
52 1 $text =~ s{
53 &lt;([a-z][a-z0-9_]*)\s+([a-z0-9_]+)="(\d+)"\s*/&gt;
54 }{<$1 $2="$3"/>}gx;
55 1 "<xlate>$text</xlate>";
56 }
57  
58 1 ▶sub _marker_tags {
Subroutine _marker_tags: called CC=2 SCAR=6.9
59 1 my $text = shift;
60 1 my %tag;
61 1 ▶ $tag{$1} = 1 while $text =~ m{
Branch: 1 branch
BranchTrueFalseTotal
$text =~ m[ <([a-z][a-z0-9_]*)\s+[a-z0-9_]+="\d+"\s*/> ]gux and do { $tag{$1} = 1 } 2 1 3
62 <([a-z][a-z0-9_]*)\s+[a-z0-9_]+="\d+"\s*/>
63 }gx;
64 1 sort keys %tag;
65 }
66  
67 2 ▶sub _xml_unwrap {
Subroutine _xml_unwrap: called CC=4 SCAR=13.9
68 2 my($text, $source) = @_;
69 2 ▶ $text =~ s/\A<xlate>//
Branch: 1 branch
BranchTrueFalseTotal
$text =~ s/\A<xlate>//u or die "DeepL XML response has no opening wrapper. " 2 0 2
70 or die "DeepL XML response has no opening wrapper.\n";
71 2 ▶ $text =~ s{</xlate>\r?\n?\z}{}
Branch: 1 branch
BranchTrueFalseTotal
$text =~ s[</xlate>\r?\n?\z][]u or die "DeepL XML response has no closing wrapper. " 2 0 2
72 or die "DeepL XML response has no closing wrapper.\n";
73 2 $text =~ s{
74 <([a-z][a-z0-9_]*)\s+([a-z0-9_]+)=["'](\d+)["']\s*/>
75 }{<$1 $2="$3" />}gx;
76 2 $text =~ s{
77 <([a-z][a-z0-9_]*)\s+([a-z0-9_]+)=["'](\d+)["']\s*>\s*</\1>
78 }{<$1 $2="$3" />}gx;
79 2 $text =~ s/&lt;/</g;
80 2 $text =~ s/&gt;/>/g;
81 2 $text =~ s/&amp;/&/g;
82 2 ▶ while ($source =~ m{
Branch: 1 branch
BranchTrueFalseTotal
if ($source =~ m[ (<[a-z][a-z0-9_]*\s+[a-z0-9_]+="\d+"\s*/>) (\s+) (?=(<[a-z][a-z0-9_]*\s+[a-z0-9_]+="\d+"\s*/>)) ]gux) 1 2 3
83 (<[a-z][a-z0-9_]*\s+[a-z0-9_]+="\d+"\s*/>)
84 (\s+)
85 (?=(<[a-z][a-z0-9_]*\s+[a-z0-9_]+="\d+"\s*/>))
86 }gx) {
87 1 my($left, $space, $right) = ($1, $2, $3);
88 1 $text =~ s/\Q$left$right\E/$left$space$right/g;
89 }
90 2 $text;
91 }
92  
93 0 ▶sub deepl {
Subroutine deepl: not called CC=3 SCAR=24.8
Pod undocumented
94 0 state $deepl = Command::Run->new;
95 0 my $text = shift;
96 0 my @tags = _marker_tags($text);
97 0 my @command = deepl_command(@tags);
98 0 my $source = $text;
99 0 ▶ $text = _xml_wrap($text) if @tags;
Branch: 1 branch
BranchTrueFalseTotal
if @tags 0 0 0
100 0 my $data = $deepl->command(@command, $text)->update->data;
101 0 ▶ @tags ? _xml_unwrap($data, $source) : $data;
Branch: 1 branch
BranchTrueFalseTotal
@tags ? : 0 0 0
102 }
103  
104 0 ▶sub clipboard {
Subroutine clipboard: not called CC=6 SCAR=37.4
Pod undocumented
105 0 ▶ require Clipboard and import Clipboard unless state $called++;
Branch: 1 branch
BranchTrueFalseTotal
unless state $called++ 0 0 0
Condition0%0/6
state $called++ || require Clipboard && 'Clipboard'->import
l!l&&r!l&&!r
000
require Clipboard and "Clipboard"->import
!ll&&!rl&&r
000
MC/DC0%0/3
state $called++ || require Clipboard && 'Clipboard'->import
state $called++ require Clipboard "Clipboard"->import
Truth table0%0/4
A || B and C
A= state $called++B= require ClipboardC= "Clipboard"->import
ABCresult
1XX1
0111
00X0
0100
106 0 my $from = shift;
107 0 my $length = length $from;
108 0 Clipboard->copy($from);
109 0 STDERR->printflush(
110 "$length characters stored in the clipboard.\n",
111 "Translate it to \"$lang_to\" and clip again.\n",
112 "Then hit enter: ");
113 0 ▶ if (open my $fh, "/dev/tty" or die) {
Branch: 1 branch
BranchTrueFalseTotal
if (open my $fh, "/dev/tty" or die) 0 0 0
Condition0%0/2
open my $fh, "/dev/tty" or die
l!l
00
MC/DC0%0/1
open my $fh, "/dev/tty" or die
open my $fh, "/dev/tty"
Truth table0%0/2
A
A= open my $fh, "/dev/tty"
Aresult
11
00
114 0 my $answer = <$fh>;
115 }
116 0 my $to = Clipboard->paste;
117 0 ▶ $to = decode('utf8', $to) if not utf8::is_utf8($_);
Branch: 1 branch
BranchTrueFalseTotal
unless utf8::is_utf8($_) 0 0 0
118 0 return $to;
119 }
120  
121 0 ▶sub _progress {
Subroutine _progress: not called CC=2 SCAR=17.9
122 0 ▶ print STDERR @_ if opt('progress');
Branch: 1 branch
BranchTrueFalseTotal
if opt("progress") 0 0 0
123 }
124  
125 0 ▶sub xlate_each {
Subroutine xlate_each: not called CC=3 SCAR=24.8
Pod undocumented
126 0 ▶ my $call = $param{$method}->{sub} // die;
Condition0%0/2
$param{$App::Greple::xlate::deepl::method}{"sub"} // die()
l!l
00
MC/DC0%0/1
$param{$App::Greple::xlate::deepl::method}{"sub"} // die()
$param{$App::Greple::xlate::deepl::method}{"sub"}
Truth table0%0/2
A
A= $param{$App::Greple::xlate::deepl::method}{"sub"}
Aresult
11
00
127 0 my @count = map { int tr/\n/\n/ } @_;
128 0 _progress("From:\n", map s/^/\t< /mgr, @_);
129 0 my $to = $call->(join '', @_);
130 0 my @out = $to =~ /.*\n/g;
131 0 _progress("To:\n", map s/^/\t> /mgr, @out);
132 0 ▶ if (@out < sum @count) {
Branch: 1 branch
BranchTrueFalseTotal
if (@out < &sum(@count)) 0 0 0
133 0 die "Unexpected response:\n\n$to\n";
134 }
135 0 map { join '', splice @out, 0, $_ } @count;
136 }
137  
138 0 ▶sub xlate {
Subroutine xlate: not called CC=6 SCAR=37.4
Pod undocumented
139 0 my @from = @_;
140 0 my @to;
141 0 ▶ my $max = $App::Greple::xlate::max_length || $param{$method}->{max} // die;
Condition0%0/5
($App::Greple::xlate::max_length || $param{$App::Greple::xlate::deepl::method}{"max"}) // die()
l!l
00
$App::Greple::xlate::max_length or $param{$App::Greple::xlate::deepl::method}{"max"}
l!l&&r!l&&!r
000
MC/DC0%0/2
($App::Greple::xlate::max_length || $param{$App::Greple::xlate::deepl::method}{"max"}) // die()
$App::Greple::xlate::max_length $param{$App::Greple::xlate::deepl::method}{"max"}
Truth table0%0/3
A or B
A= $App::Greple::xlate::max_lengthB= $param{$App::Greple::xlate::deepl::method}{"max"}
ABresult
1X1
011
000
142 0 ▶ while (@from) {
Branch: 1 branch
BranchTrueFalseTotal
if (@from) 0 0 0
143 0 my @tmp;
144 0 my $len = 0;
145 0 ▶ while (@from) {
Branch: 1 branch
BranchTrueFalseTotal
if (@from) 0 0 0
146 0 my $next = length $from[0];
147 0 ▶ last if $len + $next > $max;
Branch: 1 branch
BranchTrueFalseTotal
if $len + $next > $max 0 0 0
148 0 $len += $next;
149 0 push @tmp, shift @from;
150 }
151 0 push @to, xlate_each @tmp;
152 }
153 0 @to;
154 }
155  
156 2 1;
157  
158 __DATA__
« lib/App/Greple/xlate.pm ↑ Summary blib/lib/App/Greple/xlate/Cache.pm »