line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package App::Greple::xlate::deepl; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
our $VERSION = "0.23"; |
4
|
|
|
|
|
|
|
|
5
|
1
|
|
|
1
|
|
1267
|
use v5.14; |
|
1
|
|
|
|
|
4
|
|
6
|
1
|
|
|
1
|
|
6
|
use warnings; |
|
1
|
|
|
|
|
15
|
|
|
1
|
|
|
|
|
26
|
|
7
|
1
|
|
|
1
|
|
579
|
use Encode; |
|
1
|
|
|
|
|
9998
|
|
|
1
|
|
|
|
|
76
|
|
8
|
1
|
|
|
1
|
|
8
|
use Data::Dumper; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
47
|
|
9
|
|
|
|
|
|
|
|
10
|
1
|
|
|
1
|
|
6
|
use List::Util qw(sum); |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
58
|
|
11
|
1
|
|
|
1
|
|
6
|
use App::cdif::Command; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
660
|
|
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
our $lang_from //= 'ORIGINAL'; |
14
|
|
|
|
|
|
|
our $lang_to //= 'JA'; |
15
|
|
|
|
|
|
|
our $auth_key; |
16
|
|
|
|
|
|
|
our $method = 'deepl'; |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
my %param = ( |
19
|
|
|
|
|
|
|
deepl => { max => 128 * 1024, sub => \&deepl }, |
20
|
|
|
|
|
|
|
clipboard => { max => 5000, sub => \&clipboard }, |
21
|
|
|
|
|
|
|
); |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
sub deepl { |
24
|
0
|
|
|
0
|
0
|
|
state $deepl = App::cdif::Command->new; |
25
|
0
|
0
|
|
|
|
|
state $command = [ 'deepl', 'text', |
26
|
|
|
|
|
|
|
'--to' => $lang_to, |
27
|
|
|
|
|
|
|
$auth_key ? ('--auth-key' => $auth_key) : () ]; |
28
|
0
|
|
|
|
|
|
$deepl->command([@$command, +shift])->update->data; |
29
|
|
|
|
|
|
|
} |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
sub clipboard { |
32
|
0
|
0
|
0
|
0
|
0
|
|
require Clipboard and import Clipboard unless state $called++; |
33
|
0
|
|
|
|
|
|
my $from = shift; |
34
|
0
|
|
|
|
|
|
my $length = length $from; |
35
|
0
|
|
|
|
|
|
Clipboard->copy($from); |
36
|
0
|
|
|
|
|
|
STDERR->printflush( |
37
|
|
|
|
|
|
|
"$length characters stored in the clipboard.\n", |
38
|
|
|
|
|
|
|
"Translate it to \"$lang_to\" and clip again.\n", |
39
|
|
|
|
|
|
|
"Then hit enter: "); |
40
|
0
|
0
|
0
|
|
|
|
if (open my $fh, "/dev/tty" or die) { |
41
|
0
|
|
|
|
|
|
my $answer = <$fh>; |
42
|
|
|
|
|
|
|
} |
43
|
0
|
|
|
|
|
|
my $to = Clipboard->paste; |
44
|
0
|
0
|
|
|
|
|
$to = decode('utf8', $to) if not utf8::is_utf8($_); |
45
|
0
|
|
|
|
|
|
return $to; |
46
|
|
|
|
|
|
|
} |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
sub xlate_each { |
49
|
0
|
|
0
|
0
|
0
|
|
my $call = $param{$method}->{sub} // die; |
50
|
0
|
|
|
|
|
|
my @count = map { int tr/\n/\n/ } @_; |
|
0
|
|
|
|
|
|
|
51
|
0
|
|
|
|
|
|
my $to = $call->(join '', @_); |
52
|
0
|
|
|
|
|
|
my @out = $to =~ /.*\n/g; |
53
|
0
|
0
|
|
|
|
|
if (@out < sum @count) { |
54
|
0
|
|
|
|
|
|
die "Unexpected response from deepl command:\n\n$to\n"; |
55
|
|
|
|
|
|
|
} |
56
|
0
|
|
|
|
|
|
map { join '', splice @out, 0, $_ } @count; |
|
0
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
sub xlate { |
60
|
0
|
0
|
|
0
|
0
|
|
my @from = map { /\n\z/ ? $_ : "$_\n" } @_; |
|
0
|
|
|
|
|
|
|
61
|
0
|
|
|
|
|
|
my @to; |
62
|
0
|
|
0
|
|
|
|
my $max = $App::Greple::xlate::max_length || $param{$method}->{max} // die; |
|
|
|
0
|
|
|
|
|
63
|
0
|
|
|
|
|
|
while (@from) { |
64
|
0
|
|
|
|
|
|
my @tmp; |
65
|
0
|
|
|
|
|
|
my $len = 0; |
66
|
0
|
|
|
|
|
|
while (@from) { |
67
|
0
|
|
|
|
|
|
my $next = length $from[0]; |
68
|
0
|
0
|
|
|
|
|
last if $len + $next > $max; |
69
|
0
|
|
|
|
|
|
$len += $next; |
70
|
0
|
|
|
|
|
|
push @tmp, shift @from; |
71
|
|
|
|
|
|
|
} |
72
|
0
|
|
|
|
|
|
push @to, xlate_each @tmp; |
73
|
|
|
|
|
|
|
} |
74
|
0
|
|
|
|
|
|
@to; |
75
|
|
|
|
|
|
|
} |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
1; |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
__DATA__ |