Decode.pm

filter: Statementstmt 81.6% 71 / 87 Branchbran 61.5% 16 / 26 Conditioncond 33.3% 2 / 6 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 100% 10 / 10 Podpod 100% 1 / 1 totaltotal 75.1% 100 / 133 CC 16 SCAR 23.1
CC
16
Cov
75%
latex_decode
17.6
_get_mac
3.8
_get_diac
2.1
CRAP 10.0
↑ Summary Decode/Data.pm »
1 package LaTeX::Decode;
2  
3 3 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 =encoding utf-8
9  
10 =head1 NAME
11  
12 LaTeX::Decode - Decode from LaTeX to Unicode
13  
14 =head1 VERSION
15  
16 Version 0.05
17  
18 =cut
19  
20 3 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
21 3 our $VERSION = '0.05';
22 3 our @EXPORT = qw(latex_decode);
23 3 ▶use LaTeX::Decode::Data;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 3 ▶use Unicode::Normalize;
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 =head1 SYNOPSIS
27  
28 use LaTeX::Decode;
29  
30 my $latex_string = 'Mu\\d{h}ammad ibn M\\=us\=a al-Khw\\=arizm\\={\\i}';
31 my $new_string = latex_decode($latex_string); # => 'Muḥammad ibn Mūsā al-Khwārizmī'
32  
33 =head1 DESCRIPTION
34  
35 =head1 EXPORT
36  
37 =head1 FUNCTIONS
38  
39 =head2 latex_decode($text, %options)
40  
41 Decodes the given text from LaTeX to Unicode.
42  
43 The function accepts a number of options:
44  
45 * normalize => $bool (default 0)
46 whether the output string should be normalized with Unicode::Normalize
47  
48 * normalization => <normalization form> (default 'NFC')
49 and if yes, the normalization form to use (see the Unicode::Normalize documentation)
50  
51 * strip_outer_braces => $bool (default 0)
52 whether the outer curly braces around letters+combining marks should be
53 stripped off. By default "fut{\\'e}" becomes fut{é}, to prevent something
54 like '\\textuppercase{\\'e}' to become '\\textuppercaseé'. Setting this option to
55 TRUE can be useful for instance when converting BibTeX files.
56  
57 =head1 GLOBAL OPTIONS
58  
59 The decoding scheme can be set with
60 $LaTeX::Decode::DefaultScheme = '<name>';
61 Possible values are 'base', 'extra' and 'full'; default value is 'extra'.
62  
63 TODO : explain these scheme!
64  
65 base => Most common macros and diacritics (sufficient for Western languages
66 and common symbols)
67  
68 extra => Also converts punctuation, larger range of diacritics and macros (e.g. for IPA, Latin Extended
69 Additional, etc.)
70  
71 full => Also converts symbols, Greek letters, dingbats, negated symbols, and
72 superscript characters and symbols ...
73  
74 =cut
75  
76 3 our $DefaultScheme = 'extra';
77  
78 3 ▶sub _get_diac {
Subroutine _get_diac: called CC=2 SCAR=7.6
79 3 my $scheme = shift;
80 3 ▶ if ( $scheme eq 'base' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($scheme eq 'base') { } 0 3 3
81 0 return %DIACRITICS;
82 }
83 else {
84 3 return ( %DIACRITICS, %DIACRITICSEXTRA );
85 }
86 }
87  
88 3 ▶sub _get_mac {
Subroutine _get_mac: called CC=3 SCAR=13.3
89 3 my $scheme = shift;
90 3 ▶ if ( $scheme eq 'base' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($scheme eq 'base') { } 0 3 3
elsif ($scheme eq 'full') { } 0 3 3
91 0 return %WORDMACROS;
92 }
93 elsif ( $scheme eq 'full' ) {
94 0 return ( %WORDMACROS, %WORDMACROSEXTRA, %PUNCTUATION, %SYMBOLS,
95 %GREEK );
96 }
97 else {
98 3 return ( %WORDMACROS, %WORDMACROSEXTRA, %PUNCTUATION );
99 }
100 }
101  
102 3 ▶sub latex_decode {
Subroutine latex_decode: called CC=13 SCAR=28.7
Pod documented
103 3 my $text = shift;
104 3 my %opts = @_;
105 3 ▶ my $norm = exists $opts{normalize} ? $opts{normalize} : 1;
Branch: 1 branch
BranchTrueFalseTotal
exists $opts{'normalize'} ? : 0 3 3
106 3 ▶ my $norm_form = exists $opts{normalization} ? $opts{normalization} : 'NFC';
Branch: 1 branch
BranchTrueFalseTotal
exists $opts{'normalization'} ? : 0 3 3
107 3 ▶ my $scheme = exists $opts{scheme} ? $opts{scheme} : $DefaultScheme;
Branch: 1 branch
BranchTrueFalseTotal
exists $opts{'scheme'} ? : 0 3 3
108 3 ▶ croak "invalid scheme name '$scheme'"
Branch: 1 branch
BranchTrueFalseTotal
unless $scheme eq "full" or $scheme eq "base" or $scheme eq "extra" 0 3 3
Condition33%2/6
$scheme eq "full" or $scheme eq "base" or $scheme eq "extra"
l!l&&r!l&&!r
030
$scheme eq "full" or $scheme eq "base"
l!l&&r!l&&!r
003
MC/DC0%0/3
$scheme eq "full" or $scheme eq "base" or $scheme eq "extra"
$scheme eq "full" $scheme eq "base" $scheme eq "extra"
Truth table25%1/4
A or B or C
A= $scheme eq "full"B= $scheme eq "base"C= $scheme eq "extra"
ABCresult
1XX1
01X1
0011
0000
109 unless ( $scheme eq 'full' or $scheme eq 'base' or $scheme eq 'extra' );
110 my $strip_outer_braces =
111 3 ▶ exists $opts{strip_outer_braces} ? $opts{strip_outer_braces} : 0;
Branch: 1 branch
BranchTrueFalseTotal
exists $opts{'strip_outer_braces'} ? : 1 2 3
112  
113 3 my %DIAC = _get_diac($scheme);
114 3 my %WORDMAC = _get_mac($scheme);
115  
116 # a regex with all possible word macros
117 my $WORDMAC_RE =
118 3 join( '|', sort { length $b <=> length $a } keys %WORDMAC );
119 3 $WORDMAC_RE = qr{$WORDMAC_RE};
120  
121 3 my $DIAC_RE;
122 3 ▶ if ( $scheme eq 'base' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($scheme eq 'base') { } 0 3 3
123 0 $DIAC_RE = $DIAC_RE_BASE;
124 }
125 else {
126 3 $DIAC_RE = $DIAC_RE_EXTRA;
127 }
128  
129 3 ▶ if ( $scheme eq 'full' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($scheme eq "full") 0 3 3
130 0 $text =~ s/\\not\\($NEG_SYMB_RE)/$NEGATEDSYMBOLS{$1}/ge;
131 0 $text =~ s/\\textsuperscript\{($SUPER_RE)\}/$SUPERSCRIPTS{$1}/ge;
132 0 $text =~ s/\\textsuperscript\{\\($SUPERCMD_RE)\}/$CMDSUPERSCRIPTS{$1}/ge;
133 0 $text =~ s/\\dings\{([2-9AF][0-9A-F])\}/$DINGS{$1}/ge;
134 }
135  
136 3 $text =~ s/(\\[a-zA-Z]+)\\(\s+)/$1\{\}$2/g; # \foo\ bar -> \foo{} bar
137 3 $text =~ s/([^{]\\\w)([;,.:%])/$1\{\}$2/g; #} Aaaa\o, -> Aaaa\o{},
138 3 $text =~ s/(\\(?:$DIAC_RE_BASE|$ACCENTS_RE))\{\\i\}/$1\{i\}/g;
139 # special cases such as '\={\i}' -> '\={i}' -> "i\x{304}"
140  
141 ## remove {} around macros that print one character
142 ## by default we skip that, as it would break constructions like \foo{\i}
143 3 ▶ if ($strip_outer_braces) {
Branch: 1 branch
BranchTrueFalseTotal
if ($strip_outer_braces) 1 2 3
144 0 $text =~ s/ \{\\($WORDMAC_RE)\} / $WORDMAC{$1} /gxe;
145 }
146 3 $text =~ s/ \\($WORDMAC_RE)(?: \{\} | \s+ | \b) / $WORDMAC{$1} /gxe;
147  
148 3 $text =~ s/\\($ACCENTS_RE)\{(\p{L}\p{M}*)\}/$2 . $ACCENTS{$1}/ge;
149  
150 3 $text =~ s/\\($ACCENTS_RE)(\p{L}\p{M}*)/$2 . $ACCENTS{$1}/ge;
151  
152 3 $text =~ s/\\($DIAC_RE)\s*\{(\p{L}\p{M}*)\}/$2 . $DIAC{$1}/ge;
153  
154 3 $text =~ s/\\($DIAC_RE)\s+(\p{L}\p{M}*)/$2 . $DIAC{$1}/ge;
155  
156 3 $text =~ s/\\($ACCENTS_RE)\{(\p{L}\p{M}*)\}/$2 . $ACCENTS{$1}/ge;
157  
158 0 $text =~ s/\\($ACCENTS_RE)(\p{L}\p{M}*)/$2 . $ACCENTS{$1}/ge;
159  
160 3 $text =~ s/\\($DIAC_RE)\s*\{(\p{L}\p{M}*)\}/$2 . $DIAC{$1}/ge;
161  
162 0 $text =~ s/\\($DIAC_RE)\s+(\p{L}\p{M}*)/$2 . $DIAC{$1}/ge;
163  
164 ## remove {} around letter+combining mark(s)
165 ## by default we skip that, as it would destroy constructions like \foo{\`e}
166 3 ▶ if ($strip_outer_braces) {
Branch: 1 branch
BranchTrueFalseTotal
if ($strip_outer_braces) 1 2 3
167 1 $text =~ s/{(\PM\pM+)}/$1/g;
168 }
169  
170 3 ▶ if ($norm) {
Branch: 1 branch
BranchTrueFalseTotal
if ($norm) { } 3 0 3
171 3 return Unicode::Normalize::normalize( $norm_form, $text );
172 }
173 else {
174 0 return $text;
175 }
176 }
177  
178 =head1 AUTHOR
179  
180 François Charette, C<< <firmicus@cpan.org> >>
181  
182 =head1 BUGS
183  
184 Please report any bugs or feature requests to C<bug-latex-decode at
185 rt.cpan.org>, or through the web interface at
186 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=LaTeX-Decode>. I will be
187 notified, and then you'll automatically be notified of progress on your bug as
188 I make changes.
189  
190 =head1 COPYRIGHT & LICENSE
191  
192 Copyright 2009-2015 François Charette, all rights reserved.
193  
194 This module is free software. You can redistribute it and/or
195 modify it under the terms of the Artistic License 2.0.
196  
197 This program is distributed in the hope that it will be useful,
198 but without any warranty; without even the implied warranty of
199 merchantability or fitness for a particular purpose.
200  
201 =cut
202  
203 3 1;
204  
205 # vim: set tabstop=4 shiftwidth=4 expandtab:
↑ Summary Decode/Data.pm »