File Coverage

blib/lib/LaTeX/ToUnicode.pm
Criterion Covered Total %
statement 62 62 100.0
branch 7 10 70.0
condition n/a
subroutine 12 12 100.0
pod 1 1 100.0
total 82 85 96.4


line stmt bran cond sub pod time code
1 1     1   31857 use strict;
  1         3  
  1         62  
2 1     1   10 use warnings;
  1         2  
  1         117  
3             package LaTeX::ToUnicode;
4             BEGIN {
5 1     1   118 $LaTeX::ToUnicode::VERSION = '0.05';
6             }
7             #ABSTRACT: Convert LaTeX commands to Unicode
8              
9              
10             require Exporter;
11             our @ISA = qw(Exporter);
12             our @EXPORT_OK = qw( convert );
13              
14 1     1   18 use utf8;
  1         3  
  1         10  
15 1     1   1047 use LaTeX::ToUnicode::Tables;
  1         4  
  1         1618  
16              
17              
18             sub convert {
19 102     102 1 49431 my ( $string, %options ) = @_;
20 102         277 $string = _convert_commands( $string );
21 102         246 $string = _convert_accents( $string );
22 102 100       323 $string = _convert_german( $string ) if $options{german};
23 102         197 $string = _convert_symbols( $string );
24 102         266 $string = _convert_specials( $string );
25 102         228 $string = _convert_markups( $string );
26 102         230 $string =~ s/{(\w*)}/$1/g;
27 102         589 $string;
28             }
29              
30             sub _convert_accents {
31 102     102   187 my $string = shift;
32 102 50       349 $string =~ s/(\{\\(.)\{(\\?\w{1,2})\}\})/$LaTeX::ToUnicode::Tables::ACCENTS{$2}{$3} || $1/eg; # {\"{a}}
  29         241  
33 102 100       502 $string =~ s/(\{\\(.)(\\?\w{1,2})\})/$LaTeX::ToUnicode::Tables::ACCENTS{$2}{$3} || $1/eg; # {\"a}
  47         506  
34 102 50       277 $string =~ s/(\\(.)(\\?\w{1,2}))/$LaTeX::ToUnicode::Tables::ACCENTS{$2}{$3} || $1/eg; # \"a
  17         191  
35 102 50       177 $string =~ s/(\\(.)\{(\\?\w{1,2})\})/$LaTeX::ToUnicode::Tables::ACCENTS{$2}{$3} || $1/eg; # \"{a}
  1         8  
36 102         242 $string;
37             }
38              
39             sub _convert_specials {
40 102     102   142 my $string = shift;
41 102         303 my $specials = join( '|', @LaTeX::ToUnicode::Tables::SPECIALS );
42 102         385 my $pattern = qr/\\($specials)/o;
43 102         492 $string =~ s/$pattern/$1/g;
44 102         154 $string =~ s/\\\$/\$/g;
45 102         280 $string;
46             }
47              
48             sub _convert_commands {
49 102     102   181 my $string = shift;
50              
51 102         435 foreach my $command ( keys %LaTeX::ToUnicode::Tables::COMMANDS ) {
52 510         4984 $string =~ s/\{\\$command\}/$LaTeX::ToUnicode::Tables::COMMANDS{$command}/g;
53 510         6293 $string =~ s/\\$command(?=\s|\b)/$LaTeX::ToUnicode::Tables::COMMANDS{$command}/g;
54             }
55              
56 102         328 $string;
57             }
58              
59             sub _convert_german {
60 3     3   5 my $string = shift;
61              
62 3         19 foreach my $symbol ( keys %LaTeX::ToUnicode::Tables::GERMAN ) {
63 87         629 $string =~ s/\Q$symbol\E/$LaTeX::ToUnicode::Tables::GERMAN{$symbol}/g;
64             }
65 3         10 $string;
66             }
67              
68             sub _convert_symbols {
69 102     102   142 my $string = shift;
70              
71 102         646 foreach my $symbol ( keys %LaTeX::ToUnicode::Tables::SYMBOLS ) {
72 2244         15948 $string =~ s/{\\$symbol}/$LaTeX::ToUnicode::Tables::SYMBOLS{$symbol}/g;
73 2244         18469 $string =~ s/\\$symbol\b/$LaTeX::ToUnicode::Tables::SYMBOLS{$symbol}/g;
74             }
75 102         386 $string;
76             }
77              
78             sub _convert_markups {
79 102     102   122 my $string = shift;
80              
81 102         332 my $markups = join( '|', @LaTeX::ToUnicode::Tables::MARKUPS );
82 102         440 $string =~ s/(\{[^{}]+)\\(?:$markups)\s+([^{}]+\})/$1$2/g; # { ... \command ... }
83 102         338 my $pattern = qr/\{\\(?:$markups)\s+([^{}]*)\}/o;
84 102         410 $string =~ s/$pattern/$1/g;
85              
86 102         154 $string =~ s/``/“/g;
87 102         136 $string =~ s/`/”/g;
88 102         190 $string =~ s/''/‘/g;
89 102         177 $string =~ s/'/’/g;
90 102         251 $string;
91             }
92              
93             1;
94              
95             __END__