| 1 |
#!/usr/bin/perl |
| 2 |
use strict; |
| 3 |
use warnings; |
| 4 |
|
| 5 |
#no warnings 'utf8'; |
| 6 |
use Carp; |
| 7 |
use LaTeX::Decode; |
| 8 |
use Encode; |
| 9 |
binmode(STDOUT, ':utf8'); |
| 10 |
use Getopt::Long qw/:config no_ignore_case/; |
| 11 |
|
| 12 |
my $opts = {}; |
| 13 |
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 |
our $VERSION = '0.04'; |
| 26 |
|
| 27 |
die usage() if exists $opts->{'help'}; |
| 28 |
|
| 29 |
die version() if exists $opts->{'version'}; |
| 30 |
|
| 31 |
my $text; |
| 32 |
|
| 33 |
if ($opts->{filter}) { |
| 34 |
while (<STDIN>) { |
| 35 |
$text .= $_ |
| 36 |
} |
| 37 |
croak "Huh? There is nothing to convert!\n" if (!$text or $text =~ /^\s*$/) |
| 38 |
} |
| 39 |
else { |
| 40 |
my $infile = $ARGV[0] or die usage(); |
| 41 |
require File::Slurp; |
| 42 |
|
| 43 |
$text = read_file($infile) or croak "Cannot read input file '$infile'"; |
| 44 |
} |
| 45 |
|
| 46 |
#Options to pass to latex_decode() |
| 47 |
my %ld_opts = (); |
| 48 |
|
| 49 |
if (exists $opts->{inputencoding}) { |
| 50 |
my $encoding = $opts->{inputencoding}; |
| 51 |
$text = decode($encoding, $text); |
| 52 |
} |
| 53 |
|
| 54 |
if (exists $opts->{scheme} ) { |
| 55 |
$ld_opts{scheme} = $opts->{scheme} |
| 56 |
} |
| 57 |
|
| 58 |
if (exists $opts->{normalize} ) { |
| 59 |
die "The option 'normalize' has been removed:\n\t use '--normalization 0' to suppress normalization\n" |
| 60 |
} |
| 61 |
|
| 62 |
if (exists $opts->{normalization} ) { |
| 63 |
if (!$opts->{normalization} or $opts->{normalization} eq 'undef') { |
| 64 |
$ld_opts{normalize} = 0 |
| 65 |
} else { |
| 66 |
$ld_opts{normalization} = $opts->{normalization} |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
if (exists $opts->{strip_outer_braces} ) { |
| 71 |
$ld_opts{strip_outer_braces} = $opts->{strip_outer_braces} |
| 72 |
} |
| 73 |
|
| 74 |
$text = decode_utf8($text); |
| 75 |
|
| 76 |
print latex_decode($text, %ld_opts); |
| 77 |
|
| 78 |
sub version { |
| 79 |
my $me = "latex2utf8"; |
| 80 |
|
| 81 |
qq[ |
| 82 |
$me Version: $VERSION |
| 83 |
\n] |
| 84 |
} |
| 85 |
|
| 86 |
sub usage { |
| 87 |
qq/ |
| 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.04 |
| 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<< <[email protected]> >> |
| 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-2015 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 |
|