lib/App/Greple/xlate.pm

filter: Statementstmt 92.7% 293 / 316 Branchbran 74.4% 146 / 196 Conditioncond 74.2% 72 / 97 MC/DCmcdc 54.5% 30 / 55 Subroutinesub 80.9% 17 / 21 Podpod 5.0% 1 / 20 totaltotal 79.2% 559 / 705 CC 147 SCAR 29.8
CC
147
Cov
84%
begin
38.0
cache_update
34.3
region_context
18.5
CRAP 19.7
« blib/lib/App/Greple/xlate/gpty/gpt4.pm ↑ Summary blib/lib/App/Greple/xlate/deepl.pm »
1 package App::Greple::xlate;
2  
3 our $VERSION = "2.03";
4  
5 =encoding utf-8
6  
7 =head1 NAME
8  
9 App::Greple::xlate - translation support module for greple
10  
11 =head1 SYNOPSIS
12  
13 greple -Mxlate --xlate-engine gpt5 --xlate pattern target-file
14  
15 greple -Mxlate --xlate-engine deepl --xlate pattern target-file
16  
17 To use GPT-6 Astra:
18  
19 xlate -e gpt6 -t JA file.txt
20  
21 If C<llm models> does not list C<gpt-6-astra>, see the model registration
22 instructions in L<App::Greple::xlate::llm::gpt6>.
23  
24 =head1 VERSION
25  
26 Version 2.03
27  
28 =head1 DESCRIPTION
29  
30 B<Greple> B<xlate> module find desired text blocks and replace them by
31 the translated text. The primary engine is GPT-5.6 Terra (F<llm/gpt5.pm>),
32 which calls the L<llm|https://llm.datasette.io/> command; DeepL
33 (F<deepl.pm>) and legacy B<gpty>-based engines are also included.
34  
35 Translations are cached per file, so re-running a command costs
36 nothing for unchanged text. When a document is edited, only the
37 changed paragraphs are sent to the API again; a context-aware engine
38 also receives the surrounding translations, the raw source text
39 around the change, and the previous version of the edited paragraph,
40 so the new translation keeps the established wording (see
41 B<--xlate-context-window>). Sensitive strings can be concealed
42 before transmission (see L</ANONYMIZATION AND TEMPLATES>).
43  
44 If you want to translate normal text blocks in a document written in
45 the Perl's pod style, use B<greple> command with C<--xlate-engine gpt5>
46 and C<perl> module like this:
47  
48 greple -Mxlate --xlate-engine gpt5 -Mperl --pod --re '^([\w\pP].*\n)+' --all foo.pm
49  
50 In this command, pattern string C<^([\w\pP].*\n)+> means consecutive
51 lines starting with alpha-numeric and punctuation letter. This
52 command show the area to be translated highlighted. Option B<--all>
53 is used to produce entire text.
54  
55 =for html <p>
56 <img width="750" src="https://raw.githubusercontent.com/kaz-utashiro/App-Greple-xlate/main/images/select-area.png">
57 </p>
58  
59 Then add C<--xlate> option to translate the selected area. Then, it
60 will find the desired sections and replace them by the translation
61 engine's output.
62  
63 By default, original and translated text is printed in the "conflict
64 marker" format compatible with L<git(1)>. Using C<ifdef> format, you
65 can get desired part by L<unifdef(1)> command easily. Output format
66 can be specified by B<--xlate-format> option.
67  
68 =for html <p>
69 <img width="750" src="https://raw.githubusercontent.com/kaz-utashiro/App-Greple-xlate/main/images/format-conflict.png">
70 </p>
71  
72 If you want to translate entire text, use B<--match-all> option. This
73 is a short-cut to specify the pattern C<(?s).+> which matches entire
74 text.
75  
76 Conflict marker format data can be viewed in side-by-side style by
77 L<sdif|App::sdif> command with C<-V> option. Since it makes no sense
78 to compare on a per-string basis, the C<--no-cdif> option is
79 recommended. If you do not need to color the text, specify
80 C<--no-textcolor> (or C<--no-tc>).
81  
82 sdif -V --no-filename --no-tc --no-cdif data_shishin.deepl-EN-US.cm
83  
84 =for html <p>
85 <img width="750" src="https://raw.githubusercontent.com/kaz-utashiro/App-Greple-xlate/main/images/sdif-cm-view.png">
86 </p>
87  
88 =head1 NORMALIZATION
89  
90 Processing is done in specified units, but in the case of a sequence
91 of multiple lines of non-empty text, they are converted together into
92 a single line. This operation is performed as follows:
93  
94 =over 2
95  
96 =item *
97  
98 Remove white space at the beginning and end of each line.
99  
100 =item *
101  
102 If a line ends with a full-width punctuation character, concatenate
103 with next line.
104  
105 =item *
106  
107 If a line ends with a full-width character and the next line begins
108 with a full-width character, concatenate the lines.
109  
110 =item *
111  
112 If either the end or the beginning of a line is not a full-width
113 character, concatenate them by inserting a space character.
114  
115 =back
116  
117 Cache data is managed based on the normalized text, so even if
118 modifications are made that do not affect the normalization results,
119 the cached translation data will still be effective.
120  
121 This normalization process is performed only for the first (0th) and
122 even-numbered pattern. Thus, if two patterns are specified as
123 follows, the text matching the first pattern will be processed after
124 normalization, and no normalization process will be performed on the
125 text matching the second pattern.
126  
127 greple -Mxlate -E normalized -E not-normalized
128  
129 Therefore, use the first pattern for text that is to be processed by
130 combining multiple lines into a single line, and use the second
131 pattern for pre-formatted text. If there is no text to match in the
132 first pattern, use a pattern that does not match anything, such as
133 C<(?!)>.
134  
135 =head1 MASKING
136  
137 Occasionally, there are parts of text that you do not want translated.
138 For example, tags in markdown files. DeepL suggests that in such
139 cases, the part of the text to be excluded be converted to XML tags,
140 translated, and then restored after the translation is complete. To
141 support this, it is possible to specify the parts to be masked from
142 translation.
143  
144 --xlate-setopt maskfile=MASKPATTERN
145  
146 This will interpret each line of the file C<MASKPATTERN> as a regular
147 expression, translate strings matching it, and revert after
148 processing. Lines beginning with C<#> are ignored.
149  
150 Complex pattern can be written on multiple lines with backslash
151 escaped newline.
152  
153 How the text is transformed by masking can be seen by B<--xlate-mask>
154 option.
155  
156 Mask placeholders are well-formed self-closing XML tags such as
157 C<< <m id="1" /> >>. JSON-based LLM engines receive the tags in their
158 input arrays. For DeepL, a request containing marker tags is escaped
159 and enclosed in a temporary C<< <xlate> >> root, with XML tag handling
160 enabled and each marker category registered as a non-splitting tag.
161 The wrapper is removed before the placeholders are validated and
162 restored.
163  
164 Masking protects markup from being translated. To conceal sensitive
165 strings from the translation service itself, see L</ANONYMIZATION AND
166 TEMPLATES>; both can be used together.
167  
168 This interface is experimental and subject to change in the future.
169  
170 =head1 ANONYMIZATION AND TEMPLATES
171  
172 Sensitive strings can be concealed before they are sent to the
173 translation API and restored in the output. Three sources of
174 anonymization rules are available: a dictionary file
175 (B<--xlate-anonymize>), inline marks in the document itself
176 (B<--xlate-anonymize-mark>), and YAML front matter values
177 (B<--xlate-frontmatter>). Each string is replaced by a category tag
178 such as C<< <person id="1" /> >> during transmission. The concealment
179 target is API transmission only: local cache files store restored
180 plain text. Use B<--xlate-dryrun> to inspect exactly what would be
181 transmitted.
182  
183 For form documents (quarterly reports and the like), define the
184 actors up front and reference them in the body:
185  
186 ---
187 報告者: 山田太郎
188 発注会社: アクメ株式会社
189 ---
190 本件について {{ 報告者 }} が調査を行った。
191  
192 Translate the template once per language with C<--xlate-template>
193 (and C<--xlate-frontmatter> when the values are kept in the file),
194 then render each case with B<pandoc-embedz> standalone mode --
195 values under C<global:> in an external config never reach the
196 translation API at all:
197  
198 greple -Mxlate --xlate --xlate-engine=gpt5 --xlate-to=EN-US \
199 --xlate-template= --xlate-format=xtxt \
200 --match-paragraph --all --need=0 \
201 report-template.md > report-template.EN.md
202 pandoc-embedz --standalone report-template.EN.md \
203 -c case-123.yaml -o report-123.EN.md < /dev/null
204  
205 For inline marks, providing a macro definition config makes the same
206 translated template render either the real names or a redacted
207 version:
208  
209 # macros.yaml # macros-redacted.yaml
210 preamble: | preamble: |
211 {% macro person(name) %}{{ name }}{% endmacro %}
212 {% macro person(name) %}(関係者){% endmacro %}
213  
214 Exclude embedz blocks from translation when a document contains them:
215  
216 --exclude '^```embedz\n(?s:.*?)^```\n'
217  
218 =head1 OPTIONS
219  
220 =over 7
221  
222 =item B<--xlate>
223  
224 =item B<--xlate-color>
225  
226 =item B<--xlate-fold>
227  
228 =item B<--xlate-fold-width>=I<n> (Default: 70)
229  
230 Invoke the translation process for each matched area.
231  
232 Without this option, B<greple> behaves as a normal search command. So
233 you can check which part of the file will be subject of the
234 translation before invoking actual work.
235  
236 Command result goes to standard out, so redirect to file if necessary,
237 or consider to use L<App::Greple::update> module.
238  
239 Option B<--xlate> calls B<--xlate-color> option with B<--color=never>
240 option.
241  
242 With B<--xlate-fold> option, converted text is folded by the specified
243 width. Default width is 70 and can be set by B<--xlate-fold-width>
244 option. Four columns are reserved for run-in operation, so each line
245 could hold 74 characters at most.
246  
247 =item B<--xlate-engine>=I<engine>
248  
249 Specifies the translation engine to be used.
250  
251 At this time, the following engines are available
252  
253 =over 2
254  
255 =item * B<gpt6>: gpt-6-astra (via the C<llm> command; see
256 L<App::Greple::xlate::llm::gpt6> for model registration)
257  
258 =item * B<gpt5>: gpt-5.6-terra (via the C<llm> command)
259  
260 =item * B<deepl>: DeepL API (via the C<deepl> command)
261  
262 =item * B<gpt3>: gpt-3.5-turbo (legacy, via the C<gpty> command)
263  
264 =item * B<gpt4o>: gpt-4o-mini (legacy, via the C<gpty> command)
265  
266 =back
267  
268 Engine modules are searched in backend namespaces first (C<llm>, then
269 C<gpty>), then directly under C<App::Greple::xlate>. So C<gpt5> loads
270 C<App::Greple::xlate::llm::gpt5> which calls the C<llm> command, while
271 C<gpt4o> falls back to C<App::Greple::xlate::gpty::gpt4o>. Use
272 C<--xlate-setopt backend=gpty> to force a specific backend.
273  
274 =item B<--xlate-labor>
275  
276 =item B<--xlabor>
277  
278 Instead of calling translation engine, you are expected to work for.
279 After preparing text to be translated, they are copied to the
280 clipboard. You are expected to paste them to the form, copy the
281 result to the clipboard, and hit return.
282  
283 =item B<--xlate-to> (Default: C<EN-US>)
284  
285 Specify the target language. LLM engines accept any language name
286 or code the model understands; it is interpolated into the
287 translation prompt. You can get available languages by C<deepl
288 languages> command when using B<DeepL> engine.
289  
290 =item B<--xlate-from> (Default: C<ORIGINAL>)
291  
292 Label used for the original text in C<conflict>, C<colon> and
293 C<ifdef> output formats. With the B<DeepL> engine a non-default
294 value is also passed as the source language.
295  
296 =item B<--xlate-format>=I<format> (Default: C<conflict>)
297  
298 Specify the output format for original and translated text.
299  
300 The following formats other than C<xtxt> assume that the part to be
301 translated is a collection of lines. In fact, it is possible to
302 translate only a portion of a line, but specifying a format other than
303 C<xtxt> will not produce meaningful results.
304  
305 =over 4
306  
307 =item B<conflict>, B<cm>
308  
309 Original and converted text are printed in L<git(1)> conflict marker
310 format.
311  
312 <<<<<<< ORIGINAL
313 original text
314 =======
315 translated Japanese text
316 >>>>>>> JA
317  
318 You can recover the original file by next L<sed(1)> command.
319  
320 sed -e '/^<<<<<<< /d' -e '/^=======$/,/^>>>>>>> /d'
321  
322 =item B<colon>, I<:::::::>
323  
324 The original and translated text are output in a markdown's custom
325 container style.
326  
327 ::::::: ORIGINAL
328 original text
329 :::::::
330 ::::::: JA
331 translated Japanese text
332 :::::::
333  
334 Above text will be translated to the following in HTML.
335  
336 <div class="ORIGINAL">
337 original text
338 </div>
339 <div class="JA">
340 translated Japanese text
341 </div>
342  
343 Number of colon is 7 by default. If you specify colon sequence like
344 C<:::::>, it is used instead of 7 colons.
345  
346 =item B<ifdef>
347  
348 Original and converted text are printed in L<cpp(1)> C<#ifdef>
349 format.
350  
351 #ifdef ORIGINAL
352 original text
353 #endif
354 #ifdef JA
355 translated Japanese text
356 #endif
357  
358 You can retrieve only Japanese text by the B<unifdef> command:
359  
360 unifdef -UORIGINAL -DJA foo.ja.pm
361  
362 =item B<space>
363  
364 =item B<space+>
365  
366 Original and converted text are printed separated by single blank
367 line. For C<space+>, it also outputs a newline after the converted
368 text.
369  
370 =item B<xtxt>
371  
372 If the format is C<xtxt> (translated text) or unkown, only translated
373 text is printed.
374  
375 =back
376  
377 =item B<--xlate-maxlen>=I<chars> (Default: 0)
378  
379 Specify the maximum length of text to be sent to the API at once.
380 The default value 0 means the engine's own limit: for the free DeepL
381 account service that is 128K for the API (B<--xlate>) and 5000 for
382 the clipboard interface (B<--xlate-labor>). You may be able to
383 change these value if you are using Pro service.
384  
385 =item B<--xlate-maxline>=I<n> (Default: 0)
386  
387 Specify the maximum lines of text to be sent to the API at once.
388  
389 Set this value to 1 if you want to translate one line at a time. This
390 option takes precedence over the C<--xlate-maxlen> option.
391  
392 =item B<--xlate-prompt>=I<text>
393  
394 Specify a custom prompt to be sent to the translation engine. This
395 option is available for the LLM engines (C<gpt3>, C<gpt4o>, C<gpt5>, C<gpt6>)
396 but not for DeepL. You can customize the translation behavior by
397 providing specific instructions to the AI model. If the prompt
398 contains C<%s>, it will be replaced with the target language name.
399 For the llm-backed C<gpt5> and C<gpt6> engines, the document is supplied separately
400 as a JSON request whose C<input> member is the array to translate and
401 whose optional C<context> member contains reference data. A fixed
402 instruction that treats those members as document data, not commands,
403 is appended even when a custom prompt is used.
404  
405 =item B<--xlate-context>=I<text>
406  
407 Specify additional context information to be sent to the translation
408 engine. This option can be used multiple times to provide multiple
409 context strings. The context information helps the translation engine
410 understand the background and produce more accurate translations.
411  
412 =item B<--xlate-context-window>=I<n>
413  
414 (Context-aware engines only, e.g. C<gpt5> or C<gpt6> on the llm backend)
415 Number of surrounding translated blocks passed as reference context
416 when re-translating changed blocks (default 2). The context also
417 includes the raw source text around the changed region (headings,
418 list structure, captions) and, when available, the previous version
419 of the changed text recovered from the cache, so that unchanged
420 wording is preserved. Set to 0 to disable context-aware translation
421 entirely.
422 Note that each changed region is translated in its own API call and
423 the context can add up to about 8000 characters to the JSON user
424 request, so context-aware translation trades some extra cost for
425 consistency. Document-derived context is kept out of the system
426 prompt.
427  
428 =item B<--xlate-cache-seed>=I<file>
429  
430 Initialize a new document's cache from another document's cache
431 file. Useful for periodic reports: seed the new issue's cache with
432 the previous issue's, so unchanged paragraphs are not re-translated
433 and edited paragraphs keep the previous issue's wording. The seed
434 is used only when the target cache is empty; otherwise it is
435 ignored with a warning. With the default C<--xlate-cache=auto>, specifying a seed also
436 implies creating the new document's cache file.
437  
438 =item B<--xlate-anonymize>=I<file>
439  
440 Anonymize sensitive strings before they are sent to the translation
441 API, and restore them in the output. The dictionary file gives one
442 entry per item: in JSON (canonical, machine-generatable)
443  
444 [ { "category": "person", "text": "山田太郎" },
445 { "category": "company", "regex": "アクメ(株式会社)?" } ]
446  
447 or in a simple line format (C<category pattern>, C</.../> for regex).
448 Each item is replaced by a category tag such as C<< <person id="1" /> >>;
449 the same string always gets the same tag, so the model can keep track
450 of who is who. Unknown JSON fields are ignored, so generators (e.g. a
451 local LLM extracting entities) may add their own annotations.
452 Category C<lit> is reserved. Local cache files still store restored
453 plain text: the concealment target is API transmission only.
454  
455 A dictionary can be generated by an external tool -- for example a
456 local model extracting sensitive entities:
457  
458 llm -m <local-model> \
459 -s 'Extract sensitive entities as a JSON array of objects
460 with "category" and "text" fields.' \
461 < report.md > report.anon.json
462 greple -Mxlate --xlate-anonymize=report.anon.json ...
463  
464 A UTF-8 BOM in the file is tolerated. Values in the front matter
465 line format may carry a trailing comment only on their own line, not
466 after the value.
467  
468 =item B<--xlate-anonymize-mark>[=I<regex>]
469  
470 Collect anonymization entries from inline marks in the document
471 itself. Mark the first occurrence like C<{{ person("山田太郎") }}>
472 and every occurrence of the string document-wide is anonymized. The
473 mark itself stays in the source and in the translation, so a document
474 can also be processed by a Jinja2-style macro processor (define the
475 C<person> macro to print or redact the name). A custom I<regex> must
476 contain C<< (?<category>...) >> and C<< (?<text>...) >> named captures.
477  
478 Note that with an optional-value option like this, a following
479 file argument would be taken as the value: write
480 C<--xlate-anonymize-mark=> (with a trailing C<=>) when using the
481 default notation.
482  
483 Alternative notations can be configured, for example
484 C<< --xlate-anonymize-mark='@@(?<category>[a-z][a-z0-9_]*):(?<text>[^\n]+?)@@' >>
485 for C<@@person:NAME@@>-style marks, or an HTML-comment form that stays
486 invisible in rendered Markdown. Mark rules are collected per
487 document: a string marked in one input file is not concealed in
488 another file of the same run (unlike front matter values, which
489 accumulate across files).
490  
491 =item B<--xlate-template>[=I<regex>]
492  
493 Treat template expressions (default: Jinja2 C<{{ ... }}>,
494 C<{% ... %}>, C<{# ... #}>) as opaque placeholders: instruct the
495 model to copy them unchanged and verify, per block, that the response
496 contains exactly the same expressions, each the same number of times.
497 Their order may change, since translation legitimately reorders them
498 to follow the target language word order. A broken expression
499 aborts the run; the cache is checkpointed and frozen, so nothing paid
500 for is lost.
501  
502 Note that with an optional-value option like this, a following
503 file argument would be taken as the value: write
504 C<--xlate-template=> (with a trailing C<=>) when using the
505 default notation.
506  
507 =item B<--xlate-frontmatter>
508  
509 Treat a leading C<---> ... C<---> block as YAML front matter: exclude
510 it from translation and from the phase-2 context slices, and add its
511 flat C<key: value> values to the anonymization rules (category
512 C<var>) as a safety net. With multiple input files the collected
513 values accumulate (erring on the side of concealment).
514  
515 Always leave a blank line after the closing C<--->. With a
516 paragraph-style match pattern, front matter that runs directly into
517 the body text forms one straddling block that the exclusion cannot
518 suppress (a warning is printed in that case); the values are still
519 anonymized, but the front matter itself would be sent for
520 translation.
521  
522 =item B<--xlate-glossary>=I<glossary>
523  
524 Specify a glossary ID to be used for translation. This option is only
525 available when using the DeepL engine. The glossary ID should be obtained
526 from your DeepL account and ensures consistent translation of specific terms.
527  
528 =item B<--xlate-dryrun>
529  
530 Do not call the translation API; instead show, through the progress
531 display, each payload exactly as it would be transmitted (after
532 anonymization and masking). Useful for checking what leaves the
533 machine and for estimating the cost of a run.
534  
535 =item B<-->[B<no->]B<xlate-progress> (Default: True)
536  
537 See the translation result in real time in the STDERR output. The
538 C<From> payload is shown as transmitted, after anonymization and
539 masking.
540  
541 =item B<--xlate-review>
542  
543 For a one-to-one changed block, show the smallest contiguous changed
544 span in the old and new source, followed by the corresponding span in
545 the old and new translation. The report is written to STDERR, makes no
546 additional API call, and is omitted when old and new blocks cannot be
547 paired unambiguously.
548  
549 =item B<--xlate-stripe>
550  
551 Use L<App::Greple::stripe> module to show the matched part by zebra
552 striping fashion. This is useful when the matched parts are connected
553 back-to-back.
554  
555 The color palette is switched according to the background color of the
556 terminal. If you want to specify explicitly, you can use
557 B<--xlate-stripe-light> or B<--xlate-stripe-dark>.
558  
559 =item B<--xlate-mask>
560  
561 Perform masking function and display the converted text as is without
562 restoration.
563  
564 =item B<--match-all>
565  
566 Set the whole text of the file as a target area.
567  
568 =item B<--lineify-cm>
569  
570 =item B<--lineify-colon>
571  
572 In the case of the C<cm> and C<colon> formats, the output is split and
573 formatted line by line. Therefore, if only a portion of a line is to
574 be translated, the expected result cannot be obtained. These filters
575 fix output that is corrupted by translating part of a line into normal
576 line-by-line output.
577  
578 In the current implementation, if multiple parts of a line are
579 translated, they are output as independent lines.
580  
581 =back
582  
583 =head1 CACHE OPTIONS
584  
585 B<xlate> module can store cached text of translation for each file and
586 read it before execution to eliminate the overhead of asking to
587 server. With the default cache strategy C<auto>, it maintains cache
588 data only when the cache file exists for target file.
589  
590 Use B<--xlate-cache=clear> to initiate cache management or to clean up
591 all existing cache data. Once executed with this option, a new cache
592 file will be created if one does not exist and then automatically
593 maintained afterward.
594  
595 =over 7
596  
597 =item --xlate-cache=I<strategy>
598  
599 =over 4
600  
601 =item C<auto> (Default)
602  
603 Maintain the cache file if it exists.
604  
605 =item C<create>
606  
607 Create empty cache file and exit.
608  
609 =item C<always>, C<yes>, C<1>
610  
611 Maintain cache anyway as far as the target is normal file.
612  
613 =item C<clear>
614  
615 Clear the cache data first.
616  
617 =item C<never>, C<no>, C<0>
618  
619 Never use cache file even if it exists.
620  
621 =item C<accumulate>
622  
623 By default behavior, unused data is removed from the cache file. If
624 you don't want to remove them and keep in the file, use C<accumulate>.
625  
626 =back
627  
628 =item B<--xlate-update>
629  
630 This option forces to update cache file even if it is not necessary.
631  
632 =back
633  
634 =head1 COMMAND LINE INTERFACE
635  
636 You can easily use this module from the command line by using the
637 C<xlate> command included in the distribution. See the C<xlate> man
638 page for usage.
639  
640 The C<xlate> command supports GNU-style long options such as
641 C<--to-lang>, C<--from-lang>, C<--engine>, and C<--file>. Use
642 C<xlate -h> to see all available options.
643  
644 The C<xlate> command works in concert with the Docker environment, so
645 even if you do not have anything installed on hand, you can use it as
646 long as Docker is available. Use C<-D> or C<-C> option.
647  
648 Docker operations are handled by L<App::dozo>, which can also be
649 used as a standalone command. The C<dozo> command supports the
650 C<.dozorc> configuration file for persistent container settings.
651  
652 Also, since makefiles for various document styles are provided,
653 translation into other languages is possible without special
654 specification. Use C<-M> option.
655  
656 You can also combine the Docker and C<make> options so that you can
657 run C<make> in a Docker environment.
658  
659 Running like C<xlate -C> will launch a shell with the current working
660 git repository mounted.
661  
662 Read Japanese article in L</SEE ALSO> section for detail.
663  
664 =head1 EMACS
665  
666 Load the F<xlate.el> file included in the repository to use C<xlate>
667 command from Emacs editor. C<xlate-region> function translate the
668 given region. Default language is C<EN-US> and you can specify
669 language invoking it with prefix argument.
670  
671 =for html <p>
672 <img width="750" src="https://raw.githubusercontent.com/kaz-utashiro/App-Greple-xlate/main/images/emacs.png">
673 </p>
674  
675 =head1 ENVIRONMENT
676  
677 =over 7
678  
679 =item DEEPL_AUTH_KEY
680  
681 Set your authentication key for DeepL service.
682  
683 =item OPENAI_API_KEY
684  
685 OpenAI authentication key, used by the legacy B<gpty> engines. The
686 C<llm>-based B<gpt5> engine reads this variable too, but keys stored
687 with C<llm keys set openai> also work.
688  
689 =item GREPLE_XLATE_CACHE
690  
691 Set the default cache strategy (see L</CACHE OPTIONS>).
692  
693 =back
694  
695 =head1 INSTALL
696  
697 =head2 CPANMINUS
698  
699 $ cpanm App::Greple::xlate
700  
701 =head2 TOOLS
702  
703 Install the command line tool for the engine you use: C<llm> for the
704 B<gpt5> engine, C<deepl> for DeepL, C<gpty> for the legacy GPT
705 engines.
706  
707 L<https://llm.datasette.io/>
708  
709 L<https://github.com/DeepLcom/deepl-python>
710  
711 L<https://github.com/tecolicom/App-gpty>
712  
713 =head1 SEE ALSO
714  
715 =head2 MODULES
716  
717 L<App::Greple::xlate::llm>,
718 L<App::Greple::xlate::deepl>
719  
720 L<App::dozo> - Generic Docker runner used by xlate for container operations
721  
722 =head2 RELATED MODULES
723  
724 =over 2
725  
726 =item * L<App::Greple>
727  
728 See the B<greple> manual for the detail about target text pattern.
729 Use B<--inside>, B<--outside>, B<--include>, B<--exclude> options to
730 limit the matching area.
731  
732 =item * L<App::Greple::update>
733  
734 You can use C<-Mupdate> module to modify files by the result of
735 B<greple> command.
736  
737 =item * L<App::sdif>
738  
739 Use B<sdif> to show conflict marker format side by side with B<-V>
740 option.
741  
742 =item * L<App::Greple::stripe>
743  
744 Greple B<stripe> module use by B<--xlate-stripe> option.
745  
746 =back
747  
748 =head2 RESOURCES
749  
750 =over 2
751  
752 =item * L<https://hub.docker.com/r/tecolicom/xlate>
753  
754 Docker container image.
755  
756 =item * L<https://github.com/tecolicom/getoptlong>
757  
758 The C<getoptlong.sh> library used for option parsing in the C<xlate>
759 script and L<App::dozo>.
760  
761 =item * L<https://llm.datasette.io/>
762  
763 The C<llm> command used by the B<gpt5> engine to access LLM models.
764  
765 =item * L<https://github.com/DeepLcom/deepl-python>
766  
767 DeepL Python library and CLI command.
768  
769 =item * L<https://github.com/openai/openai-python>
770  
771 OpenAI Python Library
772  
773 =item * L<https://github.com/tecolicom/App-gpty>
774  
775 OpenAI command line interface
776  
777 =back
778  
779 =head2 ARTICLES
780  
781 =over 2
782  
783 =item * L<https://qiita.com/kaz-utashiro/items/1c1a51a4591922e18250>
784  
785 Greple module to translate and replace only the necessary parts with DeepL API (in Japanese)
786  
787 =item * L<https://qiita.com/kaz-utashiro/items/a5e19736416ca183ecf6>
788  
789 Generating documents in 15 languages with DeepL API module (in Japanese)
790  
791 =item * L<https://qiita.com/kaz-utashiro/items/1b9e155d6ae0620ab4dd>
792  
793 Automatic translation Docker environment with DeepL API (in Japanese)
794  
795 =back
796  
797 =head1 AUTHOR
798  
799 Kazumasa Utashiro
800  
801 =head1 LICENSE
802  
803 Copyright © 2023-2026 Kazumasa Utashiro.
804  
805 This library is free software; you can redistribute it and/or modify
806 it under the same terms as Perl itself.
807  
808 =cut
809  
810 use v5.26;
811 use warnings;
812 use utf8;
813  
814 use Data::Dumper;
815  
816 use Text::ANSI::Fold ':constants';
817 use Command::Run;
818 use Hash::Util qw(lock_keys);
819 use Unicode::EastAsianWidth;
820 use List::Util qw(max);
821  
822 use Exporter 'import';
823 our @EXPORT_OK = qw($VERSION &opt %opt);
824 our @EXPORT_TAGS = ( all => [ qw($VERSION) ] );
825  
826 our %opt = (
827 debug => \(our $debug = 0),
828 engine => \(our $xlate_engine),
829 progress => \(our $show_progress = 1),
830 format => \(our $output_format = 'conflict'),
831 collapse => \(our $collapse_spaces = 1),
832 from => \(our $lang_from = 'ORIGINAL'),
833 to => \(our $lang_to = 'EN-US'),
834 fold => \(our $fold_line = 0),
835 width => \(our $fold_width = 70),
836 auth_key => \(our $auth_key),
837 method => \(our $cache_method //= $ENV{GREPLE_XLATE_CACHE} || 'auto'),
838 update => \(our $force_update = 0),
839 dryrun => \(our $dryrun = 0),
840 maxlen => \(our $max_length = 0),
841 maxline => \(our $max_line = 0),
842 prompt => \(our $prompt),
843 mask => \(our $mask),
844 maskfile => \(our $maskfile),
845 glossary => \(our $glossary),
846 backend => \(our $engine_backend = ''),
847 cache_seed => \(our $cache_seed),
848 context_window => \(our $context_window = 2),
849 anonymize => \(our $anonymize_file),
850 anonymize_mark => \(our $anonymize_mark),
851 template => \(our $template_option),
852 frontmatter => \(our $use_frontmatter = 0),
853 review => \(our $show_review = 0),
854 contexts => (\our @contexts),
855 );
856 lock_keys %opt;
857 66 ▶sub opt :lvalue { ${$opt{+shift}} }
Subroutine opt: called CC=1 SCAR=0.0
Pod undocumented
858  
859 our $current_file;
860 my $current_text; # whole document, set in begin()
861 my $frontmatter_len = 0; # body starts after this offset
862 our $call_context; # per-call context for context-aware engines
863 our $engine_supports_context; # engine declares $XLATE_CONTEXT
864 my $colon_count = 7;
865  
866 our %formatter = (
867 xtxt => undef,
868 none => undef,
869 conflict => sub {
870 join '',
871 "<<<<<<< $lang_from\n",
872 $_[0],
873 "=======\n",
874 $_[1],
875 ">>>>>>> $lang_to\n";
876 },
877 cm => 'conflict',
878 colon => sub {
879 my $colon = ':' x $colon_count;
880 join '',
881 "$colon $lang_from\n",
882 $_[0],
883 "$colon\n",
884 "$colon $lang_to\n",
885 $_[1],
886 "$colon\n";
887 },
888 ifdef => sub {
889 join '',
890 "#ifdef $lang_from\n",
891 $_[0],
892 "#endif\n",
893 "#ifdef $lang_to\n",
894 $_[1],
895 "#endif\n";
896 },
897 space => sub { join("\n", @_) },
898 'space+' => sub { join("\n", @_) . "\n" },
899 discard => sub { '' },
900 );
901  
902 # aliases
903 for (keys %formatter) {
904 next if ! $formatter{$_} or ref $formatter{$_};
905 $formatter{$_} = $formatter{$formatter{$_}} // die;
906 }
907  
908 my %cache;
909  
910 use App::Greple::xlate::Mask;
911 my $maskobj;
912 my $anonobj;
913  
914 sub setup {
915 66 ▶ return if state $once_called++;
Subroutine setup: called CC=18 SCAR=29.1
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
if state $once_called++ 0 56 56
916 66 ▶ if (defined $cache_method) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $App::Greple::xlate::cache_method) 56 0 56
917 66 ▶ if ($cache_method eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($App::Greple::xlate::cache_method eq "") 0 56 56
918 66 $cache_method = 'auto';
919 }
920 66 ▶ if ($cache_method =~ /^(no|never)/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($App::Greple::xlate::cache_method =~ /^(no|never)/iu) 4 52 56
921 66 $cache_method = '';
922 }
923 }
924 66 ▶ if ($xlate_engine) {
Branch: 1 branch
BranchTrueFalseTotal
if ($App::Greple::xlate::xlate_engine) 56 0 56
925 # Resolve the engine module. Backend-based engines live under a
926 # backend namespace (e.g. llm::gpt5, gpty::gpt5); others live
927 # directly under App::Greple::xlate (e.g. deepl, null). Try
928 # backend namespaces FIRST, in order of preference, so that
929 # --xlate-engine=gpt5 binds to llm::gpt5 even if a stale
930 # top-level App::Greple::xlate::gpt5 lingers in @INC from an
931 # older install. Use --xlate-setopt backend=NAME to force a
932 # specific backend (e.g. backend=gpty for comparison with the
933 # old gpty engine).
934 66 ▶ my @backend = length($engine_backend // '') ? $engine_backend : qw(llm gpty);
Branch: 1 branch
BranchTrueFalseTotal
length($App::Greple::xlate::engine_backend // '') ? : 1 55 56
Condition50%1/2
$App::Greple::xlate::engine_backend // ""
l!l
560
MC/DC0%0/1
$App::Greple::xlate::engine_backend // ""
$App::Greple::xlate::engine_backend
Truth table50%1/2
A
A= $App::Greple::xlate::engine_backend
Aresult
11
00
935 66 my $mod;
936 66 for my $cand ((map __PACKAGE__ . "::$_\::$xlate_engine", @backend),
937 __PACKAGE__ . "::$xlate_engine") {
938 66 ▶ if (eval "require $cand; 1") { $mod = $cand; last }
Branch: 1 branch
BranchTrueFalseTotal
if (eval "require $cand; 1") 54 23 77
939 # Fall through only when the candidate itself is missing;
940 # a syntax error or a missing dependency inside an existing
941 # module must be reported, not silently skipped.
942 66 (my $path = $cand) =~ s{::}{/}g;
943 66 ▶ die $@ unless $@ =~ /^Can't locate \Q$path.pm\E /;
Branch: 1 branch
BranchTrueFalseTotal
unless $@ =~ /^Can't locate \Q$path.pm\E /u 1 22 23
944 }
945 66 ▶ $mod or die "Engine $xlate_engine is not available.\n";
Branch: 1 branch
BranchTrueFalseTotal
$mod or die "Engine $App::Greple::xlate::xlate_engine is not available.\n" 54 1 55
946 66 $mod->import;
947 no strict 'refs';
948 66 ${"$mod\::lang_from"} = $lang_from;
949 66 ${"$mod\::lang_to"} = $lang_to;
950 66 *XLATE = \&{"$mod\::xlate"};
951 66 $engine_supports_context = ${"$mod\::XLATE_CONTEXT"};
952 66 ▶ if (not defined &XLATE) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined &App::Greple::xlate::XLATE) 0 54 54
953 66 die "No \"xlate\" function in $mod.\n";
954 }
955 }
956 66 ▶ if (my $pat = opt('mask')) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $pat = opt("mask")) 0 54 54
957 66 $maskobj = App::Greple::xlate::Mask->new(pattern => $pat);
958 }
959 66 ▶ if (my $patfile = opt('maskfile')) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $patfile = opt("maskfile")) 1 53 54
960 66 $maskobj = App::Greple::xlate::Mask->new(file => $patfile);
961 }
962 66 ▶ if (defined $anonymize_file or defined $anonymize_mark) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $App::Greple::xlate::anonymize_file or defined $App::Greple::xlate::anonymize_mark) 9 45 54
Condition100%3/3
defined $App::Greple::xlate::anonymize_file or defined $App::Greple::xlate::anonymize_mark
l!l&&r!l&&!r
8145
MC/DC100%2/2
defined $App::Greple::xlate::anonymize_file or defined $App::Greple::xlate::anonymize_mark
defined $App::Greple::xlate::anonymize_file defined $App::Greple::xlate::anonymize_mark
Truth table100%3/3
A or B
A= defined $App::Greple::xlate::anonymize_fileB= defined $App::Greple::xlate::anonymize_mark
ABresult
1X1
011
000
963 66 $anonobj = App::Greple::xlate::Mask->new(STABLE => 1);
964 66 $anonobj->add_escape_rule;
965 66 ▶ $anonobj->load_anonymize_file($anonymize_file)
Branch: 1 branch
BranchTrueFalseTotal
if defined $App::Greple::xlate::anonymize_file 8 1 9
966 if defined $anonymize_file;
967 }
968 }
969  
970 use App::Greple::xlate::Text;
971  
972 sub postgrep {
973 66 ▶ my $grep = shift;
Subroutine postgrep: called CC=16 SCAR=28.0
Pod undocumented
974 441 my @blocks;
975 my %pending;
976 441 for my $r ($grep->result) {
977 27 my($b, @match) = @$r;
978 0 for my $m (@match) {
979 0 my($s, $e, $i) = @$m;
980 2 my $key = App::Greple::xlate::Text
981 ->new($grep->cut(@$m), paragraph => ($i % 2 == 0))
982 ->normalized;
983 0 ▶ my $hit = !$pending{$key} && exists $cache{$key};
Condition100%3/3
!$pending{$key} && exists $cache{$key}
!ll&&!rl&&r
211231
MC/DC100%2/2
!$pending{$key} && exists $cache{$key}
!$pending{$key} exists $cache{$key}
Truth table100%3/3
not(A) && B
A= !$pending{$key}B= exists $cache{$key}
ABresult
0X0
100
111
984 0 ▶ if (not $hit and not $pending{$key}++) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($hit or $pending{$key}++) 112 33 145
Condition100%3/3
$hit or $pending{$key}++
l!l&&r!l&&!r
312112
MC/DC100%2/2
$hit or $pending{$key}++
$hit $pending{$key}++
Truth table100%3/3
A or B
A= $hitB= $pending{$key}++
ABresult
1X1
011
000
985 0 $cache{$key} = undef;
986 }
987 56 push @blocks, { key => $key, s => $s, e => $e, hit => $hit };
988 }
989 }
990 56 my @regions;
991 56 my $i = 0;
992 0 ▶ while ($i < @blocks) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @blocks) 82 49 131
993 56 ▶ if ($blocks[$i]{hit}) { $i++; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($blocks[$i]{"hit"}) 31 51 82
994 56 my $j = $i;
995 56 ▶ $j++ while $j < @blocks and not $blocks[$j]{hit};
Branch: 1 branch
BranchTrueFalseTotal
$j < @blocks and not $blocks[$j]{"hit"} and do { ++$j } 114 51 165
Condition100%3/3
$j < @blocks and not $blocks[$j]{"hit"}
!ll&&!rl&&r
3912114
MC/DC100%2/2
$j < @blocks and not $blocks[$j]{"hit"}
$j < @blocks not $blocks[$j]{"hit"}
Truth table100%3/3
A and not(B)
A= $j < @blocksB= not $blocks[$j]{"hit"}
ABresult
0X0
100
111
996 56 push @regions, [ $i, $j - 1 ];
997 77 $i = $j;
998 }
999 54 ▶ return if not @regions;
Branch: 1 branch
BranchTrueFalseTotal
unless @regions 0 49 49
1000 my $with_context = $engine_supports_context
1001 && $context_window > 0
1002 54 ▶ && grep { $_->{hit} } @blocks;
Condition100%6/6
$App::Greple::xlate::engine_supports_context && $App::Greple::xlate::context_window > 0 && grep({$_->{'hit'};} @blocks)
!ll&&!rl&&r
92911
$App::Greple::xlate::engine_supports_context and $App::Greple::xlate::context_window > 0
!ll&&!rl&&r
8140
MC/DC100%3/3
$App::Greple::xlate::engine_supports_context && $App::Greple::xlate::context_window > 0 && grep({$_->{'hit'};} @blocks)
$App::Greple::xlate::engine_supports_context $App::Greple::xlate::context_window > 0 grep({$_->{'hit'};} @blocks)
Truth table100%4/4
A and B && C
A= $App::Greple::xlate::engine_supports_contextB= $App::Greple::xlate::context_window > 0C= grep({$_->{'hit'};} @blocks)
ABCresult
0XX0
10X0
1100
1111
1003 23 ▶ if ($with_context) {
Branch: 1 branch
BranchTrueFalseTotal
if ($with_context) { } 11 38 49
1004 55 my %queued;
1005 54 for my $region (@regions) {
1006 54 my @texts = grep { not $queued{$_}++ }
1007 54 map $blocks[$_]{key}, $region->[0] .. $region->[1];
1008 54 ▶ next unless @texts;
Branch: 1 branch
BranchTrueFalseTotal
unless @texts 0 13 13
1009 54 cache_update({
1010 texts => \@texts,
1011 context => region_context(\@blocks, @$region),
1012 });
1013 }
1014 } else {
1015 54 my %seen;
1016 54 my @texts = grep { not $seen{$_}++ }
1017 map $blocks[$_]{key},
1018 54 map { $_->[0] .. $_->[1] } @regions;
1019 54 cache_update({ texts => \@texts, context => undef });
1020 }
1021 }
1022  
1023 our $CONTEXT_SOURCE_MAX = 2000; # per-side raw source slice limit
1024  
1025 our $TEMPLATE_DEFAULT = q[\{\{.*?\}\}|\{%.*?%\}|\{#.*?#\}];
1026  
1027 sub template_regex {
1028 0 ▶ return undef unless defined $template_option;
Subroutine template_regex: called CC=3 SCAR=12.0
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
unless defined $App::Greple::xlate::template_option 89 12 101
1029 54 ▶ length($template_option) ? $template_option : $TEMPLATE_DEFAULT;
Branch: 1 branch
BranchTrueFalseTotal
length $App::Greple::xlate::template_option ? : 0 12 12
1030 }
1031  
1032 ##
1033 ## Build the per-region context (surrounding source slices, neighbor
1034 ## pairs, previous-version pairs).
1035 ##
1036 sub region_context {
1037 0 ▶ my($blocks, $from, $to) = @_;
Subroutine region_context: called CC=18 SCAR=29.2
Pod undocumented
1038 54 my(@before, @after);
1039 1 ▶ for (my $k = $from - 1; $k >= 0 and @before < $context_window; $k--) {
Branch: 1 branch
BranchTrueFalseTotal
if ($k >= 0 and @before < $App::Greple::xlate::context_window) 15 13 28
Condition100%3/3
$k >= 0 and @before < $App::Greple::xlate::context_window
!ll&&!rl&&r
12115
MC/DC100%2/2
$k >= 0 and @before < $App::Greple::xlate::context_window
$k >= 0 @before < $App::Greple::xlate::context_window
Truth table100%3/3
A and B
A= $k >= 0B= @before < $App::Greple::xlate::context_window
ABresult
0X0
100
111
1040 54 ▶ next unless $blocks->[$k]{hit};
Branch: 1 branch
BranchTrueFalseTotal
unless $blocks->[$k]{"hit"} 1 14 15
1041 9 my $v = $cache{$blocks->[$k]{key}};
1042 9 ▶ push @before, [ $blocks->[$k]{key}, $v ] if defined $v;
Branch: 1 branch
BranchTrueFalseTotal
if defined $v 14 0 14
1043 }
1044 9 ▶ for (my $k = $to + 1; $k < @$blocks and @after < $context_window; $k++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($k < @$blocks and @after < $App::Greple::xlate::context_window) 18 13 31
Condition100%3/3
$k < @$blocks and @after < $App::Greple::xlate::context_window
!ll&&!rl&&r
12118
MC/DC100%2/2
$k < @$blocks and @after < $App::Greple::xlate::context_window
$k < @$blocks @after < $App::Greple::xlate::context_window
Truth table100%3/3
A and B
A= $k < @$blocksB= @after < $App::Greple::xlate::context_window
ABresult
0X0
100
111
1045 49 ▶ next unless $blocks->[$k]{hit};
Branch: 1 branch
BranchTrueFalseTotal
unless $blocks->[$k]{"hit"} 1 17 18
1046 49 my $v = $cache{$blocks->[$k]{key}};
1047 49 ▶ push @after, [ $blocks->[$k]{key}, $v ] if defined $v;
Branch: 1 branch
BranchTrueFalseTotal
if defined $v 17 0 17
1048 }
1049 72 my @old;
1050 72 ▶ if (my $tied = tied %cache) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $tied = tied %cache) 13 0 13
1051 145 my %is_hit = map { $_->{key} => 1 } grep { $_->{hit} } @$blocks;
1052 145 my($lo, $hi);
1053 112 ▶ for (my $k = $from - 1; $k >= 0; $k--) {
Branch: 1 branch
BranchTrueFalseTotal
if ($k >= 0) 11 2 13
1054 145 ▶ next unless $blocks->[$k]{hit};
Branch: 1 branch
BranchTrueFalseTotal
unless $blocks->[$k]{"hit"} 0 11 11
1055 49 my $pos = $tied->old_position($blocks->[$k]{key});
1056 49 ▶ if (defined $pos) { $lo = $pos + 1; last }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $pos) 11 0 11
1057 }
1058 31 ▶ for (my $k = $to + 1; $k < @$blocks; $k++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($k < @$blocks) 11 2 13
1059 31 ▶ next unless $blocks->[$k]{hit};
Branch: 1 branch
BranchTrueFalseTotal
unless $blocks->[$k]{"hit"} 0 11 11
1060 51 my $pos = $tied->old_position($blocks->[$k]{key});
1061 51 ▶ if (defined $pos) { $hi = $pos - 1; last }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $pos) 11 0 11
1062 }
1063 49 ▶ $lo //= 0;
Condition100%2/2
$lo //= 0
l!l
112
MC/DC100%1/1
$lo //= 0
$lo
Truth table100%2/2
A
A= $lo
Aresult
11
00
1064 49 ▶ $hi //= $tied->old_size - 1;
Condition100%2/2
$hi //= $tied->old_size - 1
l!l
112
MC/DC100%1/1
$hi //= $tied->old_size - 1
$hi
Truth table100%2/2
A
A= $hi
Aresult
11
00
1065 111 @old = grep { not $is_hit{$_->[0]} }
1066 $tied->old_entries_slice($lo, $hi);
1067 }
1068 return {
1069 new_texts => [ map $blocks->[$_]{key}, $from .. $to ],
1070 source_before => source_slice_before($blocks->[$from]{s}),
1071 11 source_after => source_slice_after($blocks->[$to]{e}),
1072 hits_before => \@before,
1073 hits_after => \@after,
1074 old_pairs => \@old,
1075 };
1076 }
1077  
1078 sub source_slice_before {
1079 11 ▶ my $end = shift;
Subroutine source_slice_before: called CC=5 SCAR=16.4
Pod undocumented
1080 13 ▶ return '' unless defined $current_text and $end > $frontmatter_len;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $current_text and $end > $frontmatter_len 1 12 13
Condition66%2/3
defined $current_text and $end > $frontmatter_len
!ll&&!rl&&r
0112
MC/DC50%1/2
defined $current_text and $end > $frontmatter_len
defined $current_text $end > $frontmatter_len
Truth table66%2/3
A and B
A= defined $current_textB= $end > $frontmatter_len
ABresult
0X0
100
111
1081 14 my $start = $end - $CONTEXT_SOURCE_MAX;
1082 13 ▶ $start = $frontmatter_len if $start < $frontmatter_len;
Branch: 1 branch
BranchTrueFalseTotal
if $start < $frontmatter_len 12 0 12
1083 13 my $s = substr($current_text, $start, $end - $start);
1084 38 ▶ $s =~ s/\A[^\n]*\n// if $start > $frontmatter_len; # round up to a line start
Branch: 1 branch
BranchTrueFalseTotal
if $start > $frontmatter_len 0 12 12
1085 38 $s;
1086 }
1087  
1088 sub source_slice_after {
1089 100 ▶ my $start = shift;
Subroutine source_slice_after: called CC=4 SCAR=14.0
Pod undocumented
1090 38 ▶ return '' unless defined $current_text
Branch: 1 branch
BranchTrueFalseTotal
unless defined $current_text and $start < length $current_text 2 11 13
Condition66%2/3
defined $current_text and $start < length $current_text
!ll&&!rl&&r
0211
MC/DC50%1/2
defined $current_text and $start < length $current_text
defined $current_text $start < length $current_text
Truth table66%2/3
A and B
A= defined $current_textB= $start < length $current_text
ABresult
0X0
100
111
1091 and $start < length($current_text);
1092 38 my $s = substr($current_text, $start, $CONTEXT_SOURCE_MAX);
1093 101 ▶ if ($start + $CONTEXT_SOURCE_MAX < length($current_text)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($start + $App::Greple::xlate::CONTEXT_SOURCE_MAX < length $current_text) 0 11 11
1094 12 $s =~ s/(?<=\n)[^\n]*\z//; # drop trailing partial line
1095 }
1096 13 $s;
1097 }
1098  
1099 sub _progress {
1100 13 ▶ my $opt = ref $_[0] ? shift : {};
Subroutine _progress: called CC=7 SCAR=20.1
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 109 0 109
1101 13 ▶ opt('progress') or return;
Branch: 1 branch
BranchTrueFalseTotal
opt("progress") or return 109 0 109
1102 15 ▶ if (my $label = $opt->{label}) { print STDERR "[xlate.pm] $label:\n" }
Branch: 1 branch
BranchTrueFalseTotal
if (my $label = $opt->{"label"}) 109 0 109
1103 14 for (my @s = @_) {
1104 13 my $i =()= /^/mg;
1105 18 ▶ my @m = ($i == 1 ? '╶' : '│') x $i ;
Branch: 1 branch
BranchTrueFalseTotal
$i == 1 ? : 230 0 230
1106 17 ▶ @m[0,-1] = qw(┌ └) if $i > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $i > 1 0 230 230
1107 17 s/^/sprintf "%7s ", shift(@m)/mge;
1108 13 s/(?<!\n)\z/\n/;
1109 13 s/( +)$/"␣" x length($1)/mge;
1110 49 print STDERR $_;
1111 }
1112 }
1113  
1114 sub clone_context {
1115 13 ▶ my $ctx = shift;
Subroutine clone_context: called CC=5 SCAR=17.2
Pod undocumented
1116 return {
1117 11 ▶ new_texts => [ @{$ctx->{new_texts} // []} ],
Condition50%1/2
$ctx->{"new_texts"} // []
l!l
20
MC/DC0%0/1
$ctx->{"new_texts"} // []
$ctx->{"new_texts"}
Truth table50%1/2
A
A= $ctx->{"new_texts"}
Aresult
11
00
1118 source_before => $ctx->{source_before},
1119 source_after => $ctx->{source_after},
1120 11 ▶ hits_before => [ map [ @$_ ], @{$ctx->{hits_before} // []} ],
Condition50%1/2
$ctx->{"hits_before"} // []
l!l
20
MC/DC0%0/1
$ctx->{"hits_before"} // []
$ctx->{"hits_before"}
Truth table50%1/2
A
A= $ctx->{"hits_before"}
Aresult
11
00
1121 11 ▶ hits_after => [ map [ @$_ ], @{$ctx->{hits_after} // []} ],
Condition50%1/2
$ctx->{"hits_after"} // []
l!l
20
MC/DC0%0/1
$ctx->{"hits_after"} // []
$ctx->{"hits_after"}
Truth table50%1/2
A
A= $ctx->{"hits_after"}
Aresult
11
00
1122 13 ▶ old_pairs => [ map [ @$_ ], @{$ctx->{old_pairs} // []} ],
Condition50%1/2
$ctx->{"old_pairs"} // []
l!l
20
MC/DC0%0/1
$ctx->{"old_pairs"} // []
$ctx->{"old_pairs"}
Truth table50%1/2
A
A= $ctx->{"old_pairs"}
Aresult
11
00
1123 };
1124 }
1125  
1126 sub change_span {
1127 11 ▶ my($old, $new) = @_;
Subroutine change_span: called CC=9 SCAR=22.9
Pod undocumented
1128 13 my @old = split //, $old;
1129 11 my @new = split //, $new;
1130 11 my $prefix = 0;
1131 11 ▶ $prefix++ while $prefix < @old && $prefix < @new
Branch: 1 branch
BranchTrueFalseTotal
$prefix < @old and $prefix < @new and $old[$prefix] eq $new[$prefix] and do { ++$prefix } 34 2 36
Condition50%3/6
$prefix < @old and $prefix < @new and $old[$prefix] eq $new[$prefix]
!ll&&!rl&&r
0234
$prefix < @old and $prefix < @new
!ll&&!rl&&r
0036
MC/DC33%1/3
$prefix < @old and $prefix < @new and $old[$prefix] eq $new[$prefix]
$prefix < @old $prefix < @new $old[$prefix] eq $new[$prefix]
Truth table50%2/4
A and B and C
A= $prefix < @oldB= $prefix < @newC= $old[$prefix] eq $new[$prefix]
ABCresult
0XX0
10X0
1100
1111
1132 && $old[$prefix] eq $new[$prefix];
1133 11 my $suffix = 0;
1134 11 ▶ $suffix++ while $suffix < @old - $prefix
Branch: 1 branch
BranchTrueFalseTotal
$suffix < @old - $prefix and $suffix < @new - $prefix and $old[-1 - $suffix] eq $new[-1 - $suffix] and do { ++$suffix } 22 2 24
Condition66%4/6
$suffix < @old - $prefix and $suffix < @new - $prefix and $old[-1 - $suffix] eq $new[-1 - $suffix]
!ll&&!rl&&r
2022
$suffix < @old - $prefix and $suffix < @new - $prefix
!ll&&!rl&&r
2022
MC/DC33%1/3
$suffix < @old - $prefix and $suffix < @new - $prefix and $old[-1 - $suffix] eq $new[-1 - $suffix]
$suffix < @old - $prefix $suffix < @new - $prefix $old[-1 - $suffix] eq $new[-1 - $suffix]
Truth table50%2/4
A and B and C
A= $suffix < @old - $prefixB= $suffix < @new - $prefixC= $old[-1 - $suffix] eq $new[-1 - $suffix]
ABCresult
0XX0
10X0
1100
1111
1135 && $suffix < @new - $prefix
1136 && $old[-1 - $suffix] eq $new[-1 - $suffix];
1137 13 my $old_len = @old - $prefix - $suffix;
1138 13 my $new_len = @new - $prefix - $suffix;
1139 13 ▶ my $old_change = $old_len
Branch: 1 branch
BranchTrueFalseTotal
$old_len ? : 0 2 2
1140 ? join('', @old[$prefix .. $prefix + $old_len - 1]) : '';
1141 14 ▶ my $new_change = $new_len
Branch: 1 branch
BranchTrueFalseTotal
$new_len ? : 2 0 2
1142 ? join('', @new[$prefix .. $prefix + $new_len - 1]) : '';
1143 13 ($prefix, $old_change, $new_change);
1144 }
1145  
1146 sub review_quote {
1147 13 ▶ local $_ = shift // '';
Subroutine review_quote: called CC=2 SCAR=7.0
Pod undocumented
Condition50%1/2
shift() // ""
l!l
40
MC/DC0%0/1
shift() // ""
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
1148 13 s/\\/\\\\/g;
1149 12 s/\n/\\n/g;
1150 12 s/\t/\\t/g;
1151 12 s/"/\\"/g;
1152 12 qq{"$_"};
1153 }
1154  
1155 sub review_change {
1156 12 ▶ my($old_source, $new_source, $old_translation, $new_translation) = @_;
Subroutine review_change: called CC=2 SCAR=6.9
Pod undocumented
1157 13 print STDERR "[xlate.pm] Review:\n";
1158 13 for my $change (
1159 [ source => change_span($old_source, $new_source) ],
1160 [ target => change_span($old_translation, $new_translation) ],
1161 ) {
1162 11 my($label, $offset, $old, $new) = @$change;
1163 11 printf STDERR " %s \@%d: %s -> %s\n",
1164 $label, $offset, review_quote($old), review_quote($new);
1165 }
1166 }
1167  
1168 sub cache_update {
1169 0 ▶ binmode STDERR, ':encoding(utf8)';
Subroutine cache_update: called CC=32 SCAR=35.4
Pod undocumented
1170  
1171 11 ▶ my $region = ref $_[0] eq 'HASH' ? shift : { texts => [ @_ ] };
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'HASH' ? : 51 0 51
1172 109 my @from = @{$region->{texts}};
1173 109 my @pristine = @from;
1174 109 my $context = $region->{context};
1175  
1176 109 ▶ if ($context) {
Branch: 1 branch
BranchTrueFalseTotal
if ($context) 13 38 51
1177 230 ▶ my $refs = @{$context->{hits_before} // []}
Condition50%1/2
$context->{"hits_before"} // []
l!l
130
MC/DC0%0/1
$context->{"hits_before"} // []
$context->{"hits_before"}
Truth table50%1/2
A
A= $context->{"hits_before"}
Aresult
11
00
1178 230 ▶ + @{$context->{hits_after} // []};
Condition50%1/2
$context->{"hits_after"} // []
l!l
130
MC/DC0%0/1
$context->{"hits_after"} // []
$context->{"hits_after"}
Truth table50%1/2
A
A= $context->{"hits_after"}
Aresult
11
00
1179 230 ▶ my $olds = @{$context->{old_pairs} // []};
Condition50%1/2
$context->{"old_pairs"} // []
l!l
130
MC/DC0%0/1
$context->{"old_pairs"} // []
$context->{"old_pairs"}
Truth table50%1/2
A
A= $context->{"old_pairs"}
Aresult
11
00
1180 230 _progress({label => "Context"},
1181 sprintf("%d reference pair(s), %d previous pair(s)",
1182 $refs, $olds));
1183 }
1184 230 ▶ if ($dryrun) {
Branch: 1 branch
BranchTrueFalseTotal
if ($App::Greple::xlate::dryrun) 4 47 51
1185 0 my @preview = @from;
1186 230 ▶ if ($anonobj) {
Branch: 1 branch
BranchTrueFalseTotal
if ($anonobj) 2 2 4
1187 2 $anonobj->mask(@preview);
1188 2 $anonobj->reset;
1189 }
1190 2 ▶ if ($maskobj) {
Branch: 1 branch
BranchTrueFalseTotal
if ($maskobj) 0 4 4
1191 2 $maskobj->mask(@preview);
1192 2 $maskobj->reset;
1193 }
1194 2 _progress({label => "From"}, @preview);
1195 # Populate cache with original text so callback can fetch it
1196 2 @cache{@{$region->{texts}}} = @from;
1197 2 return @from;
1198 }
1199 2 my @result = eval {
1200 2 my $masked_context = $context;
1201 2 ▶ if ($anonobj) {
Branch: 1 branch
BranchTrueFalseTotal
if ($anonobj) 10 37 47
1202 2 $anonobj->mask(@from);
1203 2 ▶ if ($context) {
Branch: 1 branch
BranchTrueFalseTotal
if ($context) 2 8 10
1204 2 $masked_context = clone_context($context);
1205 2 $anonobj->mask_reference(@{$masked_context->{new_texts}});
1206 $anonobj->mask_reference(
1207 $masked_context->{source_before},
1208 2 $masked_context->{source_after});
1209 4 for my $pairs (@{$masked_context}{qw(hits_before hits_after old_pairs)}) {
1210 4 $anonobj->mask_reference(@$_) for @$pairs;
1211 }
1212 }
1213 }
1214 4 ▶ $maskobj->mask(@from) if $maskobj;
Branch: 1 branch
BranchTrueFalseTotal
if $maskobj 1 46 47
1215 # Show the payload as it will be transmitted, consistent with
1216 # the dryrun preview and the --xlate-mask display.
1217 4 _progress({label => "From"}, @from);
1218 4 ▶ warn Dumper $masked_context if $context and opt('debug');
Branch: 1 branch
BranchTrueFalseTotal
if $context and opt("debug") 0 47 47
Condition66%2/3
$context and opt("debug")
!ll&&!rl&&r
35120
MC/DC0%0/2
$context and opt("debug")
$context opt("debug")
Truth table66%2/3
A and B
A= $contextB= opt("debug")
ABresult
0X0
100
111
1219 1 my @chop = grep { $from[$_] =~ s/(?<!\n)\z/\n/ } keys @from;
1220 1 my @to = do {
1221 2 local $call_context = $masked_context;
1222 2 map { s/ +$//mgr } &XLATE(@from);
1223 };
1224 51 chop @to[@chop];
1225 51 ▶ $maskobj->unmask(@to)->reset if $maskobj;
Branch: 1 branch
BranchTrueFalseTotal
if $maskobj 1 44 45
1226 51 ▶ if ($anonobj) {
Branch: 1 branch
BranchTrueFalseTotal
if ($anonobj) 10 35 45
1227 51 $anonobj->unmask(@to);
1228 51 $anonobj->reset;
1229 }
1230  
1231 51 _progress({label => "To"}, @to);
1232 13 ▶ if ($show_review && $context
Branch: 1 branch
BranchTrueFalseTotal
if ($App::Greple::xlate::show_review and $context and @pristine == 1 and @{$context->{"old_pairs"} // [];} == 1) 1 44 45
Condition66%6/9
$App::Greple::xlate::show_review and $context and @pristine == 1 and @{$context->{"old_pairs"} // [];} == 1
!ll&&!rl&&r
4401
$App::Greple::xlate::show_review and $context and @pristine == 1
!ll&&!rl&&r
4401
$App::Greple::xlate::show_review and $context
!ll&&!rl&&r
4401
MC/DC25%1/4
$App::Greple::xlate::show_review and $context and @pristine == 1 and @{$context->{"old_pairs"} // [];} == 1
$App::Greple::xlate::show_review $context @pristine == 1 @{$context->{"old_pairs"} // [];} == 1
Truth table40%2/5
A and B and C and D
A= $App::Greple::xlate::show_reviewB= $contextC= @pristine == 1D= @{$context->{"old_pairs"} // [];} == 1
ABCDresult
0XXX0
10XX0
110X0
11100
11111
1233 && @pristine == 1
1234 13 ▶ && @{$context->{old_pairs} // []} == 1) {
Condition50%1/2
$context->{"old_pairs"} // []
l!l
10
MC/DC0%0/1
$context->{"old_pairs"} // []
$context->{"old_pairs"}
Truth table50%1/2
A
A= $context->{"old_pairs"}
Aresult
11
00
1235 13 my($old_source, $old_translation) = @{$context->{old_pairs}[0]};
1236 13 review_change($old_source, $pristine[0],
1237 $old_translation, $to[0]);
1238 }
1239 13 ▶ if (defined(my $tre = template_regex())) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $tre = template_regex())) 6 39 45
1240 51 for my $i (0 .. $#pristine) {
1241 # Translation may legitimately reorder expressions to
1242 # follow the target language word order; require the
1243 # same multiset, not the same sequence.
1244 4 my @want = sort $pristine[$i] =~ /($tre)/g;
1245 4 ▶ my @got = sort +($to[$i] // '') =~ /($tre)/g;
Condition50%1/2
$to[$i] // ""
l!l
70
MC/DC0%0/1
$to[$i] // ""
$to[$i]
Truth table50%1/2
A
A= $to[$i]
Aresult
11
00
1246 2 ▶ if (@want != @got
Branch: 1 branch
BranchTrueFalseTotal
if (@want != @got or grep {$want[$_] ne $got[$_];} 0 .. $#want) 2 5 7
Condition66%2/3
@want != @got or grep {$want[$_] ne $got[$_];} 0 .. $#want
l!l&&r!l&&!r
025
MC/DC50%1/2
@want != @got or grep {$want[$_] ne $got[$_];} 0 .. $#want
@want != @got grep {$want[$_] ne $got[$_];} 0 .. $#want
Truth table66%2/3
A or B
A= @want != @gotB= grep {$want[$_] ne $got[$_];} 0 .. $#want
ABresult
1X1
011
000
1247 2 or grep { $want[$_] ne $got[$_] } 0 .. $#want) {
1248 4 die sprintf("Template expressions broken in response:\n" .
1249 " expected: %s\n got: %s\n",
1250 join(' ', @want), join(' ', @got));
1251 }
1252 }
1253 }
1254 0 ▶ die "Unmatched response:\n@to" if @from != @to;
Branch: 1 branch
BranchTrueFalseTotal
if @from != @to 0 43 43
1255 0 @cache{@{$region->{texts}}} = @to;
1256 4 ▶ if (my $obj = tied %cache) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $obj = tied %cache) 32 11 43
1257 4 $obj->checkpoint;
1258 }
1259 4 @to;
1260 };
1261 47 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 4 43 47
1262 # Preserve everything already paid for (including old pairs)
1263 # and freeze the cache so global destruction cannot purge it.
1264 47 ▶ if (my $obj = tied %cache) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $obj = tied %cache) 3 1 4
1265 47 $obj->checkpoint;
1266 10 $obj->readonly = 1;
1267 }
1268 10 die $@;
1269 }
1270 2 @result;
1271 }
1272  
1273 sub fold_lines {
1274 2 ▶ state $fold = Text::ANSI::Fold->new(
Subroutine fold_lines: called CC=1 SCAR=0.0
Pod undocumented
1275 width => $fold_width,
1276 boundary => 'word',
1277 linebreak => LINEBREAK_ALL,
1278 runin => 4,
1279 runout => 4,
1280 );
1281 2 local $_ = shift;
1282 2 s/(.+)/join "\n", $fold->text($1)->chops/ge;
1283 2 $_;
1284 }
1285  
1286 sub xlate {
1287 6 ▶ my $param = { @_ };
Subroutine xlate: called CC=4 SCAR=14.0
Pod documented
1288 47 my($index, $text) = $param->@{qw(index match)};
1289 47 my $obj = App::Greple::xlate::Text->new($text,
1290 paragraph => ($index % 2 == 0));
1291 47 ▶ my $s = $cache{$obj->normalized} // "!!! TRANSLATION ERROR !!!\n";
Condition50%1/2
$cache{$obj->normalized} // "!!! TRANSLATION ERROR !!! "
l!l
1340
MC/DC0%0/1
$cache{$obj->normalized} // "!!! TRANSLATION ERROR !!! "
$cache{$obj->normalized}
Truth table50%1/2
A
A= $cache{$obj->normalized}
Aresult
11
00
1292 47 $obj->unstrip($s);
1293 108 ▶ $s = fold_lines $s if $fold_line;
Branch: 1 branch
BranchTrueFalseTotal
if $App::Greple::xlate::fold_line 0 134 134
1294 47 ▶ if (state $formatter = $formatter{$output_format}) {
Branch: 1 branch
BranchTrueFalseTotal
if (state $formatter = $App::Greple::xlate::formatter{$App::Greple::xlate::output_format}) { } 29 105 134
1295 47 return $formatter->($text, $s);
1296 } else {
1297 47 return $s;
1298 }
1299 }
1300 105 ▶sub callback { goto &xlate }
Subroutine callback: not called CC=1 SCAR=0.0
Pod undocumented
1301  
1302 sub mask_string {
1303 45 ▶ my($s) = +{ @_ }->{match};
Subroutine mask_string: called CC=3 SCAR=12.7
Pod undocumented
1304 45 ▶ if ($anonobj) {
Branch: 1 branch
BranchTrueFalseTotal
if ($anonobj) 0 0 0
1305 45 $anonobj->mask($s);
1306 }
1307 10 ▶ if ($maskobj) {
Branch: 1 branch
BranchTrueFalseTotal
if ($maskobj) 0 0 0
1308 10 $maskobj->mask($s);
1309 }
1310 45 $s;
1311 }
1312  
1313 sub cache_file {
1314 45 ▶ my $file = sprintf("%s.xlate-%s-%s.json",
Subroutine cache_file: not called CC=6 SCAR=17.9
Pod undocumented
1315 $current_file, $xlate_engine, $lang_to);
1316 1 ▶ if ($cache_method eq 'auto') {
Branch: 1 branch
BranchTrueFalseTotal
if ($App::Greple::xlate::cache_method eq 'auto') { } 42 9 51
1317 # Seeding targets a document whose cache does not exist yet,
1318 # so a seed implies cache creation even in auto mode.
1319 1 ▶ (-f $file or defined $cache_seed) ? $file : undef;
Branch: 1 branch
BranchTrueFalseTotal
-f $file || defined $App::Greple::xlate::cache_seed ? : 36 6 42
Condition100%3/3
-f $file or defined $App::Greple::xlate::cache_seed
l!l&&r!l&&!r
3516
MC/DC100%2/2
-f $file or defined $App::Greple::xlate::cache_seed
-f $file defined $App::Greple::xlate::cache_seed
Truth table100%3/3
A or B
A= -f $fileB= defined $App::Greple::xlate::cache_seed
ABresult
1X1
011
000
1320 } else {
1321 1 ▶ if ($cache_method and -f $current_file) {
Branch: 1 branch
BranchTrueFalseTotal
if ($App::Greple::xlate::cache_method and -f $App::Greple::xlate::current_file) { } 2 7 9
Condition100%3/3
$App::Greple::xlate::cache_method and -f $App::Greple::xlate::current_file
!ll&&!rl&&r
432
MC/DC100%2/2
$App::Greple::xlate::cache_method and -f $App::Greple::xlate::current_file
$App::Greple::xlate::cache_method -f $App::Greple::xlate::current_file
Truth table100%3/3
A and B
A= $App::Greple::xlate::cache_methodB= -f $App::Greple::xlate::current_file
ABresult
0X0
100
111
1322 1 $file;
1323 } else {
1324 45 undef;
1325 }
1326 }
1327 }
1328  
1329 sub begin {
1330 6 ▶ setup if not (state $done++);
Subroutine begin: not called CC=25 SCAR=36.4
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
unless state $done++ 49 0 49
1331 7 my %args = @_;
1332 7 ▶ $current_file = delete $args{&::FILELABEL} or die;
Branch: 1 branch
BranchTrueFalseTotal
$App::Greple::xlate::current_file = delete $args{&main::FILELABEL} or die 49 0 49
1333 7 ▶ s/\z/\n/ if /.\z/;
Branch: 1 branch
BranchTrueFalseTotal
if /.\z/u 0 49 49
1334 7 $current_text = $_;
1335 2 $frontmatter_len = 0;
1336 43 ▶ if ($use_frontmatter
Branch: 1 branch
BranchTrueFalseTotal
if ($App::Greple::xlate::use_frontmatter and $current_text =~ /\A(---\n(?s:.*?)^---\n)/mu) 3 46 49
Condition66%2/3
$App::Greple::xlate::use_frontmatter and $current_text =~ /\A(---\n(?s:.*?)^---\n)/mu
!ll&&!rl&&r
4603
MC/DC50%1/2
$App::Greple::xlate::use_frontmatter and $current_text =~ /\A(---\n(?s:.*?)^---\n)/mu
$App::Greple::xlate::use_frontmatter $current_text =~ /\A(---\n(?s:.*?)^---\n)/mu
Truth table66%2/3
A and B
A= $App::Greple::xlate::use_frontmatterB= $current_text =~ /\A(---\n(?s:.*?)^---\n)/mu
ABresult
0X0
100
111
1337 and $current_text =~ /\A(---\n(?s:.*?)^---\n)/m) {
1338 43 my $fm = $1;
1339 43 $frontmatter_len = length $fm;
1340 # A paragraph-style match pattern joins the front matter with
1341 # the first body paragraph unless a blank line separates them,
1342 # and a straddling match defeats the --exclude region.
1343 43 ▶ if (substr($current_text, $frontmatter_len, 1) ne "\n") {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($current_text, $frontmatter_len, 1) ne " ") 3 0 3
1344 32 warn "$current_file: no blank line after front matter; " .
1345 "it may be caught by paragraph matching.\n";
1346 }
1347 43 my @values;
1348 47 for my $line (split /\n/, $fm) {
1349 4 ▶ next if $line =~ /^---/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^---/u 6 7 13
1350 3 ▶ my($k, $v) = $line =~ /^([^\s:#][^:]*):\s*(.+?)\s*$/ or next;
Branch: 1 branch
BranchTrueFalseTotal
() = $line =~ /^([^\s:#][^:]*):\s*(.+?)\s*$/u or next 7 0 7
1351 3 $v =~ s/\A(["'])(.*)\1\z/$2/s; # strip surrounding quotes
1352 4 push @values, $v;
1353 }
1354 43 ▶ if (@values) {
Branch: 1 branch
BranchTrueFalseTotal
if (@values) 3 0 3
1355 0 ▶ if (not $anonobj) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($anonobj) 3 0 3
1356 0 $anonobj = App::Greple::xlate::Mask->new(STABLE => 1);
1357 0 $anonobj->add_escape_rule;
1358 }
1359 0 $anonobj->add_rule(var => quotemeta($_)) for @values;
1360 }
1361 }
1362 0 ▶ if ($anonobj and defined $anonymize_mark) {
Branch: 1 branch
BranchTrueFalseTotal
if ($anonobj and defined $App::Greple::xlate::anonymize_mark) 1 48 49
Condition100%3/3
$anonobj and defined $App::Greple::xlate::anonymize_mark
!ll&&!rl&&r
37111
MC/DC100%2/2
$anonobj and defined $App::Greple::xlate::anonymize_mark
$anonobj defined $App::Greple::xlate::anonymize_mark
Truth table100%3/3
A and B
A= $anonobjB= defined $App::Greple::xlate::anonymize_mark
ABresult
0X0
100
111
1363 134 ▶ my $regex = length($anonymize_mark)
Branch: 1 branch
BranchTrueFalseTotal
length $App::Greple::xlate::anonymize_mark ? : 0 1 1
1364 ? $anonymize_mark : $App::Greple::xlate::Mask::DEFAULT_MARK;
1365 134 $anonobj->file_rules(
1366 App::Greple::xlate::Mask::extract_marks($current_text, $regex));
1367 }
1368 134 ▶ if (not defined $xlate_engine) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $App::Greple::xlate::xlate_engine) 0 49 49
1369 134 die "Select translation engine.\n";
1370 }
1371 134 ▶ if ($output_format =~ /^(:+)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($App::Greple::xlate::output_format =~ /^(:+)$/u) 0 49 49
1372 134 $colon_count = length($1);
1373 134 $output_format = 'colon';
1374 }
1375 29 ▶ if (my $file = cache_file) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $file = cache_file()) 37 12 49
1376 105 my @opt;
1377 134 ▶ if ($cache_method =~ /create|clear/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($App::Greple::xlate::cache_method =~ /create|clear/iu) 1 36 37
1378 0 push @opt, clear => 1;
1379 }
1380 0 ▶ if ($cache_method =~ /accumulate/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($App::Greple::xlate::cache_method =~ /accumulate/iu) 0 37 37
1381 0 push @opt, accumulate => 1;
1382 }
1383 0 ▶ if ($force_update) {
Branch: 1 branch
BranchTrueFalseTotal
if ($App::Greple::xlate::force_update) 0 37 37
1384 0 push @opt, force_update => 1;
1385 }
1386 0 ▶ if (defined $cache_seed) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $App::Greple::xlate::cache_seed) 2 35 37
1387 51 push @opt, seed => $cache_seed;
1388 }
1389 51 ▶ if ($dryrun) {
Branch: 1 branch
BranchTrueFalseTotal
if ($App::Greple::xlate::dryrun) 4 33 37
1390 42 push @opt, readonly => 1;
1391 }
1392 9 require App::Greple::xlate::Cache;
1393 2 tie %cache, 'App::Greple::xlate::Cache', $file, @opt;
1394 7 ▶ die "skip $current_file" if $cache_method eq 'create';
Branch: 1 branch
BranchTrueFalseTotal
if $App::Greple::xlate::cache_method eq "create" 0 37 37
1395 }
1396 }
1397  
1398 0 ▶sub end {
Subroutine end: not called CC=1 SCAR=0.0
Pod undocumented
1399 # if (my $obj = tied %cache) {
1400 # $obj->update;
1401 # }
1402 }
1403  
1404 sub set {
1405 49 ▶ while (my($key, $val) = splice @_, 0, 2) {
Subroutine set: called CC=4 SCAR=14.2
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
if (() = splice(@_, 0, 2)) 4 4 8
1406 49 ▶ next if $key eq &::FILELABEL;
Branch: 1 branch
BranchTrueFalseTotal
if $key eq &main::FILELABEL 0 4 4
1407 49 ▶ die "$key: Invalid option.\n" if not exists $opt{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $App::Greple::xlate::opt{$key} 0 4 4
1408 49 opt($key) = $val;
1409 }
1410 }
1411  
1412 1;
1413  
1414 __DATA__
« blib/lib/App/Greple/xlate/gpty/gpt4.pm ↑ Summary blib/lib/App/Greple/xlate/deepl.pm »