#!/usr/bin/env perl # Use the LaTeX::ToUnicode module (also in the bibtexperllibs # repository/package, like this script) to convert LaTeX to Unicode. # # We work on fragments of text, not whole documents, the goal being to # replace LaTeX commands and syntax with obvious plain text equivalents, # or remove them. use strict; use warnings; use Cwd; use File::Basename; use File::Spec; BEGIN { # find files relative to our installed location within TeX Live chomp(my $TLMaster = `kpsewhich -var-value=SELFAUTOPARENT`); # TL root if (length($TLMaster)) { unshift @INC, "$TLMaster/texmf-dist/scripts/bibtexperllibs"; } # find development bibtexperllibs in sibling checkout to this script, # even if $0 is a symlink. Irrelevant when using from an installation. my $real0 = Cwd::abs_path($0); my $scriptdir = File::Basename::dirname($real0); my $dev_btxperllibs = Cwd::abs_path("$scriptdir/../.."); # we need the lib/ subdirectories inside ... unshift (@INC, glob ("$dev_btxperllibs/*/lib")) if -d $dev_btxperllibs; } use LaTeX::ToUnicode; our %opts; local *OUT; # output filehandle exit(main()); sub main { init(); # by paragraph? while (<>) { print OUT (convert($_)); } return 0; } sub convert { my ($in) = @_; my @args = (); # what we'll pass to the convert() fn. # if (defined(&{"LaTeX_ToUnicode_convert_hook"})) { push (@args, "hook" => \&LaTeX_ToUnicode_convert_hook); } if ($opts{e}) { push (@args, "entities" => 1); } if ($opts{g}) { push (@args, "german" => 1); } if ($opts{h}) { push (@args, "html" => 1); } LaTeX::ToUnicode::debuglevel($opts{v}); my $out = LaTeX::ToUnicode::convert($in, @args); #warn "out=$out"; return $out; } # Command line options, etc. # sub init { my $USAGE = < \($opts{c}), "entities|e" => \($opts{e}), "german|g" => \($opts{g}), "html|h" => \($opts{h}), "output|o=s" => \($opts{o}), "verbose|v" => \($opts{v}), "version|V" => \($opts{V}), "help|?" => \($opts{help})) || die "Try $0 --help for more information.\n"; if ($opts{help}) { print "$USAGE\n$VERSION"; exit 0; } if ($opts{V}) { print $VERSION; exit 0; } binmode(STDOUT, ":utf8"); *OUT = *STDOUT; if (defined($opts{o})) { open(OUT, ">$opts{o}") || die "open(>$opts{o}) failed: $!\n"; binmode(OUT, ":utf8") } if ($opts{c}) { if (-r $opts{c}) { # if config arg is absolute, fine; if not, prepend "./" as slightly # less troublesome than putting "." in the @INC path. my $rel = (File::Spec->file_name_is_absolute($opts{c}) ? "" : "./"); my $cnffile = "$rel$opts{c}"; verbose("requiring config file: $cnffile"); require $cnffile; } else { die "open config file ($opts{c}) for reading failed: $!\n"; } } } sub verbose { print @_ if $::opts{v}; }