lib/oEdtk/TexTag.pm

filter: Statementstmt 26.1% 17 / 65 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 15 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 57.1% 4 / 7 Podpod 66.6% 2 / 3 totaltotal 19.1% 23 / 120 CC 19 SCAR 39.9
CC
19
Cov
17%
emit
90.0
new
56.0
escape_0
30.0
CRAP 54.2
« blib/lib/oEdtk/Tracking.pm ↑ Summary blib/lib/oEdtk/Restitution.pm »
1 package oEdtk::TexTag;
2  
3 21 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 21 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 21 our $VERSION = '2.1126';
7 21 my $_FLAG_DIGITS = 1;
8  
9 # A SIMPLE OBJECT THAT DESCRIBES A TEX TAG.
10 0 ▶sub new {
Subroutine new: not called CC=7 SCAR=40.3
Pod documented
11 0 my ( $class, $name, $val ) = @_;
12  
13 0 my $ref = ref($val);
14 0 ▶ if ( $ref ne '' && $ref ne 'ARRAY' && $ref ne 'HASH' && $ref ne 'oEdtk::TexDoc' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref ne "" and $ref ne "ARRAY" and $ref ne "HASH" and $ref ne "oEdtk::TexDoc") 0 0 0
Condition0%0/9
$ref ne "" and $ref ne "ARRAY" and $ref ne "HASH" and $ref ne "oEdtk::TexDoc"
!ll&&!rl&&r
000
$ref ne "" and $ref ne "ARRAY" and $ref ne "HASH"
!ll&&!rl&&r
000
$ref ne "" and $ref ne "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/4
$ref ne "" and $ref ne "ARRAY" and $ref ne "HASH" and $ref ne "oEdtk::TexDoc"
$ref ne "" $ref ne "ARRAY" $ref ne "HASH" $ref ne "oEdtk::TexDoc"
Truth table0%0/5
A and B and C and D
A= $ref ne ""B= $ref ne "ARRAY"C= $ref ne "HASH"D= $ref ne "oEdtk::TexDoc"
ABCDresult
0XXX0
10XX0
110X0
11100
11111
15 0 die "ERROR: Unexpected value type, must be a scalar or an oEdtk::TexDoc object\n";
16 }
17  
18 0 ▶ if ( $name =~ /\d/ && $_FLAG_DIGITS ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name =~ /\d/ and $_FLAG_DIGITS) 0 0 0
Condition0%0/3
$name =~ /\d/ and $_FLAG_DIGITS
!ll&&!rl&&r
000
MC/DC0%0/2
$name =~ /\d/ and $_FLAG_DIGITS
$name =~ /\d/ $_FLAG_DIGITS
Truth table0%0/3
A and B
A= $name =~ /\d/B= $_FLAG_DIGITS
ABresult
0X0
100
111
19 0 warn "INFO: Tex Tag name cannot contain digits : $name !\n";
20 0 warn "INFO: further messages for Tex Tag containing digits will be ignored\n";
21 0 $_FLAG_DIGITS = 0;
22 }
23  
24 0 my $self = {
25 name => $name,
26 value => $val
27 };
28 0 bless $self, $class;
29  
30 # warn "INFO: TexTag object $self created...\n";
31 0 return $self;
32 }
33  
34 0 ▶sub emit {
Subroutine emit: not called CC=9 SCAR=45.0
Pod documented
35 0 my ($self) = @_;
36  
37 0 ▶ if ( defined $self->{'name'} && $self->{'name'} =~ /^_include_$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"name"} and $self->{"name"} =~ /^_include_$/) 0 0 0
Condition0%0/3
defined $self->{"name"} and $self->{"name"} =~ /^_include_$/
!ll&&!rl&&r
000
MC/DC0%0/2
defined $self->{"name"} and $self->{"name"} =~ /^_include_$/
defined $self->{"name"} $self->{"name"} =~ /^_include_$/
Truth table0%0/3
A and B
A= defined $self->{"name"}B= $self->{"name"} =~ /^_include_$/
ABresult
0X0
100
111
38 0 return "\\input{" . $self->{'value'} . "\}";
39 }
40  
41 # A tag containing a scalar value or an HASH/ARRAY/TexDoc object.
42 0 ▶ if ( defined $self->{'value'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"value"}) 0 0 0
43 0 my $ref = ref( $self->{'value'} );
44 0 my $name = $self->{'name'};
45  
46 # A list of values.
47 0 ▶ if ( $ref eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref eq "ARRAY") 0 0 0
48 0 my $macro = "\\edListNew{$self->{'name'}}";
49 0 foreach ( @{ $self->{'value'} } ) {
50  
51 # my $val = escape($_);
52 0 my $val = oEdtk::TexDoc::escape($_); # NOT CLEAN !
53  
54 # warn "INFO: call to oEdtk::Doc::escape in TexTag $val\n";
55 0 $macro .= "\\edListAdd{$self->{'name'}}{$val}";
56 }
57 0 return $macro;
58 }
59  
60 # A tag containing other tags.
61 0 my $value = $self->{'value'};
62 0 ▶ if ( $ref eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref eq "HASH") 0 0 0
63 0 my $inner = oEdtk::TexDoc->new();
64 0 ▶ while ( my ( $key, $val ) = each %{ $self->{'value'} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$self{"value"};}) 0 0 0
65 0 $inner->append( $key, $val );
66 }
67 0 $value = $inner;
68 }
69  
70 # Escape if we have a scalar value.
71 0 ▶ if ( ref($value) eq '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value eq "") 0 0 0
72 0 $value =~ s/\s+/ /g;
73  
74 # $value = escape($value);
75 0 $value = oEdtk::TexDoc::escape($value);
76  
77 # warn "INFO: call to oEdtk::Doc::escape in TexTag $value\n";
78 }
79  
80 0 return "\\long\\gdef\\$name\{$value\}";
81 }
82  
83 # A command call.
84 0 return "\\$self->{'name'}";
85 }
86  
87 21 ▶use oEdtk::Config qw(config_read);
Subroutine BEGIN: called CC=1 SCAR=0.0
88 21 ▶use oEdtk::Dict;
Subroutine BEGIN: called CC=1 SCAR=0.0
89 21 my $_CFG = config_read();
90  
91 # OPEN THE DICTIONARY STATICALLY TO AVOID MULTIPLE ACCESSES TO THE CORRESPONDING FILE
92 21 my $_DICO_CHAR = oEdtk::Dict->new( $_CFG->{'EDTK_DICO_XLAT'}, { section => 'LATEX' } );
93  
94 # http://woufeil.developpez.com/tutoriels/perl/poo/
95 0 ▶sub escape_0 { # NOT CLEAN use inheritance! TexTag->TexDoc
Subroutine escape_0: not called CC=5 SCAR=34.0
Pod undocumented
96 0 my $str = shift;
97  
98 # ESCAPE SPECIAL CARACTERS FOR TEXTAGS
99  
100 # Deal with backslashes and curly braces first and at the same
101 # time, because escaping backslashes introduces curly braces, and,
102 # inversely, escaping curly braces introduces backslashes.
103 # see http://detexify.kirelabs.org/classify.html
104 0 my $new = '';
105 0 foreach my $s ( split( /([{}\\])/, $str ) ) {
106 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
107 0 $new .= "\\textbraceleft{}";
108 }
109 elsif ( $s eq "}" ) {
110 0 $new .= "\\textbraceright{}";
111 }
112 elsif ( $s eq "\\" ) {
113 0 $new .= "\\textbackslash{}";
114 }
115 else {
116 0 $new .= $s;
117 }
118 }
119 0 $new =~ s/([%&\$_#])/\\$1/g;
120  
121 # warn "INFO: \$_DICO_CHAR = $_DICO_CHAR\n";
122 # xxx the following line causes an error after program end
123 # $new = $_DICO_CHAR->substitue($new);
124  
125 # \\"{} => CAUSES TEX ERRORS DURING THE INDEXING PROCESS (FOR DB INSERTION)
126 0 $new =~ s/\\\"\{\}/\\textquotestraightdblbase{}/g;
127 0 $new =~ s/\\\"/\\textquotestraightdblbase{}/g;
128  
129 # 01...@A...yz{}|~ 1°
130 0 return $new;
131 }
132  
133 #END {
134 # undef $_DICO_CHAR;
135 # undef $_CFG;
136 # warn "INFO: TexTag object deleted!\n";
137 #}
138  
139 21 1;
140  
141 =head1 NAME
142  
143 oEdtk::TexTag - a single LaTeX tag
144  
145 =head1 SYNOPSIS
146  
147 use oEdtk::TexTag;
148  
149 my $tag = oEdtk::TexTag->new('NOM', 'value');
150 print $tag->emit; # \long\gdef\NOM{value}
151  
152 =head1 DESCRIPTION
153  
154 A small object describing one LaTeX tag: a name and a value. The value may be a
155 scalar, an array reference (emitted as a list macro), a hash reference (emitted
156 as nested tags), or an C<oEdtk::TexDoc> object.
157  
158 =head1 METHODS
159  
160 =head2 new
161  
162 my $tag = oEdtk::TexTag->new($name, $val);
163  
164 Build a tag of name C<$name> and value C<$val>. A value that is not a scalar,
165 an array, a hash or an C<oEdtk::TexDoc> is an error; a name containing digits
166 raises a one-time warning.
167  
168 =head2 emit
169  
170 my $latex = $tag->emit;
171  
172 Return the LaTeX rendering: an C<\input{}> for an C<_include_> tag, a list
173 macro for an array value, a C<\long\gdef\name{...}> for a scalar or nested
174 value, or a bare C<\name> command when there is no value.
175  
176 The C<escape_0> sub is a deprecated, not-clean duplicate of
177 L<oEdtk::TexDoc/escape>; it is kept for reference only and is not part of the
178 public API.
179  
180 =head1 AUTHOR
181  
182 The oEdtk team.
183  
184 =cut
« blib/lib/oEdtk/Tracking.pm ↑ Summary blib/lib/oEdtk/Restitution.pm »