blib/lib/oEdtk/TexDoc.pm untested

filter: Statementstmt 0.0% 0 / 110 Branchbran 0.0% 0 / 20 Conditioncond 0.0% 0 / 17 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 0.0% 0 / 22 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 187 CC 11 SCAR 25.1
CC
11
Cov
0%
append_qr_code
42.0
escape
20.0
hhline_content
6.0
CRAP 12.2
1 package oEdtk::TexDoc;
2 use strict;
3 use utf8;
4  
5 our $VERSION = 2.1129;
6  
7 use base 'oEdtk::Doc';
8 use oEdtk::Config qw(config_read);
9 use oEdtk::Dict;
10 use oEdtk::TexTag;
11 use Carp qw(croak);
12 use Data::Dumper qw(Dumper);
13 use JSON::PP qw(encode_json);
14 use YAML::Tiny;
15 use Readonly;
16  
17 use strict;
18 use warnings;
19  
20 # QR code geometry defaults.
21 Readonly my $DEFAULT_QR_CODE_LEVEL => 'H'; # error-correction level: L M Q H
22 Readonly my $DEFAULT_QR_CODE_UNIT => 'mm';
23 Readonly my $DEFAULT_QR_CODE_SIZE => 25;
24 Readonly my $PAGE_WIDTH_IN_MM => 210;
25 Readonly my $RIGHT_MARGIN_IN_MM => 10;
26 Readonly my $TOP_MARGIN_IN_MM => 10;
27 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 Readonly my $DEFAULT_QR_CODE_VPOS => sprintf '%f%s', $TOP_MARGIN_IN_MM, $DEFAULT_QR_CODE_UNIT;
31  
32 sub mktag {
33 my ( $self, $name, $value ) = @_;
34  
35 return oEdtk::TexTag->new( $name, $value );
36 }
37  
38 sub append_table {
39 my ( $self, $name, @values ) = @_;
40  
41 $self->append( $name, \@values );
42 return $self;
43 }
44  
45 sub hhline_content {
46 my ($row) = @_;
47 return join q{}, map { $_ eq '*' ? '-' : '~' } @{$row};
48 }
49  
50 sub is_in_list {
51 my ( $item, @l ) = @_;
52 foreach my $v (@l) {
53 return 1 if $item eq $v;
54 }
55 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 sub append_qr_code {
62 my ( $self, $tag, $obj, $level, $size, $unit, $hpos, $vpos ) = @_;
63 require oEdtk::Main; # lazy: oEdtk::Main itself requires this module
64 croak "Bad tag name '$tag'" if $tag =~ m{[^a-z]}six;
65 croak 'Bad object: ' . Dumper($obj)
66 if !is_in_list( ( ref $obj ), q{}, 'ARRAY', 'HASH' );
67 $level //= $DEFAULT_QR_CODE_LEVEL;
68 $size //= $DEFAULT_QR_CODE_SIZE;
69 $unit //= $DEFAULT_QR_CODE_UNIT;
70 $hpos //= $DEFAULT_QR_CODE_HPOS;
71 $vpos //= $DEFAULT_QR_CODE_VPOS;
72  
73 my $string = ( ref $obj ) ? YAML::Tiny::Dump($obj) : $obj;
74 my @log = ( encode_json( [$string] ) );
75 unshift @log, encode_json($obj) if ref $obj;
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 eval { require Text::QRCode; 1 }
80 or croak
81 'append_qr_code requires the optional Text::QRCode module (which needs libqrencode)';
82 my $aoa = Text::QRCode->new( level => $level )->plot($string);
83 my $n = scalar @{$aoa};
84 die 'Empty QR code' if !$n;
85 my $hsize = sprintf '%s%s', $size, $unit;
86 my $len = sprintf '%.6f%s', $size / $n, $unit;
87  
88 foreach my $tex_line (
89 ( 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 ( map { "\\hhline{" . hhline_content($_) . "}" } @{$aoa} ),
101 ( '&' x ( $n - 1 ) ) . '\\\\',
102 "\\end{tabular}",
103 "\\setlength{\\arrayrulewidth}{\\arrayrulewidthSav}",
104 "\\end{textblock*}",
105 '}',
106 )
107 {
108 oEdtk::Main::oe_print_to_output_file("$tex_line\n");
109 }
110 return $self;
111 }
112  
113 sub line_break {
114 return "%\n";
115 }
116  
117 my $_CFG = config_read();
118  
119 # OPEN THE DICTIONARY STATICALLY TO AVOID MULTIPLE ACCESSES TO THE CORRESPONDING FILE
120 my $_DICO_TEX_CHAR = oEdtk::Dict->new( $_CFG->{'EDTK_DICO_XLAT'}, { section => 'LATEX' } );
121  
122 # http://woufeil.developpez.com/tutoriels/perl/poo/
123 sub escape {
124 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 my $new = '';
133 foreach my $s ( split( /([{}\\])/, $str ) ) {
134 if ( $s eq "{" ) {
135 $new .= "\\textbraceleft{}";
136 }
137 elsif ( $s eq "}" ) {
138 $new .= "\\textbraceright{}";
139 }
140 elsif ( $s eq "\\" ) {
141 $new .= "\\textbackslash{}";
142 }
143 else {
144 $new .= $s;
145 }
146 }
147 $new =~ s/([%&\$_#])/\\$1/g;
148  
149 # warn "DEBUG : \$_DICO_TEX_CHAR = $_DICO_TEX_CHAR\n";
150 $new = $_DICO_TEX_CHAR->substitue($new);
151  
152 # \\"{} => CAUSES TEX ERRORS DURING THE INDEXING PROCESS (FOR DB INSERTION)
153 $new =~ s/\\\"\{\}/\\textquotestraightdblbase{}/g;
154 $new =~ s/\\\"/\\textquotestraightdblbase{}/g;
155  
156 # 01...@A...yz{}|~ 1°
157 return $new;
158 }
159  
160 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