File Coverage

blib/lib/App/Greple/xlate/deepl.pm
Criterion Covered Total %
statement 23 71 32.3
branch 0 20 0.0
condition 0 12 0.0
subroutine 8 13 61.5
pod 0 4 0.0
total 31 120 25.8


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