blib/lib/LaTeX/Decode.pm

filter: Statementstmt 82.7% 72 / 87 Branchbran 65.3% 17 / 26 Conditioncond 33.3% 2 / 6 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 100% 10 / 10 Podpod 100% 1 / 1 totaltotal 76.6% 102 / 133 SCAR 29.6
CC
16
Cov
76%
latex_decode
16.5
_get_mac
3.8
_get_diac
2.1
CRAP 19.3
↑ Summary Build »
1 package LaTeX::Decode;
2  
3 6 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 6 ▶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.03
17  
18 =cut
19  
20 6 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
21 6 our $VERSION = '0.03';
22 6 our @EXPORT = qw(latex_decode);
23 6 ▶use LaTeX::Decode::Data;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 6 ▶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 1)
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  
61 $LaTeX::Decode::DefaultScheme = '<name>';
62  
63 Possible values are 'base', 'extra' and 'full'; default value is 'extra'.
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
69 (e.g. for IPA, Latin Extended Additional, etc.)
70  
71 full => Also converts symbols, Greek letters, dingbats, negated symbols, and
72 superscript characters and symbols ...
73  
74 =cut
75  
76 6 our $DefaultScheme = 'extra';
77  
78 sub _get_diac {
79 6 ▶ my $scheme = shift;
Subroutine _get_diac: called CC=2 SCAR=7.6
80 6 ▶ if ( $scheme eq 'base' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($scheme eq 'base') { } 0 6 6
81 0 return %DIACRITICS;
82 }
83 else {
84 6 return ( %DIACRITICS, %DIACRITICSEXTRA );
85 }
86 }
87  
88 sub _get_mac {
89 6 ▶ my $scheme = shift;
Subroutine _get_mac: called CC=3 SCAR=13.3
90 6 ▶ if ( $scheme eq 'base' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($scheme eq 'base') { } 0 6 6
elsif ($scheme eq 'full') { } 0 6 6
91 0 return %WORDMACROS;
92 }
93 elsif ( $scheme eq 'full' ) {
94 0 return ( %WORDMACROS, %WORDMACROSEXTRA, %PUNCTUATION, %SYMBOLS,
95 %GREEK );
96 }
97 else {
98 6 return ( %WORDMACROS, %WORDMACROSEXTRA, %PUNCTUATION );
99 }
100 }
101  
102 sub latex_decode {
103 6 ▶ my $text = shift;
Subroutine latex_decode: called CC=13 SCAR=28.0
Pod documented
104 6 my %opts = @_;
105 6 ▶ my $norm = exists $opts{normalize} ? $opts{normalize} : 1;
Branch: 1 branch
BranchTrueFalseTotal
exists $opts{'normalize'} ? : 0 6 6
106 6 ▶ my $norm_form = exists $opts{normalization} ? $opts{normalization} : 'NFC';
Branch: 1 branch
BranchTrueFalseTotal
exists $opts{'normalization'} ? : 1 5 6
107 6 ▶ my $scheme = exists $opts{scheme} ? $opts{scheme} : $DefaultScheme;
Branch: 1 branch
BranchTrueFalseTotal
exists $opts{'scheme'} ? : 0 6 6
108 6 ▶ croak "invalid scheme name '$scheme'"
Branch: 1 branch
BranchTrueFalseTotal
unless $scheme eq "full" or $scheme eq "base" or $scheme eq "extra" 0 6 6
Condition33%2/6
$scheme eq "full" or $scheme eq "base" or $scheme eq "extra"
l!l&&r!l&&!r
060
$scheme eq "full" or $scheme eq "base"
l!l&&r!l&&!r
006
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 6 ▶ exists $opts{strip_outer_braces} ? $opts{strip_outer_braces} : 0;
Branch: 1 branch
BranchTrueFalseTotal
exists $opts{'strip_outer_braces'} ? : 3 3 6
112  
113 6 my %DIAC = _get_diac($scheme);
114 6 my %WORDMAC = _get_mac($scheme);
115  
116 # a regex with all possible word macros
117 my $WORDMAC_RE =
118 6 join( '|', sort { length $b <=> length $a } keys %WORDMAC );
119 6 $WORDMAC_RE = qr{$WORDMAC_RE};
120  
121 6 my $DIAC_RE;
122 6 ▶ if ( $scheme eq 'base' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($scheme eq 'base') { } 0 6 6
123 0 $DIAC_RE = $DIAC_RE_BASE;
124 }
125 else {
126 6 $DIAC_RE = $DIAC_RE_EXTRA;
127 }
128  
129 6 ▶ if ( $scheme eq 'full' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($scheme eq "full") 0 6 6
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 6 $text =~ s/(\\[a-zA-Z]+)\\(\s+)/$1\{\}$2/g; # \foo\ bar -> \foo{} bar
137 6 $text =~ s/([^{]\\\w)([;,.:%])/$1\{\}$2/g; #} Aaaa\o, -> Aaaa\o{},
138 6 $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 6 ▶ if ($strip_outer_braces) {
Branch: 1 branch
BranchTrueFalseTotal
if ($strip_outer_braces) 3 3 6
144 3 $text =~ s/ \{\\($WORDMAC_RE)\} / $WORDMAC{$1} /gxe;
145 }
146 6 $text =~ s/ \\($WORDMAC_RE)(?: \{\} | \s+ | \b) / $WORDMAC{$1} /gxe;
147  
148 6 $text =~ s/\\($ACCENTS_RE)\{(\p{L}\p{M}*)\}/$2 . $ACCENTS{$1}/ge;
149  
150 6 $text =~ s/\\($ACCENTS_RE)(\p{L}\p{M}*)/$2 . $ACCENTS{$1}/ge;
151  
152 6 $text =~ s/\\($DIAC_RE)\s*\{(\p{L}\p{M}*)\}/$2 . $DIAC{$1}/ge;
153  
154 6 $text =~ s/\\($DIAC_RE)\s+(\p{L}\p{M}*)/$2 . $DIAC{$1}/ge;
155  
156 6 $text =~ s/\\($ACCENTS_RE)\{(\p{L}\p{M}*)\}/$2 . $ACCENTS{$1}/ge;
157  
158 6 $text =~ s/\\($ACCENTS_RE)(\p{L}\p{M}*)/$2 . $ACCENTS{$1}/ge;
159  
160 6 $text =~ s/\\($DIAC_RE)\s*\{(\p{L}\p{M}*)\}/$2 . $DIAC{$1}/ge;
161  
162 6 $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 6 ▶ if ($strip_outer_braces) {
Branch: 1 branch
BranchTrueFalseTotal
if ($strip_outer_braces) 3 3 6
167 3 $text =~ s/{(\PM\pM+)}/$1/g;
168 }
169  
170 6 ▶ if ($norm) {
Branch: 1 branch
BranchTrueFalseTotal
if ($norm) { } 6 0 6
171 6 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-2010 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 6 1;
204  
205 # vim: set tabstop=4 shiftwidth=4 expandtab:
↑ Summary Build »