| 1 | #!/usr/bin/perl | ||||||||||||||||||||||||||||
| 2 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | #no warnings 'utf8'; | ||||||||||||||||||||||||||||
| 6 | 3 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 3 | ▶ | use LaTeX::Decode; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 3 | ▶ | use Encode; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 3 | binmode(STDOUT, ':utf8'); | |||||||||||||||||||||||||||
| 10 | 3 | ▶ | use Getopt::Long qw/:config no_ignore_case/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 3 | my $opts = {}; | |||||||||||||||||||||||||||
| 13 | 3 | GetOptions( | |||||||||||||||||||||||||||
| 14 | $opts, | ||||||||||||||||||||||||||||
| 15 | 'help|h|?', | ||||||||||||||||||||||||||||
| 16 | 'version|v', | ||||||||||||||||||||||||||||
| 17 | 'inputencoding|i=s', | ||||||||||||||||||||||||||||
| 18 | 'filter|f!', | ||||||||||||||||||||||||||||
| 19 | 'scheme|s=s', | ||||||||||||||||||||||||||||
| 20 | 'normalize|n!', | ||||||||||||||||||||||||||||
| 21 | 'normalization|N=s', | ||||||||||||||||||||||||||||
| 22 | 'strip_outer_braces|b!' | ||||||||||||||||||||||||||||
| 23 | ); | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 3 | our $VERSION = '0.03'; | |||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 3 | ▶ | die usage() if exists $opts->{'help'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 3 | ▶ | die version() if exists $opts->{'version'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 3 | my $text; | |||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 3 | ▶ | if ($opts->{filter}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 34 | 0 | ▶ | while (<STDIN>) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 35 | 0 | $text .= $_ | |||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||
| 37 | 0 | ▶ | croak "Huh? There is nothing to convert!\n" if (!$text or $text =~ /^\s*$/) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not $text or $text =~ /^\s*$/
MC/DC0%0/2
not $text or $text =~ /^\s*$/
not $text
$text =~ /^\s*$/
Truth table0%0/3
not(A) or B
A= not $textB= $text =~ /^\s*$/
| |||||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||
| 39 | else { | ||||||||||||||||||||||||||||
| 40 | 3 | ▶ | my $infile = $ARGV[0] or die usage(); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 41 | 3 | require File::Slurp; | |||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 3 | ▶ | $text = File::Slurp::read_file($infile) or croak "Cannot read input file '$infile'"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | #Options to pass to latex_decode() | ||||||||||||||||||||||||||||
| 47 | 3 | my %ld_opts = (); | |||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 3 | ▶ | if (exists $opts->{inputencoding}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 50 | 0 | my $encoding = $opts->{inputencoding}; | |||||||||||||||||||||||||||
| 51 | 0 | $text = decode($encoding, $text); | |||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 3 | ▶ | if (exists $opts->{scheme} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 55 | $ld_opts{scheme} = $opts->{scheme} | ||||||||||||||||||||||||||||
| 56 | 0 | } | |||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 3 | ▶ | if (exists $opts->{normalize} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 59 | 0 | die "The option 'normalize' has been removed:\n\t use '--normalization 0' to suppress normalization\n" | |||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 3 | ▶ | if (exists $opts->{normalization} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 63 | 1 | ▶ | if (!$opts->{normalization} or $opts->{normalization} eq 'undef') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not $opts->{"normalization"} or $opts->{"normalization"} eq "undef"
MC/DC0%0/2
not $opts->{"normalization"} or $opts->{"normalization"} eq "undef"
not $opts->{"normalization"}
$opts->{"normalization"} eq "undef"
Truth table33%1/3
not(A) or B
A= not $opts->{"normalization"}B= $opts->{"normalization"} eq "undef"
| |||||||||||||||||||||||||||||
| 64 | 0 | $ld_opts{normalize} = 0 | |||||||||||||||||||||||||||
| 65 | } else { | ||||||||||||||||||||||||||||
| 66 | $ld_opts{normalization} = $opts->{normalization} | ||||||||||||||||||||||||||||
| 67 | 1 | } | |||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 3 | ▶ | if (exists $opts->{strip_outer_braces} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 71 | $ld_opts{strip_outer_braces} = $opts->{strip_outer_braces} | ||||||||||||||||||||||||||||
| 72 | 2 | } | |||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 3 | $text = decode_utf8($text); | |||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 3 | print latex_decode($text, %ld_opts); | |||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | sub version { | ||||||||||||||||||||||||||||
| 79 | 0 | ▶ | my $me = "latex2utf8"; | ||||||||||||||||||||||||||
|
Subroutine
version: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 0 | qq[ | |||||||||||||||||||||||||||
| 82 | $me Version: $VERSION | ||||||||||||||||||||||||||||
| 83 | \n] | ||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | sub usage { | ||||||||||||||||||||||||||||
| 87 | 0 | ▶ | qq/ | ||||||||||||||||||||||||||
|
Subroutine
usage: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 88 | Usage: latex2utf8 infile > outfile | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | Options: | ||||||||||||||||||||||||||||
| 91 | --help|-h Show this help message. | ||||||||||||||||||||||||||||
| 92 | --version|-v Display version number. | ||||||||||||||||||||||||||||
| 93 | --filter|-f Use script as a filter, using standard input instead of | ||||||||||||||||||||||||||||
| 94 | an input file | ||||||||||||||||||||||||||||
| 95 | --inputencoding|-i [encoding] | ||||||||||||||||||||||||||||
| 96 | Encoding used in the input file (or STDIN if using | ||||||||||||||||||||||||||||
| 97 | the option --filter) | ||||||||||||||||||||||||||||
| 98 | --scheme|-s Decoding scheme to use (possible values are 'base', | ||||||||||||||||||||||||||||
| 99 | 'extra', 'full'; default = 'extra') | ||||||||||||||||||||||||||||
| 100 | --normalization|-N [form] | ||||||||||||||||||||||||||||
| 101 | The normalization form to use (default = 'NFC') | ||||||||||||||||||||||||||||
| 102 | (with a value of 0 or undef the output will not be | ||||||||||||||||||||||||||||
| 103 | normalized with Unicode::Normalize) | ||||||||||||||||||||||||||||
| 104 | --strip_outer_braces|-b | ||||||||||||||||||||||||||||
| 105 | Remove curly braces around characters (boolean) | ||||||||||||||||||||||||||||
| 106 | (e.g. "saut{\\\'e}" => "sauté") | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | (See "perldoc LaTeX::Decode" for more information on the last three options.) | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | Example: latex2utf8 -i latin1 -s base -N NFD infile.tex > outfile.tex | ||||||||||||||||||||||||||||
| 111 | \n/ | ||||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | =pod | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | =encoding utf8 | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | =head1 NAME | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | C<latex2utf8> - converts LaTeX encoding to UTF-8 | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | =head1 VERSION | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | Version 0.03 | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | latex2utf8 file.tex > utf8file.tex | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | echo '\textexclamdown\textctj\alpha\textphook\texthvlig!' | latex2utf8 --scheme full --filter | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | Command-line utility to convert a LaTeX-encoded file to UTF-8. | ||||||||||||||||||||||||||||
| 135 | See the output of C<latex2utf8 --help> for usage and options. | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | François Charette, C<< <firmicus@cpan.org> >> | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | =head1 BUGS | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | Please report any bugs or feature requests to C<bug-latex-decode at | ||||||||||||||||||||||||||||
| 144 | rt.cpan.org>, or through the web interface at | ||||||||||||||||||||||||||||
| 145 | L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=LaTeX-Decode>. I will be | ||||||||||||||||||||||||||||
| 146 | notified, and then you'll automatically be notified of progress on your bug as | ||||||||||||||||||||||||||||
| 147 | I make changes. | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | =head1 COPYRIGHT & LICENSE | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | Copyright 2009-2010 François Charette, all rights reserved. | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | This module is free software. You can redistribute it and/or | ||||||||||||||||||||||||||||
| 154 | modify it under the terms of the Artistic License 2.0. | ||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | This program is distributed in the hope that it will be useful, | ||||||||||||||||||||||||||||
| 157 | but without any warranty; without even the implied warranty of | ||||||||||||||||||||||||||||
| 158 | merchantability or fitness for a particular purpose. | ||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | =cut | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | # vim: set tabstop=4 shiftwidth=4 expandtab: | ||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||