| 1 | package oEdtk::TexTag; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use utf8; | ||
| 5 | |||
| 6 | our $VERSION = '2.1129'; | ||
| 7 | my $_FLAG_DIGITS = 1; | ||
| 8 | |||
| 9 | # A SIMPLE OBJECT THAT DESCRIBES A TEX TAG. | ||
| 10 | sub new { | ||
| 11 | my ( $class, $name, $val ) = @_; | ||
| 12 | |||
| 13 | my $ref = ref($val); | ||
| 14 | if ( $ref ne '' && $ref ne 'ARRAY' && $ref ne 'HASH' && $ref ne 'oEdtk::TexDoc' ) { | ||
| 15 | die "ERROR: Unexpected value type, must be a scalar or an oEdtk::TexDoc object\n"; | ||
| 16 | } | ||
| 17 | |||
| 18 | if ( $name =~ /\d/ && $_FLAG_DIGITS ) { | ||
| 19 | warn "INFO: Tex Tag name cannot contain digits : $name !\n"; | ||
| 20 | warn "INFO: further messages for Tex Tag containing digits will be ignored\n"; | ||
| 21 | $_FLAG_DIGITS = 0; | ||
| 22 | } | ||
| 23 | |||
| 24 | my $self = { | ||
| 25 | name => $name, | ||
| 26 | value => $val | ||
| 27 | }; | ||
| 28 | bless $self, $class; | ||
| 29 | |||
| 30 | # warn "INFO: TexTag object $self created...\n"; | ||
| 31 | return $self; | ||
| 32 | } | ||
| 33 | |||
| 34 | sub emit { | ||
| 35 | my ($self) = @_; | ||
| 36 | |||
| 37 | if ( defined $self->{'name'} && $self->{'name'} =~ /^_include_$/ ) { | ||
| 38 | return "\\input{" . $self->{'value'} . "\}"; | ||
| 39 | } | ||
| 40 | |||
| 41 | # A tag containing a scalar value or an HASH/ARRAY/TexDoc object. | ||
| 42 | if ( defined $self->{'value'} ) { | ||
| 43 | my $ref = ref( $self->{'value'} ); | ||
| 44 | my $name = $self->{'name'}; | ||
| 45 | |||
| 46 | # A list of values. | ||
| 47 | if ( $ref eq 'ARRAY' ) { | ||
| 48 | my $macro = "\\edListNew{$self->{'name'}}"; | ||
| 49 | foreach ( @{ $self->{'value'} } ) { | ||
| 50 | |||
| 51 | # my $val = escape($_); | ||
| 52 | my $val = oEdtk::TexDoc::escape($_); # NOT CLEAN ! | ||
| 53 | |||
| 54 | # warn "INFO: call to oEdtk::Doc::escape in TexTag $val\n"; | ||
| 55 | $macro .= "\\edListAdd{$self->{'name'}}{$val}"; | ||
| 56 | } | ||
| 57 | return $macro; | ||
| 58 | } | ||
| 59 | |||
| 60 | # A tag containing other tags. | ||
| 61 | my $value = $self->{'value'}; | ||
| 62 | if ( $ref eq 'HASH' ) { | ||
| 63 | my $inner = oEdtk::TexDoc->new(); | ||
| 64 | while ( my ( $key, $val ) = each %{ $self->{'value'} } ) { | ||
| 65 | $inner->append( $key, $val ); | ||
| 66 | } | ||
| 67 | $value = $inner; | ||
| 68 | } | ||
| 69 | |||
| 70 | # Escape if we have a scalar value. | ||
| 71 | if ( ref($value) eq '' ) { | ||
| 72 | $value =~ s/\s+/ /g; | ||
| 73 | |||
| 74 | # $value = escape($value); | ||
| 75 | $value = oEdtk::TexDoc::escape($value); | ||
| 76 | |||
| 77 | # warn "INFO: call to oEdtk::Doc::escape in TexTag $value\n"; | ||
| 78 | } | ||
| 79 | |||
| 80 | return "\\long\\gdef\\$name\{$value\}"; | ||
| 81 | } | ||
| 82 | |||
| 83 | # A command call. | ||
| 84 | return "\\$self->{'name'}"; | ||
| 85 | } | ||
| 86 | |||
| 87 | use oEdtk::Config qw(config_read); | ||
| 88 | use oEdtk::Dict; | ||
| 89 | my $_CFG = config_read(); | ||
| 90 | |||
| 91 | # OPEN THE DICTIONARY STATICALLY TO AVOID MULTIPLE ACCESSES TO THE CORRESPONDING FILE | ||
| 92 | my $_DICO_CHAR = oEdtk::Dict->new( $_CFG->{'EDTK_DICO_XLAT'}, { section => 'LATEX' } ); | ||
| 93 | |||
| 94 | # http://woufeil.developpez.com/tutoriels/perl/poo/ | ||
| 95 | sub escape_0 { # NOT CLEAN use inheritance! TexTag->TexDoc | ||
| 96 | 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 | my $new = ''; | ||
| 105 | foreach my $s ( split( /([{}\\])/, $str ) ) { | ||
| 106 | if ( $s eq "{" ) { | ||
| 107 | $new .= "\\textbraceleft{}"; | ||
| 108 | } | ||
| 109 | elsif ( $s eq "}" ) { | ||
| 110 | $new .= "\\textbraceright{}"; | ||
| 111 | } | ||
| 112 | elsif ( $s eq "\\" ) { | ||
| 113 | $new .= "\\textbackslash{}"; | ||
| 114 | } | ||
| 115 | else { | ||
| 116 | $new .= $s; | ||
| 117 | } | ||
| 118 | } | ||
| 119 | $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 | $new =~ s/\\\"\{\}/\\textquotestraightdblbase{}/g; | ||
| 127 | $new =~ s/\\\"/\\textquotestraightdblbase{}/g; | ||
| 128 | |||
| 129 | # 01...@A...yz{}|~ 1° | ||
| 130 | return $new; | ||
| 131 | } | ||
| 132 | |||
| 133 | #END { | ||
| 134 | # undef $_DICO_CHAR; | ||
| 135 | # undef $_CFG; | ||
| 136 | # warn "INFO: TexTag object deleted!\n"; | ||
| 137 | #} | ||
| 138 | |||
| 139 | 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 |