blib/lib/oEdtk/LaTeX.pm

filter: Statementstmt 34.9% 101 / 289 Branchbran 40.0% 68 / 170 Conditioncond 2.7% 1 / 36 MC/DCmcdc 0.0% 0 / 18 Subroutinesub 33.3% 16 / 48 Podpod 31.5% 12 / 38 totaltotal 33.0% 198 / 599 CC 102 SCAR 30.2
CC
102
Cov
34%
special_colored_address
56.0
remove_roman_numbering
56.0
skip_csv
56.0
CRAP 20.5
« lib/oEdtk/Ext.pm ↑ Summary blib/lib/oEdtk/Record.pm »
1 package oEdtk::LaTeX;
2  
3 # LaTeX helpers, ported from the fork's oEdtk::LaTeX at S23
4 # (docs/integration/PLAN.md). The module is generic: it only
5 # depends on oEdtk::Utils for the index format, and on the latin-9 character
6 # table. The fork exported three names it never defined (demo_write,
7 # demo_expected_write, unfold_list); they are restored here so the exported API
8 # is truthful. They are diagnostic helpers, unused by the production path.
9  
10 1 ▶use base qw( Exporter );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Carp qw( carp croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use Data::Dumper qw( Dumper );
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use Encode qw( encode );
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use Roman qw( roman );
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶use oEdtk::Utils qw( IDX_FORMAT );
Subroutine BEGIN: called CC=1 SCAR=0.0
19 1 ▶use charnames ':full';
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 1 our $VERSION = 2.1126;
22  
23 1 our @EXPORT_OK =
24 qw( tag tag_args ged_tag make_index demo demo_write demo_expected_write latex_escape_string translate_unit concat_string latex_boolean unfold_list
25 );
26  
27 1 Readonly my $LATIN9_ENCODING => 'iso-8859-15';
28 1 Readonly my $OUTPUT_ENCODING => $LATIN9_ENCODING;
29 1 Readonly my $LIST_SEPARATOR =>
30 "\N{LATIN CAPITAL LETTER S WITH CARON}"; # 0xa6 in latin-9 (U+00A6 is BROKEN BAR)
31 1 Readonly my $LINE_BREAK => '\\\\';
32 1 Readonly my $ADDRESS_SEPARATOR => '~\edGreenBullet~';
33  
34 1 Readonly my $UNKNOWN_CHARACTER => '\textcolor{red}{(\#)}';
35 1 Readonly my $DEMO_FILE => '/tmp/demo_chars.tex';
36  
37 1 Readonly my $INDEX_FORMAT => oEdtk::Utils::IDX_FORMAT();
38  
39 1 Readonly my $WRITE_UNKNOWN_CHAR => '?';
40 1 Readonly my $DEMO_WRITE_FILE => '/tmp/demo_write.tex';
41 1 Readonly my $EXPECTED_WRITE_FILE => '/tmp/expected_write.txt';
42  
43 1 ▶Readonly my $VERBOSE => exists $ENV{'VERBOSE'} && $ENV{'VERBOSE'};
Condition33%1/3
exists $ENV{'VERBOSE'} && $ENV{'VERBOSE'}
!ll&&!rl&&r
100
MC/DC0%0/2
exists $ENV{'VERBOSE'} && $ENV{'VERBOSE'}
exists $ENV{'VERBOSE'} $ENV{'VERBOSE'}
Truth table33%1/3
A && B
A= exists $ENV{'VERBOSE'}B= $ENV{'VERBOSE'}
ABresult
0X0
100
111
44  
45 1 Readonly my $GED_TAG_XSOURCE => 'xSOURCE';
46 1 Readonly my $GED_VALUE_PRECONTENTIEUX => 'PREC';
47  
48 1 Readonly my $CSV_SEPARATOR => q{;}; # hardcoded in edtkdoc.cls
49  
50 1 Readonly my @ORDS => sort { $a <=> $b }
51 ( 0 .. 255, 0x20ac, 0x160, 0x161, 0x17d, 0x17e, 0x152, 0x153, 0x178 )
52 ; # character #'s to know about
53 1 my $CHARACTER_CACHE = { map { $_ => compute_character_cache($_) } @ORDS };
54  
55 0 ▶sub ged_tag_xSOURCE {
Subroutine ged_tag_xSOURCE: not called CC=1 SCAR=6.9
Pod undocumented
56 0 return $GED_TAG_XSOURCE;
57 }
58  
59 0 ▶sub ged_value_precontentieux {
Subroutine ged_value_precontentieux: not called CC=1 SCAR=6.9
Pod undocumented
60 0 return $GED_VALUE_PRECONTENTIEUX;
61 }
62  
63 8 ▶sub encode_error {
Subroutine encode_error: called CC=2 SCAR=7.1
Pod undocumented
64 8 my ($ord) = @_;
65 8 ▶ carp sprintf(
Branch: 1 branch
BranchTrueFalseTotal
if $VERBOSE 0 8 8
66 "Character <U+%04x> ('%s') does not map to %s",
67 $ord, charnames::viacode($ord),
68 $OUTPUT_ENCODING
69 ) if $VERBOSE;
70 8 return $UNKNOWN_CHARACTER;
71 }
72  
73 63 ▶sub unknown_character {
Subroutine unknown_character: called CC=2 SCAR=7.1
Pod undocumented
74 63 my ($str) = @_;
75 63 ▶ die "Bad str '$str'" if $str =~ m{[^a-z0-9]}six; # avoid deep recursion
Branch: 1 branch
BranchTrueFalseTotal
if $str =~ /[^a-z0-9]/six 0 63 63
76 63 return '\textcolor{red}{(' . $str . ')}';
77 }
78  
79 95 ▶sub compute_character_cache_ascii {
Subroutine compute_character_cache_ascii: called CC=11 SCAR=24.0
Pod undocumented
80 95 my ($ord) = @_;
81 95 ▶ return '\#' if $ord == 0x23;
Branch: 1 branch
BranchTrueFalseTotal
if $ord == 35 1 94 95
82 94 ▶ return '\$' if $ord == 0x24;
Branch: 1 branch
BranchTrueFalseTotal
if $ord == 36 1 93 94
83 93 ▶ return '\%' if $ord == 0x25;
Branch: 1 branch
BranchTrueFalseTotal
if $ord == 37 1 92 93
84 92 ▶ return '\&' if $ord == 0x26;
Branch: 1 branch
BranchTrueFalseTotal
if $ord == 38 1 91 92
85 91 ▶ return '\_' if $ord == 0x5f;
Branch: 1 branch
BranchTrueFalseTotal
if $ord == 95 1 90 91
86 90 ▶ return '\textbackslash{}' if $ord == 0x5c; # U+005C REVERSE SOLIDUS
Branch: 1 branch
BranchTrueFalseTotal
if $ord == 92 1 89 90
87 89 ▶ return '\textasciicircum{}' if $ord == 0x5e; # U+005E CIRCUMFLEX ACCENT
Branch: 1 branch
BranchTrueFalseTotal
if $ord == 94 1 88 89
88 88 ▶ return '\{' if $ord == 0x7b; # '\\textbraceleft{}'
Branch: 1 branch
BranchTrueFalseTotal
if $ord == 123 1 87 88
89 87 ▶ return '\}' if $ord == 0x7d; # '\\textbraceright{}'
Branch: 1 branch
BranchTrueFalseTotal
if $ord == 125 1 86 87
90 86 ▶ return '\textasciitilde{}' if $ord == 0x7e; # U+007E TILDE
Branch: 1 branch
BranchTrueFalseTotal
if $ord == 126 1 85 86
91 85 return chr($ord);
92 }
93  
94 # should be useless since the switch to lualatex (see PHX-COUR)
95 96 ▶sub compute_character_cache_8bit {
Subroutine compute_character_cache_8bit: called CC=9 SCAR=22.0
Pod undocumented
96 96 my ($ord) = @_;
97 96 ▶ return '{$\neg$}' if $ord == 0xac; # U+00AC NOT SIGN
Branch: 1 branch
BranchTrueFalseTotal
if $ord == 172 1 95 96
98 95 ▶ return '{$\pm$}' if $ord == 0xb1; # U+00B1 PLUS-MINUS SIGN
Branch: 1 branch
BranchTrueFalseTotal
if $ord == 177 1 94 95
99 94 ▶ return '{$^2$}' if $ord == 0xb2; # U+00B2 SUPERSCRIPT TWO
Branch: 1 branch
BranchTrueFalseTotal
if $ord == 178 1 93 94
100 93 ▶ return '{$^3$}' if $ord == 0xb3; # U+00B3 SUPERSCRIPT THREE
Branch: 1 branch
BranchTrueFalseTotal
if $ord == 179 1 92 93
101 92 ▶ return '{$\mu$}' if $ord == 0xb5; # U+00B5 MICRO SIGN
Branch: 1 branch
BranchTrueFalseTotal
if $ord == 181 1 91 92
102 91 ▶ return '{$^1$}' if $ord == 0xb9; # U+00B9 SUPERSCRIPT ONE
Branch: 1 branch
BranchTrueFalseTotal
if $ord == 185 1 90 91
103 90 ▶ return '{$\times$}' if $ord == 0xd7; # U+00D7 MULTIPLICATION SIGN
Branch: 1 branch
BranchTrueFalseTotal
if $ord == 215 1 89 90
104 89 ▶ return '\textdiv{}' if $ord == 0xf7; # U+00F7 DIVISION SIGN
Branch: 1 branch
BranchTrueFalseTotal
if $ord == 247 1 88 89
105 88 return encode( $OUTPUT_ENCODING, chr($ord), \&encode_error ); # not latin => 8 exceptions
106 }
107  
108 8 ▶sub compute_character_cache_extra {
Subroutine compute_character_cache_extra: called CC=10 SCAR=23.5
Pod undocumented
109 8 my ($ord) = @_;
110 8 ▶ return chr($ord) if $ord == 0x152; # U+0152 LATIN CAPITAL LIGATURE OE
Branch: 1 branch
BranchTrueFalseTotal
if $ord == 338 1 7 8
111 7 ▶ return chr($ord) if $ord == 0x153; # U+0153 LATIN SMALL LIGATURE OE
Branch: 1 branch
BranchTrueFalseTotal
if $ord == 339 1 6 7
112 6 ▶ return chr($ord)
Branch: 1 branch
BranchTrueFalseTotal
if $ord == 352 1 5 6
113 if $ord == 0x160; # U+0160 LATIN CAPITAL LETTER S WITH CARON
114 5 ▶ return chr($ord) if $ord == 0x161; # U+0161 LATIN SMALL LETTER S WITH CARON
Branch: 1 branch
BranchTrueFalseTotal
if $ord == 353 1 4 5
115 4 ▶ return chr($ord)
Branch: 1 branch
BranchTrueFalseTotal
if $ord == 376 1 3 4
116 if $ord == 0x178; # U+0178 LATIN CAPITAL LETTER Y WITH DIAERESIS
117 3 ▶ return chr($ord)
Branch: 1 branch
BranchTrueFalseTotal
if $ord == 381 1 2 3
118 if $ord == 0x17d; # U+017D LATIN CAPITAL LETTER Z WITH CARON
119 2 ▶ return chr($ord) if $ord == 0x17e; # U+017E LATIN SMALL LETTER Z WITH CARON
Branch: 1 branch
BranchTrueFalseTotal
if $ord == 382 1 1 2
120 1 ▶ return chr($ord)
Branch: 1 branch
BranchTrueFalseTotal
if $ord == 8364 1 0 1
121 if $ord == 0x20ac; # U+20AC EURO SIGN -- NB violates the standard, unlike eurosym's \EUR{}
122 0 ▶ carp Dumper( sprintf( "Unknown character 0x%02x ('%s')", $ord, chr($ord) ) )
Branch: 1 branch
BranchTrueFalseTotal
if $VERBOSE 0 0 0
123 if $VERBOSE;
124 0 return $UNKNOWN_CHARACTER;
125 }
126  
127 264 ▶sub compute_character_cache {
Subroutine compute_character_cache: called CC=10 SCAR=23.1
Pod undocumented
128 264 my ($ord) = @_;
129 264 ▶ croak "Unimplemented output encoding '$OUTPUT_ENCODING'"
Branch: 1 branch
BranchTrueFalseTotal
if $OUTPUT_ENCODING ne $LATIN9_ENCODING 0 264 264
130 if $OUTPUT_ENCODING ne $LATIN9_ENCODING;
131 264 ▶ if ( $ord < 0x20 ) { # control character
Branch: 4 branches
BranchTrueFalseTotal
if ($ord < 32) { } 32 232 264
elsif ($ord < 127) { } 95 137 232
elsif ($ord <= 159) { } 33 104 137
elsif ($ord <= 255) { } 96 8 104
132 32 ▶ return "$LINE_BREAK\n"
Branch: 1 branch
BranchTrueFalseTotal
if $ord == 10 1 31 32
133 if $ord == 0x0a
134 ; # \n --; \n avoids '\\"{}' which makes indexation fail # might fail if line is empty
135 31 ▶ return '' if $ord == 0x0d; # \r
Branch: 1 branch
BranchTrueFalseTotal
if $ord == 13 1 30 31
136 30 ▶ carp sprintf "Found character 0x%02x ('%s')", $ord, charnames::viacode($ord)
Branch: 1 branch
BranchTrueFalseTotal
if $VERBOSE 0 30 30
137 if $VERBOSE;
138 30 return unknown_character( sprintf( "%02x", $ord ) );
139 }
140 95 elsif ( $ord < 0x7f ) { return compute_character_cache_ascii($ord); }
141 elsif ( $ord <= 0x9f ) { # windows-1252? bugware this?
142 33 ▶ carp sprintf "Found character 0x%02x ('%s')", $ord, charnames::viacode($ord)
Branch: 1 branch
BranchTrueFalseTotal
if $VERBOSE 0 33 33
143 if $VERBOSE;
144 33 return unknown_character( sprintf( "%02x", $ord ) );
145 }
146 96 elsif ( $ord <= 0xff ) { return compute_character_cache_8bit($ord); }
147 8 else { return compute_character_cache_extra($ord); }
148 }
149  
150 0 ▶sub latex_escape_character {
Subroutine latex_escape_character: not called CC=2 SCAR=17.9
Pod undocumented
151 0 my ($ord) = @_;
152 return exists $CHARACTER_CACHE->{$ord}
153 0 ▶ ? $CHARACTER_CACHE->{$ord}
Branch: 1 branch
BranchTrueFalseTotal
exists $CHARACTER_CACHE->{$ord} ? : 0 0 0
154 : compute_character_cache($ord);
155 }
156  
157 0 ▶sub latex_boolean {
Subroutine latex_boolean: not called CC=2 SCAR=17.9
Pod documented
158 0 my ($bool) = @_;
159 0 ▶ return $bool ? "true" : "false";
Branch: 1 branch
BranchTrueFalseTotal
$bool ? : 0 0 0
160 }
161  
162 0 ▶sub translate_unit {
Subroutine translate_unit: not called CC=5 SCAR=34.0
Pod documented
163 0 my ($str) = @_;
164 0 ▶ die "str undef" unless defined $str;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $str 0 0 0
165 0 ▶ if ( $str eq '' ) { return ''; }
Branch: 3 branches
BranchTrueFalseTotal
if ($str eq '') { } 0 0 0
elsif ($str eq 'EUR') { } 0 0 0
elsif ($str eq '%') { } 0 0 0
166 elsif ( $str eq 'EUR' ) {
167 0 return latex_narrow_space() . tag_args('EUR');
168 } # eurosym EURO symbol
169 elsif ( $str eq '%' ) {
170 0 return latex_narrow_space() . latex_escape_string('%');
171 }
172 0 else { croak "Unknown unit '$str'"; }
173 }
174  
175 0 ▶sub latex_escape_string {
Subroutine latex_escape_string: not called CC=1 SCAR=6.9
Pod documented
176 0 my ($str) = @_;
177 0 return join( "", map { latex_escape_character( ord($_) ) } split( m//, $str ) );
178 }
179  
180 0 ▶sub latex_escape_string_no_byte_a6 {
Subroutine latex_escape_string_no_byte_a6: not called CC=3 SCAR=24.8
Pod undocumented
181 0 my ($str) = @_;
182 0 ▶ croak "OUTPUT_ENCODING unexpected: $OUTPUT_ENCODING"
Branch: 1 branch
BranchTrueFalseTotal
if $OUTPUT_ENCODING ne $LATIN9_ENCODING 0 0 0
183 if $OUTPUT_ENCODING ne $LATIN9_ENCODING;
184 0 ▶ if ( $str =~ m/\N{LATIN CAPITAL LETTER S WITH CARON}/sox ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($str =~ /\N{U+160}/osx) 0 0 0
185 0 carp "Replace capital letter S's with caron in '$str' with capital S's...";
186 0 $str =~ s/\N{LATIN CAPITAL LETTER S WITH CARON}/S/gsox;
187 }
188 0 return latex_escape_string($str);
189 }
190  
191 0 ▶sub latex_escape_list_no_byte_a6 {
Subroutine latex_escape_list_no_byte_a6: not called CC=2 SCAR=17.9
Pod undocumented
192 0 my (@l) = @_;
193 0 ▶ return map { ( $_ =~ m{\S}sx ) ? latex_escape_string_no_byte_a6($_) : () } @l;
Branch: 1 branch
BranchTrueFalseTotal
$_ =~ /\S/sx ? : 0 0 0
194 }
195  
196 0 ▶sub latex_list {
Subroutine latex_list: not called CC=1 SCAR=6.9
Pod undocumented
197 0 my ($l) = @_;
198 0 my @res = latex_escape_list_no_byte_a6( @{$l} );
199 0 return join( $LIST_SEPARATOR, @res );
200 }
201  
202 # cannot use colors with \StrSubstitute replacements... (TeX capacity exceeded)
203 # used for addresses in the bottom of pages (green separators)
204 0 ▶sub special_colored_address {
Subroutine special_colored_address: not called CC=7 SCAR=40.3
Pod undocumented
205 0 my ($val) = @_;
206 0 ▶ if ( ref $val eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $val eq "HASH") 0 0 0
207 0 my $nKeys = scalar keys %{$val};
208 0 ▶ if ( ( $nKeys == 1 )
Branch: 1 branch
BranchTrueFalseTotal
if ($nKeys == 1 and exists $val->{"address"} and ref $val->{"address"} eq "ARRAY") 0 0 0
Condition0%0/6
$nKeys == 1 and exists $val->{"address"} and ref $val->{"address"} eq "ARRAY"
!ll&&!rl&&r
000
$nKeys == 1 and exists $val->{"address"}
!ll&&!rl&&r
000
MC/DC0%0/3
$nKeys == 1 and exists $val->{"address"} and ref $val->{"address"} eq "ARRAY"
$nKeys == 1 exists $val->{"address"} ref $val->{"address"} eq "ARRAY"
Truth table0%0/4
A and B and C
A= $nKeys == 1B= exists $val->{"address"}C= ref $val->{"address"} eq "ARRAY"
ABCresult
0XX0
10X0
1100
1111
209 && ( exists $val->{'address'} )
210 && ( ( ref( $val->{'address'} ) eq 'ARRAY' ) ) )
211 {
212 0 my @res = ();
213 0 my @val = @{ $val->{'address'} };
214 0 my $first = shift @val;
215 0 ▶ push @res, '\textbf{', latex_escape_string_no_byte_a6($first), '}' . $LINE_BREAK
Branch: 1 branch
BranchTrueFalseTotal
if $first =~ /\S/osx 0 0 0
216 if $first =~ m{\S}sox;
217 0 push @res, join( $ADDRESS_SEPARATOR, latex_escape_list_no_byte_a6(@val) );
218 0 ▶ push @res, $LINE_BREAK if scalar @val;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @val 0 0 0
219 0 push @res, '{\TelIndigoWebsite}';
220 0 return join( '', @res );
221 }
222 }
223 0 croak 'Not implemented: ' . Dumper($val);
224 }
225  
226 # kludges when we need Perl to compute complicated LaTeX
227 0 ▶sub special_treatment {
Subroutine special_treatment: not called CC=3 SCAR=24.8
Pod undocumented
228 0 my ($h) = @_;
229 0 my @keys = keys %{$h};
230 0 my $nKeys = scalar @keys;
231 0 ▶ if ( $nKeys == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nKeys == 1) 0 0 0
232 0 my $key = $keys[0];
233 0 ▶ return special_colored_address( $h->{$key} ) if $key eq 'colored_address';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "colored_address" 0 0 0
234 }
235 0 croak 'Not implemented: ' . Dumper($h);
236 }
237  
238 0 ▶sub latex_xkeyval_value {
Subroutine latex_xkeyval_value: not called CC=5 SCAR=34.0
Pod undocumented
239 0 my ($obj) = @_;
240 0 my $ref = ref $obj;
241 0 ▶ if ( $ref eq "" ) { # should we care about embedded newlines?
Branch: 3 branches
BranchTrueFalseTotal
if ($ref eq '') { } 0 0 0
elsif ($ref eq 'ARRAY') { } 0 0 0
elsif ($ref eq 'HASH') { } 0 0 0
242 0 $obj = latex_escape_string($obj);
243 }
244 elsif ( $ref eq "ARRAY" ) {
245 0 $obj = latex_list($obj);
246 }
247 elsif ( $ref eq "HASH" ) {
248 0 $obj = special_treatment($obj);
249 }
250 else {
251 0 croak "Should not happen/not implemented. Bad object ref: " . Dumper($obj);
252 }
253 0 ▶ return ( $obj =~ m{[=,\{\}]}sx ) ? "{$obj}" : $obj;
Branch: 1 branch
BranchTrueFalseTotal
$obj =~ /[=,\{\}]/sx ? : 0 0 0
254 }
255  
256 # define a list the oEdtk way
257 0 ▶sub latex_define_list {
Subroutine latex_define_list: not called CC=1 SCAR=6.9
Pod undocumented
258 0 my ( $tag, $l ) = @_;
259 0 return ( "\\edListNew{$tag}", map { "\\edListAdd{$tag}{$_}" } @{$l} );
260 }
261  
262 0 ▶sub remove_roman_numbering {
Subroutine remove_roman_numbering: not called CC=7 SCAR=40.3
Pod undocumented
263 0 my ($h) = @_;
264 0 foreach my $key ( sort keys %{$h} ) {
265 0 my $ref = ref $h->{$key};
266 0 ▶ if ( substr( $key, -6 ) eq "#roman" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($key, -6) eq '#roman') { } 0 0 0
267 0 ▶ croak "Unexpected value for key '$key': " . Dumper($h)
Branch: 1 branch
BranchTrueFalseTotal
unless $ref eq "ARRAY" 0 0 0
268 unless $ref eq "ARRAY";
269 0 my $counter = 0;
270 0 my $baseKey = substr( $key, 0, -6 );
271 0 foreach my $val ( @{ $h->{$key} } ) {
272 0 $counter++;
273 0 my $romanKey = $baseKey . roman($counter);
274 0 ▶ croak "Bad tag '$romanKey'" unless is_valid_tag($romanKey);
Branch: 1 branch
BranchTrueFalseTotal
unless is_valid_tag($romanKey) 0 0 0
275 0 $h->{$romanKey} = $val;
276 }
277 0 delete $h->{$key};
278 }
279 else {
280 0 ▶ croak "Bad tag '$key'" unless is_valid_tag($key);
Branch: 1 branch
BranchTrueFalseTotal
unless is_valid_tag($key) 0 0 0
281 }
282 }
283 0 return $h;
284 }
285  
286 # needed to circumvent edtkdoc.cls bug (poor man's CSV)
287 # (whitespace and u+FFFD are already taken care of in string_to_latex,
288 # but just in case)
289 0 ▶sub skip_csv {
Subroutine skip_csv: not called CC=7 SCAR=40.3
Pod undocumented
290 0 my ($c) = @_;
291 return
292 0 ▶ ( $c eq $CSV_SEPARATOR )
Condition0%0/18
$c eq $CSV_SEPARATOR || $c eq q['] || $c eq '"' || $c eq "\n" || $c eq "\r" || $c eq "\t" || $c eq "\x{fffd}"
l!l&&r!l&&!r
000
$c eq $CSV_SEPARATOR or $c eq "'" or $c eq "\"" or $c eq "\n" or $c eq "\r" or $c eq "\t"
l!l&&r!l&&!r
000
$c eq $CSV_SEPARATOR or $c eq "'" or $c eq "\"" or $c eq "\n" or $c eq "\r"
l!l&&r!l&&!r
000
$c eq $CSV_SEPARATOR or $c eq "'" or $c eq "\"" or $c eq "\n"
l!l&&r!l&&!r
000
$c eq $CSV_SEPARATOR or $c eq "'" or $c eq "\""
l!l&&r!l&&!r
000
$c eq $CSV_SEPARATOR or $c eq "'"
l!l&&r!l&&!r
000
MC/DC0%0/7
$c eq $CSV_SEPARATOR || $c eq q['] || $c eq '"' || $c eq "\n" || $c eq "\r" || $c eq "\t" || $c eq "\x{fffd}"
$c eq $CSV_SEPARATOR $c eq "'" $c eq "\"" $c eq "\n" $c eq "\r" $c eq "\t" $c eq "\x{fffd}"
Truth table0%0/8
A or B or C or D or E or F || G
A= $c eq $CSV_SEPARATORB= $c eq "'"C= $c eq "\""D= $c eq "\n"E= $c eq "\r"F= $c eq "\t"G= $c eq "\x{fffd}"
ABCDEFGresult
1XXXXXX1
01XXXXX1
001XXXX1
0001XXX1
00001XX1
000001X1
00000011
00000000
293 || ( $c eq q{'} )
294 || ( $c eq q{"} )
295 || ( $c eq "\n" )
296 || ( $c eq "\r" )
297 || ( $c eq "\t" )
298 || ( $c eq "\x{fffd}" ); # U+FFFD REPLACEMENT CHARACTER
299 }
300  
301 0 ▶sub sanitize_csv {
Subroutine sanitize_csv: not called CC=2 SCAR=17.9
Pod undocumented
302 0 my ($str) = @_;
303 0 ▶ return join q{}, map { skip_csv($_) ? q{ } : $_ } split m//, $str;
Branch: 1 branch
BranchTrueFalseTotal
skip_csv($_) ? : 0 0 0
304 }
305  
306 0 ▶sub index_value {
Subroutine index_value: not called CC=3 SCAR=24.8
Pod undocumented
307 0 my ( $h, $tag ) = @_;
308 0 my $ref = ref $h->{$tag};
309 return ( $ref eq '' )
310 ? "\\long\\gdef\\$tag" . "{" . latex_escape_string( sanitize_csv( $h->{$tag} ) ) . "}%"
311 0 ▶ : ( $ref eq 'ARRAY' ) ? latex_define_list( $tag, $h->{$tag} )
Branch: 2 branches
BranchTrueFalseTotal
$ref eq '' ? : 0 0 0
$ref eq 'ARRAY' ? : 0 0 0
312 : croak "Unexpected value for key '$tag': " . Dumper($h);
313 }
314  
315 0 ▶sub make_index {
Subroutine make_index: not called CC=1 SCAR=6.9
Pod documented
316 0 my ( $h, %extra ) = @_;
317 0 my $rrn = remove_roman_numbering($h); # avoid side effects
318 0 my @res = map { index_value( $rrn, $_ ) } sort keys %{$rrn};
319 0 my $extra = remove_roman_numbering( \%extra );
320 0 push @res, map { index_value( $extra, $_ ) } sort keys %{$extra};
321 0 return join( "\n", @res, '' );
322 }
323  
324 0 ▶sub check_index_is_complete {
Subroutine check_index_is_complete: not called CC=7 SCAR=40.3
Pod undocumented
325 0 my ($h) = @_;
326 0 my @errors = ();
327 0 foreach my $s ( @{$INDEX_FORMAT} ) {
328 0 my ( $orig, $new, $offset ) = @{$s};
329 0 ▶ next unless length $new;
Branch: 1 branch
BranchTrueFalseTotal
unless length $new 0 0 0
330 0 ▶ if ( !exists $h->{$new} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $h->{$new}) { } 0 0 0
331 0 push @errors, "Missing field '$new' (was '\\$orig' in the previous index system)";
332 }
333 else {
334 0 ▶ if ( defined $offset ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $offset) 0 0 0
335 push @errors,
336 "Field '$new' not a list (was '\\$orig' in the previous index system)"
337 0 ▶ unless ref( $h->{$new} ) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $h->{$new} eq "ARRAY" 0 0 0
338 }
339 }
340 }
341 0 ▶ croak join( "\n", @errors, '' ) if scalar @errors;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @errors 0 0 0
342 0 return;
343 }
344  
345 0 ▶sub max {
Subroutine max: not called CC=3 SCAR=24.8
Pod undocumented
346 0 my (@l) = @_;
347 0 my $res = 0;
348 0 foreach my $val (@l) {
349 0 ▶ $res = $val if $val > $res;
Branch: 1 branch
BranchTrueFalseTotal
if $val > $res 0 0 0
350 }
351 0 return $res;
352 }
353  
354 0 ▶sub pad_left {
Subroutine pad_left: not called CC=1 SCAR=6.9
Pod undocumented
355 0 my ( $str, $len ) = @_;
356 0 return ( " " x ( $len - length($str) ) ) . $str;
357 }
358  
359 0 ▶sub is_valid_tag {
Subroutine is_valid_tag: not called CC=1 SCAR=6.9
Pod undocumented
360 0 my ($tag) = @_;
361 0 return $tag =~ m{^[a-z]+$}six;
362 }
363  
364 0 ▶sub ged_tag {
Subroutine ged_tag: not called CC=2 SCAR=17.9
Pod documented
365 0 my ( $tag, $value ) = @_;
366 0 ▶ return ["Bad tag '$tag'"] unless is_valid_tag($tag);
Branch: 1 branch
BranchTrueFalseTotal
unless is_valid_tag($tag) 0 0 0
367 0 return "\\gdef\\$tag" . '{' . latex_escape_string($value) . '}%';
368 }
369  
370 # output value: a list of one error, or the string wanted
371 0 ▶sub tag {
Subroutine tag: not called CC=6 SCAR=37.4
Pod documented
372 0 my ( $tag, $h ) = @_;
373 0 ▶ return ["Bad tag '$tag'"] unless is_valid_tag($tag);
Branch: 1 branch
BranchTrueFalseTotal
unless is_valid_tag($tag) 0 0 0
374 0 ▶ return "\\$tag" if !defined $h;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $h 0 0 0
375 0 ▶ return ["Defined but empty hash for tag '$tag'"] unless scalar keys %{$h};
Branch: 1 branch
BranchTrueFalseTotal
unless scalar keys %{$h;} 0 0 0
376 0 my @l = ();
377 0 my $pad = 1 + max( map { length $_ } keys %{$h} );
378 0 foreach my $key ( sort keys %{$h} ) {
379 0 ▶ return [ "key '$key' undef: " . Dumper($h) ] unless defined $h->{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $h->{$key} 0 0 0
380 0 push @l, join( " = ", pad_left( $key, $pad ), latex_xkeyval_value( $h->{$key} ) );
381 }
382 0 return "\\$tag\[\n\x20" . join( "\n,", @l ) . "\n]%";
383 }
384  
385 0 ▶sub latex_narrow_space {
Subroutine latex_narrow_space: not called CC=1 SCAR=6.9
Pod undocumented
386 0 return "\\,";
387 }
388  
389 0 ▶sub concat_string {
Subroutine concat_string: not called CC=1 SCAR=6.9
Pod documented
390 0 my (@l) = @_;
391 0 return join( '', map { interpret_argument($_) } @l );
392 }
393  
394 0 ▶sub interpret_argument {
Subroutine interpret_argument: not called CC=3 SCAR=24.8
Pod undocumented
395 0 my ($arg) = @_;
396 0 my $ref = ref $arg;
397 0 ▶ if ( $ref eq '' ) { return latex_escape_string($arg); }
Branch: 2 branches
BranchTrueFalseTotal
if ($ref eq '') { } 0 0 0
elsif ($ref eq 'ARRAY') { } 0 0 0
398 0 elsif ( $ref eq 'ARRAY' ) { my ( $sub, @l ) = @{$arg}; return &{$sub}(@l); }
399 0 else { croak "Unexpected ref $ref for arg: " . Dumper($arg); }
400 }
401  
402 0 ▶sub tag_args {
Subroutine tag_args: not called CC=3 SCAR=24.8
Pod documented
403 0 my ( $tag, @args ) = @_;
404 0 ▶ die "Bad tag '$tag'" unless is_valid_tag($tag);
Branch: 1 branch
BranchTrueFalseTotal
unless is_valid_tag($tag) 0 0 0
405 0 ▶ @args = ('') unless scalar @args; # at least {} after the tag name
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @args 0 0 0
406 0 return join( '', "\\$tag", map { '{' . interpret_argument($_) . '}' } @args );
407 }
408  
409 0 ▶sub demo {
Subroutine demo: not called CC=4 SCAR=30.0
Pod documented
410 my @l =
411 0 map { [ $_, $_, $CHARACTER_CACHE->{$_}, charnames::viacode($_) ] } @ORDS;
412 0 my $preamble = <<'EOPREAMBLE';
413 \documentclass{article}
414 \usepackage[T1]{fontenc}
415 \usepackage[latin9]{inputenc}
416 \usepackage{color,textcomp,longtable}
417 \begin{document}
418 EOPREAMBLE
419 0 my $postamble = <<'EOPOSTAMBLE';
420 \end{document}
421 EOPOSTAMBLE
422 my $body = join(
423 "\n",
424 '\begin{longtable}{llcl}',
425 '\hline',
426 "\\textbf{Dec} & \\textbf{Hex} & \\textbf{Char} & \\textbf{Name}" . $LINE_BREAK,
427 '\hline', '\hline',
428 '\endhead',
429 '\endfoot',
430 (
431 map {
432 0 ▶ sprintf "%d & %02x & %s & %s$LINE_BREAK%s", @{$_}, ( $$_[0] % 2 )
Branch: 1 branch
BranchTrueFalseTotal
$_->[0] % 2 ? : 0 0 0
433 ? "\n\\hline"
434 : ''
435 } @l
436 ),
437 '\end{longtable}',
438 ''
439 );
440 0 ▶ open my $out, ">encoding($OUTPUT_ENCODING)", $DEMO_FILE || croak $!;
Condition0%0/3
$DEMO_FILE || croak($!)
l!l&&r!l&&!r
000
MC/DC0%0/2
$DEMO_FILE || croak($!)
$DEMO_FILE croak($!)
Truth table0%0/3
A || B
A= $DEMO_FILEB= croak($!)
ABresult
1X1
011
000
441 0 print $out join( '', $preamble, $body, $postamble );
442 0 ▶ close $out || croak $!;
Branch: 1 branch
BranchTrueFalseTotal
close $out or croak($!) 0 0 0
443 0 carp "demo saved to '$DEMO_FILE'";
444 0 return 0;
445 }
446  
447 # The fork listed these three names in @EXPORT_OK without defining them: an
448 # import succeeded but a call died ("Undefined subroutine"). They are restored
449 # here so the exported API is truthful and T33 holds. All three are diagnostic
450 # helpers; no production path calls them.
451  
452 # The "written" companion of demo(): same character table written to
453 # $DEMO_WRITE_FILE, with unmapped characters rendered as $WRITE_UNKNOWN_CHAR.
454 0 ▶sub demo_write {
Subroutine demo_write: not called CC=4 SCAR=30.0
Pod documented
455 my $body = join(
456 "\n",
457 (
458 map {
459 0 sprintf "%d & %02x & %s & %s", $_, $_,
460 ( $CHARACTER_CACHE->{$_} eq $UNKNOWN_CHARACTER )
461 ? $WRITE_UNKNOWN_CHAR
462 0 ▶ : $CHARACTER_CACHE->{$_},
Branch: 1 branch
BranchTrueFalseTotal
$CHARACTER_CACHE->{$_} eq $UNKNOWN_CHARACTER ? : 0 0 0
463 charnames::viacode($_);
464 } @ORDS
465 ),
466 ''
467 );
468 0 ▶ open my $out, ">encoding($OUTPUT_ENCODING)", $DEMO_WRITE_FILE || croak $!;
Condition0%0/3
$DEMO_WRITE_FILE || croak($!)
l!l&&r!l&&!r
000
MC/DC0%0/2
$DEMO_WRITE_FILE || croak($!)
$DEMO_WRITE_FILE croak($!)
Truth table0%0/3
A || B
A= $DEMO_WRITE_FILEB= croak($!)
ABresult
1X1
011
000
469 0 print $out $body;
470 0 ▶ close $out || croak $!;
Branch: 1 branch
BranchTrueFalseTotal
close $out or croak($!) 0 0 0
471 0 carp "demo_write saved to '$DEMO_WRITE_FILE'";
472 0 return 0;
473 }
474  
475 # The expected character table, written to $EXPECTED_WRITE_FILE, for a human to
476 # diff against demo_write().
477 0 ▶sub demo_expected_write {
Subroutine demo_expected_write: not called CC=3 SCAR=24.8
Pod documented
478 0 my $body = join( "\n", ( map { sprintf "%02x %s", $_, $CHARACTER_CACHE->{$_} } @ORDS ), '' );
479 0 ▶ open my $out, ">encoding($OUTPUT_ENCODING)", $EXPECTED_WRITE_FILE || croak $!;
Condition0%0/3
$EXPECTED_WRITE_FILE || croak($!)
l!l&&r!l&&!r
000
MC/DC0%0/2
$EXPECTED_WRITE_FILE || croak($!)
$EXPECTED_WRITE_FILE croak($!)
Truth table0%0/3
A || B
A= $EXPECTED_WRITE_FILEB= croak($!)
ABresult
1X1
011
000
480 0 print $out $body;
481 0 ▶ close $out || croak $!;
Branch: 1 branch
BranchTrueFalseTotal
close $out or croak($!) 0 0 0
482 0 carp "demo_expected_write saved to '$EXPECTED_WRITE_FILE'";
483 0 return 0;
484 }
485  
486 # Unfold a list one level: an arrayref element is replaced by its items.
487 0 ▶sub unfold_list {
Subroutine unfold_list: not called CC=2 SCAR=17.9
Pod documented
488 0 my (@l) = @_;
489 0 ▶ return map { ref $_ eq 'ARRAY' ? @{$_} : $_ } @l;
Branch: 1 branch
BranchTrueFalseTotal
ref $_ eq 'ARRAY' ? : 0 0 0
490 }
491  
492 1 1;
493  
494 =head1 NAME
495  
496 oEdtk::LaTeX - build the LaTeX index of an edtk document
497  
498 =head1 SYNOPSIS
499  
500 use oEdtk::LaTeX qw(make_index latex_escape_string tag_args);
501  
502 my $index = make_index( \%index_variables );
503 my $s = latex_escape_string($utf8_string);
504  
505 =head1 DESCRIPTION
506  
507 Helpers used by an edtk application to turn Perl data into LaTeX: escape a
508 string to the latin-9 output encoding (C<latex_escape_string>), build the
509 index definitions (C<make_index>, C<ged_tag>, C<tag>, C<tag_args>,
510 C<concat_string>, C<latex_boolean>), translate a unit (C<translate_unit>), and
511 dump the character table for debugging (C<demo>, C<demo_write>,
512 C<demo_expected_write>, C<unfold_list>).
513  
514 Ported from the fork's C<oEdtk::LaTeX> at S23. The latin-9 character table
515 (C<@ORDS> and C<compute_character_cache*>) is kept verbatim from the fork. The
516 fork exported C<demo_write>, C<demo_expected_write> and C<unfold_list> without
517 defining them; this port defines all three, so every exported symbol is
518 callable.
519  
520 =head1 FUNCTIONS
521  
522 =head2 tag
523  
524 my $latex = tag($name, \%pairs);
525  
526 Return the LaTeX definition of one index tag from a hash of key/value pairs, or
527 a one-line error list when the tag or a value is invalid.
528  
529 =head2 tag_args
530  
531 my $latex = tag_args($name, @args);
532  
533 Build the backslash-tag call for C<$name> and the argument list, where an
534 argument may be a coderef paired with its own arguments.
535  
536 =head2 ged_tag
537  
538 my $latex = ged_tag($name, $value);
539  
540 Return a LaTeX C<gdef> definition of C<$name> holding the escaped C<$value>, or
541 an error list when the tag is invalid.
542  
543 =head2 make_index
544  
545 my $index = make_index(\%index_variables, %extra);
546  
547 Turn an index-variables hash, plus an optional extra hash, into the LaTeX index
548 text, one definition per key.
549  
550 =head2 demo
551  
552 demo();
553  
554 Write a LaTeX character table for the latin-9 ordinals to F</tmp/demo_chars.tex>
555 (a diagnostic helper).
556  
557 =head2 demo_write
558  
559 demo_write();
560  
561 Write the same character table, with unmapped characters replaced, to
562 F</tmp/demo_write.tex>.
563  
564 =head2 demo_expected_write
565  
566 demo_expected_write();
567  
568 Write the expected character table to F</tmp/expected_write.txt>, for a human to
569 diff against L</demo_write>.
570  
571 =head2 latex_escape_string
572  
573 my $escaped = latex_escape_string($string);
574  
575 Escape a string to the latin-9 output encoding.
576  
577 =head2 translate_unit
578  
579 my $latex = translate_unit($unit);
580  
581 Translate a unit (C<EUR> or a percent sign) to its LaTeX form; an unknown unit is
582 an error.
583  
584 =head2 concat_string
585  
586 my $string = concat_string(@parts);
587  
588 Concatenate the arguments into one string, escaping scalars and calling coderef
589 arguments.
590  
591 =head2 latex_boolean
592  
593 my $word = latex_boolean($bool);
594  
595 Return C<true> or C<false> for the boolean.
596  
597 =head2 unfold_list
598  
599 my @flat = unfold_list(@list);
600  
601 Unfold the list one level, replacing each array reference element with its items.
602  
603 The unexported helpers C<latex_escape_character>, C<latex_escape_string_no_byte_a6>,
604 C<latex_list>, C<special_colored_address>, C<latex_xkeyval_value>,
605 C<remove_roman_numbering>, C<sanitize_csv>, C<index_value>,
606 C<check_index_is_complete>, C<is_valid_tag>, C<interpret_argument>, the
607 C<compute_character_cache*> builders and C<@ORDS> are not part of the public API.
608  
609 =head1 AUTHOR
610  
611 The oEdtk team.
612  
613 =cut
« lib/oEdtk/Ext.pm ↑ Summary blib/lib/oEdtk/Record.pm »