| 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"
MC/DC0%0/1
our $lang_from //= "ORIGINAL"
our $lang_from
Truth table50%1/2
A
A= our $lang_from
| |||||||||||||||||||||||||||||||||||||||||||
| 17 | 2 | ▶ | our $lang_to //= 'JA'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
our $lang_to //= "JA"
MC/DC0%0/1
our $lang_to //= "JA"
our $lang_to
Truth table50%1/2
A
A= our $lang_to
| |||||||||||||||||||||||||||||||||||||||||||
| 18 | 2 | our $auth_key; | |||||||||||||||||||||||||||||||||||||||||
| 19 | 2 | ▶ | our $method //= 'deepl'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
our $method //= "deepl"
MC/DC0%0/1
our $method //= "deepl"
our $method
Truth table50%1/2
A
A= our $method
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 32 | 2 | ▶ | push @c, ('--auth-key', $auth_key) if $auth_key; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 33 | 2 | ▶ | push @c, ('--glossary-id', $glossary) if $glossary; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 34 | 2 | ▶ | if (@tags) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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/</</g; | |||||||||||||||||||||||||||||||||||||||||
| 51 | 1 | $text =~ s/>/>/g; | |||||||||||||||||||||||||||||||||||||||||
| 52 | 1 | $text =~ s{ | |||||||||||||||||||||||||||||||||||||||||
| 53 | <([a-z][a-z0-9_]*)\s+([a-z0-9_]+)="(\d+)"\s*/> | ||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 70 | or die "DeepL XML response has no opening wrapper.\n"; | ||||||||||||||||||||||||||||||||||||||||||
| 71 | 2 | ▶ | $text =~ s{</xlate>\r?\n?\z}{} | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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/</</g; | |||||||||||||||||||||||||||||||||||||||||
| 80 | 2 | $text =~ s/>/>/g; | |||||||||||||||||||||||||||||||||||||||||
| 81 | 2 | $text =~ s/&/&/g; | |||||||||||||||||||||||||||||||||||||||||
| 82 | 2 | ▶ | while ($source =~ m{ | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 100 | 0 | my $data = $deepl->command(@command, $text)->update->data; | |||||||||||||||||||||||||||||||||||||||||
| 101 | 0 | ▶ | @tags ? _xml_unwrap($data, $source) : $data; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition0%0/6
state $called++ || require Clipboard && 'Clipboard'->import
require Clipboard and "Clipboard"->import
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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition0%0/2
open my $fh, "/dev/tty" or die
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"
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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()
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"}
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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()
$App::Greple::xlate::max_length or $param{$App::Greple::xlate::deepl::method}{"max"}
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"}
| |||||||||||||||||||||||||||||||||||||||||||
| 142 | 0 | ▶ | while (@from) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 143 | 0 | my @tmp; | |||||||||||||||||||||||||||||||||||||||||
| 144 | 0 | my $len = 0; | |||||||||||||||||||||||||||||||||||||||||
| 145 | 0 | ▶ | while (@from) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 146 | 0 | my $next = length $from[0]; | |||||||||||||||||||||||||||||||||||||||||
| 147 | 0 | ▶ | last if $len + $next > $max; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||||||||||||||||