lib/oEdtk/TexDoc.pm

filter: Statementstmt 48.5% 51 / 105 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 15 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 65.0% 13 / 20 Podpod 71.4% 5 / 7 totaltotal 38.5% 69 / 179 CC 20 SCAR 41.9
CC
20
Cov
36%
append_qr_code
182.0
escape
30.0
is_in_list
12.0
CRAP 66.2
« blib/lib/oEdtk/RecordParser.pm ↑ Summary blib/lib/oEdtk/tuiEdtk.pm »
1 package oEdtk::TexDoc;
2 21 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 21 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 21 our $VERSION = 2.1131;
6  
7 21 ▶use base 'oEdtk::Doc';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 21 ▶use oEdtk::Config qw(config_read);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 21 ▶use oEdtk::Dict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 21 ▶use oEdtk::TexTag;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 21 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 21 ▶use Data::Dumper qw(Dumper);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 21 ▶use JSON::PP qw(encode_json);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 21 ▶use YAML::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 21 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 21 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 21 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 # QR code geometry defaults.
21 21 Readonly my $DEFAULT_QR_CODE_LEVEL => 'H'; # error-correction level: L M Q H
22 21 Readonly my $DEFAULT_QR_CODE_UNIT => 'mm';
23 21 Readonly my $DEFAULT_QR_CODE_SIZE => 25;
24 21 Readonly my $PAGE_WIDTH_IN_MM => 210;
25 21 Readonly my $RIGHT_MARGIN_IN_MM => 10;
26 21 Readonly my $TOP_MARGIN_IN_MM => 10;
27 21 Readonly my $DEFAULT_QR_CODE_HPOS => sprintf '%f%s',
28 $PAGE_WIDTH_IN_MM - $RIGHT_MARGIN_IN_MM - $DEFAULT_QR_CODE_SIZE,
29 $DEFAULT_QR_CODE_UNIT;
30 21 Readonly my $DEFAULT_QR_CODE_VPOS => sprintf '%f%s', $TOP_MARGIN_IN_MM, $DEFAULT_QR_CODE_UNIT;
31  
32 0 ▶sub mktag {
Subroutine mktag: not called CC=1 SCAR=6.9
Pod documented
33 0 my ( $self, $name, $value ) = @_;
34  
35 0 return oEdtk::TexTag->new( $name, $value );
36 }
37  
38 0 ▶sub append_table {
Subroutine append_table: not called CC=1 SCAR=6.9
Pod documented
39 0 my ( $self, $name, @values ) = @_;
40  
41 0 $self->append( $name, \@values );
42 0 return $self;
43 }
44  
45 0 ▶sub hhline_content {
Subroutine hhline_content: not called CC=2 SCAR=17.9
Pod undocumented
46 0 my ($row) = @_;
47 0 ▶ return join q{}, map { $_ eq '*' ? '-' : '~' } @{$row};
Branch: 1 branch
BranchTrueFalseTotal
$_ eq '*' ? : 0 0 0
48 }
49  
50 0 ▶sub is_in_list {
Subroutine is_in_list: not called CC=3 SCAR=24.8
Pod undocumented
51 0 my ( $item, @l ) = @_;
52 0 foreach my $v (@l) {
53 0 ▶ return 1 if $item eq $v;
Branch: 1 branch
BranchTrueFalseTotal
if $item eq $v 0 0 0
54 }
55 0 return 0;
56 }
57  
58 # Write a QR code as a LaTeX textblock through the output-file handle.
59 # $tag is a lowercase macro name; $obj is a scalar, ARRAY or HASH to encode;
60 # $level / $size / $unit / $hpos / $vpos override the Readonly defaults.
61 0 ▶sub append_qr_code {
Subroutine append_qr_code: not called CC=13 SCAR=52.0
Pod documented
62 0 my ( $self, $tag, $obj, $level, $size, $unit, $hpos, $vpos ) = @_;
63 0 require oEdtk::Main; # lazy: oEdtk::Main itself requires this module
64 0 ▶ croak "Bad tag name '$tag'" if $tag =~ m{[^a-z]}six;
Branch: 1 branch
BranchTrueFalseTotal
if $tag =~ /[^a-z]/six 0 0 0
65 0 ▶ croak 'Bad object: ' . Dumper($obj)
Branch: 1 branch
BranchTrueFalseTotal
unless is_in_list(ref $obj, "", "ARRAY", "HASH") 0 0 0
66 if !is_in_list( ( ref $obj ), q{}, 'ARRAY', 'HASH' );
67 0 ▶ $level //= $DEFAULT_QR_CODE_LEVEL;
Condition0%0/3
$level //= $DEFAULT_QR_CODE_LEVEL
l!l&&r!l&&!r
000
MC/DC0%0/2
$level //= $DEFAULT_QR_CODE_LEVEL
$level $DEFAULT_QR_CODE_LEVEL
Truth table0%0/3
A //= B
A= $levelB= $DEFAULT_QR_CODE_LEVEL
ABresult
1X1
011
000
68 0 ▶ $size //= $DEFAULT_QR_CODE_SIZE;
Condition0%0/3
$size //= $DEFAULT_QR_CODE_SIZE
l!l&&r!l&&!r
000
MC/DC0%0/2
$size //= $DEFAULT_QR_CODE_SIZE
$size $DEFAULT_QR_CODE_SIZE
Truth table0%0/3
A //= B
A= $sizeB= $DEFAULT_QR_CODE_SIZE
ABresult
1X1
011
000
69 0 ▶ $unit //= $DEFAULT_QR_CODE_UNIT;
Condition0%0/3
$unit //= $DEFAULT_QR_CODE_UNIT
l!l&&r!l&&!r
000
MC/DC0%0/2
$unit //= $DEFAULT_QR_CODE_UNIT
$unit $DEFAULT_QR_CODE_UNIT
Truth table0%0/3
A //= B
A= $unitB= $DEFAULT_QR_CODE_UNIT
ABresult
1X1
011
000
70 0 ▶ $hpos //= $DEFAULT_QR_CODE_HPOS;
Condition0%0/3
$hpos //= $DEFAULT_QR_CODE_HPOS
l!l&&r!l&&!r
000
MC/DC0%0/2
$hpos //= $DEFAULT_QR_CODE_HPOS
$hpos $DEFAULT_QR_CODE_HPOS
Truth table0%0/3
A //= B
A= $hposB= $DEFAULT_QR_CODE_HPOS
ABresult
1X1
011
000
71 0 ▶ $vpos //= $DEFAULT_QR_CODE_VPOS;
Condition0%0/3
$vpos //= $DEFAULT_QR_CODE_VPOS
l!l&&r!l&&!r
000
MC/DC0%0/2
$vpos //= $DEFAULT_QR_CODE_VPOS
$vpos $DEFAULT_QR_CODE_VPOS
Truth table0%0/3
A //= B
A= $vposB= $DEFAULT_QR_CODE_VPOS
ABresult
1X1
011
000
72  
73 0 ▶ my $string = ( ref $obj ) ? YAML::Tiny::Dump($obj) : $obj;
Branch: 1 branch
BranchTrueFalseTotal
ref $obj ? : 0 0 0
74 0 my @log = ( encode_json( [$string] ) );
75 0 ▶ unshift @log, encode_json($obj) if ref $obj;
Branch: 1 branch
BranchTrueFalseTotal
if ref $obj 0 0 0
76  
77 # Text::QRCode is optional: it needs libqrencode to build. Load it only
78 # when a QR code is requested, so the module tree stays usable without it.
79 0 ▶ eval { require Text::QRCode; 1 }
Branch: 1 branch
BranchTrueFalseTotal
eval { do { require Text::QRCode; 1 } } or croak("append_qr_code requires the optional Text::QRCode module (which needs libqrencode)") 0 0 0
80 or croak
81 'append_qr_code requires the optional Text::QRCode module (which needs libqrencode)';
82 0 my $aoa = Text::QRCode->new( level => $level )->plot($string);
83 0 my $n = scalar @{$aoa};
84 0 ▶ die 'Empty QR code' if !$n;
Branch: 1 branch
BranchTrueFalseTotal
unless $n 0 0 0
85 0 my $hsize = sprintf '%s%s', $size, $unit;
86 0 my $len = sprintf '%.6f%s', $size / $n, $unit;
87  
88 0 foreach my $tex_line (
89 0 ( map { "% QR code coding: $_" } @log ),
90 "\\long\\gdef\\$tag\{%",
91 "\\begin{textblock*}{$hsize}($hpos,$vpos)",
92 "\\let\\arrayrulewidthSav\\relax", # TeX stack exchange 184991
93 "\\newlength{\\arrayrulewidthSav}",
94 "\\setlength{\\arrayrulewidthSav}{\\arrayrulewidth}",
95 "\\setlength{\\arrayrulewidth}{$len}",
96 "\\setlength{\\tabcolsep}{0pt}",
97 "\\setlength{\\extrarowheight}{0pt}",
98 "\\newcolumntype{U}{>{\\centering\\arraybackslash }b{$len}}",
99 "\\begin{tabular}{" . ( 'U' x $n ) . '}',
100 0 ( map { "\\hhline{" . hhline_content($_) . "}" } @{$aoa} ),
101 ( '&' x ( $n - 1 ) ) . '\\\\',
102 "\\end{tabular}",
103 "\\setlength{\\arrayrulewidth}{\\arrayrulewidthSav}",
104 "\\end{textblock*}",
105 '}',
106 )
107 {
108 0 oEdtk::Main::oe_print_to_output_file("$tex_line\n");
109 }
110 0 return $self;
111 }
112  
113 0 ▶sub line_break {
Subroutine line_break: not called CC=1 SCAR=6.9
Pod documented
114 0 return "%\n";
115 }
116  
117 21 my $_CFG = config_read();
118  
119 # OPEN THE DICTIONARY STATICALLY TO AVOID MULTIPLE ACCESSES TO THE CORRESPONDING FILE
120 21 my $_DICO_TEX_CHAR = oEdtk::Dict->new( $_CFG->{'EDTK_DICO_XLAT'}, { section => 'LATEX' } );
121  
122 # http://woufeil.developpez.com/tutoriels/perl/poo/
123 0 ▶sub escape {
Subroutine escape: not called CC=5 SCAR=34.0
Pod documented
124 0 my $str = shift;
125  
126 # ESCAPE SPECIAL CARACTERS FOR TEXTAGS
127  
128 # Deal with backslashes and curly braces first and at the same
129 # time, because escaping backslashes introduces curly braces, and,
130 # inversely, escaping curly braces introduces backslashes.
131 # see http://detexify.kirelabs.org/classify.html
132 0 my $new = '';
133 0 foreach my $s ( split( /([{}\\])/, $str ) ) {
134 0 ▶ if ( $s eq "{" ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($s eq '{') { } 0 0 0
elsif ($s eq '}') { } 0 0 0
elsif ($s eq '\\') { } 0 0 0
135 0 $new .= "\\textbraceleft{}";
136 }
137 elsif ( $s eq "}" ) {
138 0 $new .= "\\textbraceright{}";
139 }
140 elsif ( $s eq "\\" ) {
141 0 $new .= "\\textbackslash{}";
142 }
143 else {
144 0 $new .= $s;
145 }
146 }
147 0 $new =~ s/([%&\$_#])/\\$1/g;
148  
149 # warn "DEBUG : \$_DICO_TEX_CHAR = $_DICO_TEX_CHAR\n";
150 0 $new = $_DICO_TEX_CHAR->substitue($new);
151  
152 # \\"{} => CAUSES TEX ERRORS DURING THE INDEXING PROCESS (FOR DB INSERTION)
153 0 $new =~ s/\\\"\{\}/\\textquotestraightdblbase{}/g;
154 0 $new =~ s/\\\"/\\textquotestraightdblbase{}/g;
155  
156 # 01...@A...yz{}|~ 1°
157 0 return $new;
158 }
159  
160 21 1;
161  
162 =head1 NAME
163  
164 oEdtk::TexDoc - concrete LaTeX document emitter
165  
166 =head1 SYNOPSIS
167  
168 use oEdtk::TexDoc;
169  
170 my $doc = oEdtk::TexDoc->new;
171 $doc->append('NOM', 'value'); # tag built by mktag -> oEdtk::TexTag
172 $doc->append_qr_code('qrcode', 'text'); # QR code written to the output file
173 print $doc->escape('50% & more');
174  
175 =head1 DESCRIPTION
176  
177 The concrete C<oEdtk::Doc> subclass that produces LaTeX output. It builds
178 C<oEdtk::TexTag> objects for each tag, escapes the special LaTeX characters
179 (through the C<LATEX> dictionary), and can render a QR code as a LaTeX
180 C<textblock*>.
181  
182 =head1 METHODS
183  
184 =head2 mktag
185  
186 my $tag = $doc->mktag($name, $value);
187  
188 Return a new C<oEdtk::TexTag> for C<$name> and C<$value> (overrides the
189 abstract L<oEdtk::Doc/mktag>).
190  
191 =head2 append_table
192  
193 $doc->append_table($name, @values);
194  
195 Append C<$name> with an array reference of C<@values>, producing a LaTeX list
196 macro. Returns the object.
197  
198 =head2 append_qr_code
199  
200 $doc->append_qr_code($tag, $obj, $level, $size, $unit, $hpos, $vpos);
201  
202 Render C<$obj> (a scalar, or a reference serialised to YAML) as a QR code
203 written to the main output file as a LaTeX C<textblock*>. C<$tag> must be a
204 lowercase macro name; the geometry arguments default to the module's C<Readonly>
205 constants. For each line it calls C<oEdtk::Main::oe_print_to_output_file>.
206  
207 C<Text::QRCode> is an optional dependency (it builds against the native
208 C<libqrencode> library): it is loaded on first use and the method croaks with an
209 explicit message when it is not installed.
210  
211 =head2 line_break
212  
213 my $break = $doc->line_break;
214  
215 Return the LaTeX line break C<"%\n">.
216  
217 =head2 escape
218  
219 my $escaped = $doc->escape($str);
220  
221 Escape the characters that are special in LaTeX (backslash, braces, and
222 C<% & $ _ #>), then apply the C<LATEX> dictionary substitution.
223  
224 The private helpers C<hhline_content> and C<is_in_list> back
225 L</append_qr_code>; they are not part of the public API.
226  
227 =head1 AUTHOR
228  
229 The oEdtk team.
230  
231 =cut
« blib/lib/oEdtk/RecordParser.pm ↑ Summary blib/lib/oEdtk/tuiEdtk.pm »