Debian Codegraph @

texk/texlive/linked_scripts/bibtexperllibs/ltx2unitxt

raw
1 #!/usr/bin/env perl
2 # Use the LaTeX::ToUnicode module (also in the bibtexperllibs
3 # repository/package, like this script) to convert LaTeX to Unicode.
4 #
5 # We work on fragments of text, not whole documents, the goal being to
6 # replace LaTeX commands and syntax with obvious plain text equivalents,
7 # or remove them.
8
9 use strict;
10 use warnings;
11
12 use Cwd;
13 use File::Basename;
14 use File::Spec;
15
16 BEGIN {
17 # find files relative to our installed location within TeX Live
18 chomp(my $TLMaster = `kpsewhich -var-value=SELFAUTOPARENT`); # TL root
19 if (length($TLMaster)) {
20 unshift @INC, "$TLMaster/texmf-dist/scripts/bibtexperllibs";
21 }
22 # find development bibtexperllibs in sibling checkout to this script,
23 # even if $0 is a symlink. Irrelevant when using from an installation.
24 my $real0 = Cwd::abs_path($0);
25 my $scriptdir = File::Basename::dirname($real0);
26 my $dev_btxperllibs = Cwd::abs_path("$scriptdir/../..");
27
28 # we need the lib/ subdirectories inside ...
29 unshift (@INC, glob ("$dev_btxperllibs/*/lib")) if -d $dev_btxperllibs;
30
31 }
32
33 use LaTeX::ToUnicode;
34
35 our %opts;
36 local *OUT; # output filehandle
37
38 exit(main());
39
40 sub main {
41 init();
42
43 # by paragraph?
44 while (<>) {
45 print OUT (convert($_));
46 }
47
48 return 0;
49 }
50
51 sub convert {
52 my ($in) = @_;
53
54 my @args = (); # what we'll pass to the convert() fn.
55 #
56 if (defined(&{"LaTeX_ToUnicode_convert_hook"})) {
57 push (@args, "hook" => \&LaTeX_ToUnicode_convert_hook); }
58 if ($opts{e}) { push (@args, "entities" => 1); }
59 if ($opts{g}) { push (@args, "german" => 1); }
60 if ($opts{h}) { push (@args, "html" => 1); }
61
62 LaTeX::ToUnicode::debuglevel($opts{v});
63 my $out = LaTeX::ToUnicode::convert($in, @args);
64
65 #warn "out=$out";
66 return $out;
67 }
68
69
70 # Command line options, etc.
71 #
72 sub init {
73 my $USAGE = <<END;
74 Usage: $0 [-c CONFIG] [-o OUTPUT] [--html] [...] [INFILE]...
75
76 Convert the LaTeX source in INFILE (or standard input) to plain text
77 using Unicode code points for accents and other special characters; or,
78 optionally, output HTML with simple translations for font changes and url
79 commands.
80
81 Common accent sequences, special characters, and simple markup commands
82 are translated, but there is no attempt at completeness. Math, tables,
83 figures, sectioning, etc., are not handled in any way, and mostly left
84 in their TeX form in the output. The translations assume standard LaTeX
85 meanings for characters and control sequences; macros in the input are
86 not considered.
87
88 The input can be a fragment of text, not a full document, as the purpose
89 of this script was to handle bibliography entries and abstracts (for the
90 ltx2crossrefxml script that is part of the crossrefware package).
91 Patches to extend this script are welcome. It uses the LaTeX::ToUnicode
92 Perl library for the conversion; see its documentation for details.
93
94 Conversion is currently done line by line, so TeX constructs that cross
95 multiple lines are not handled properly. If it turns out to be useful,
96 conversion could be done by paragraph instead.
97
98 The config file is read as a Perl source file. It can define a function
99 `LaTeX_ToUnicode_convert_hook()' which will be called early; the value
100 it returns (which must be a string) will then be subject to the standard
101 conversion.
102
103 For an example of using this script and associated code, see the TUGboat
104 processing at
105 https://github.com/TeXUsersGroup/tugboat/tree/trunk/capsules/crossref.
106
107 Options:
108 -c, --config=FILE read (Perl) config FILE for a hook, as explained above
109 -e, --entities output entities &#xNNNN; instead of literal characters
110 -g, --german handle some features of the german package
111 -h, --html output simplistic HTML instead of plain text
112 -o, --output=FILE output to FILE instead of stdout
113 -v, --verbose be verbose
114 -V, --version output version information and exit
115 -?, --help display this help and exit
116
117 Options can be abbreviated unambiguously, and start with either - or --.
118
119 Dev sources, bug tracker: https://github.com/borisveytsman/bibtexperllibs
120 Releases: https://ctan.org/pkg/bibtexperllibs
121 END
122
123 my $VERSION = <<END;
124 ltx2unitxt (bibtexperllibs) 0.51
125 Copyright 2023 Karl Berry.
126 This is free software: you can redistribute it and/or
127 modify it under the same terms as Perl itself.
128 END
129
130 use Getopt::Long qw(:config no_ignore_case); # otherwise v|V is the same
131
132 GetOptions(
133 "config|c=s" => \($opts{c}),
134 "entities|e" => \($opts{e}),
135 "german|g" => \($opts{g}),
136 "html|h" => \($opts{h}),
137 "output|o=s" => \($opts{o}),
138 "verbose|v" => \($opts{v}),
139 "version|V" => \($opts{V}),
140 "help|?" => \($opts{help}))
141 || die "Try $0 --help for more information.\n";
142
143 if ($opts{help}) { print "$USAGE\n$VERSION"; exit 0; }
144 if ($opts{V}) { print $VERSION; exit 0; }
145
146 binmode(STDOUT, ":utf8");
147 *OUT = *STDOUT;
148
149 if (defined($opts{o})) {
150 open(OUT, ">$opts{o}") || die "open(>$opts{o}) failed: $!\n";
151 binmode(OUT, ":utf8")
152 }
153
154 if ($opts{c}) {
155 if (-r $opts{c}) {
156 # if config arg is absolute, fine; if not, prepend "./" as slightly
157 # less troublesome than putting "." in the @INC path.
158 my $rel = (File::Spec->file_name_is_absolute($opts{c}) ? "" : "./");
159 my $cnffile = "$rel$opts{c}";
160 verbose("requiring config file: $cnffile");
161 require $cnffile;
162 } else {
163 die "open config file ($opts{c}) for reading failed: $!\n";
164 }
165 }
166 }
167
168
169 sub verbose { print @_ if $::opts{v}; }