lib/oEdtk/TexDoc.pm

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