diff options
author | Karl Berry <karl@freefriends.org> | 2023-11-16 18:39:12 +0000 |
---|---|---|
committer | Karl Berry <karl@freefriends.org> | 2023-11-16 18:39:12 +0000 |
commit | c322165b37e8f2dee17d03e37fbc7bb1d211b14f (patch) | |
tree | c3d2af56c2d455a1046a445775d93fe491f66449 /Build | |
parent | 1eb8ec41072dd41f0c26702d2f3226f740930d8a (diff) |
bibtexperllibs: ltx2unitxt as user-level script
git-svn-id: svn://tug.org/texlive/trunk@68869 c570f23f-e606-0410-a88d-b1316a301751
Diffstat (limited to 'Build')
4 files changed, 175 insertions, 0 deletions
diff --git a/Build/source/texk/texlive/linked_scripts/Makefile.am b/Build/source/texk/texlive/linked_scripts/Makefile.am index e7601eda950..9102aee218b 100644 --- a/Build/source/texk/texlive/linked_scripts/Makefile.am +++ b/Build/source/texk/texlive/linked_scripts/Makefile.am @@ -99,6 +99,7 @@ texmf_shell_scripts = \ typeoutfileinfo/typeoutfileinfo.sh \ wordcount/wordcount.sh +# texmf_other_scripts = \ a2ping/a2ping.pl \ accfonts/mkt1font \ @@ -111,6 +112,7 @@ texmf_other_scripts = \ bib2gls/bib2gls.sh \ bib2gls/convertgls2bib.sh \ bibcop/bibcop.pl \ + bibtexperllibs/ltx2unitxt \ bundledoc/arlatex \ bundledoc/bundledoc \ cachepic/cachepic.tlu \ diff --git a/Build/source/texk/texlive/linked_scripts/Makefile.in b/Build/source/texk/texlive/linked_scripts/Makefile.in index 443b441cb23..3e350511bc0 100644 --- a/Build/source/texk/texlive/linked_scripts/Makefile.in +++ b/Build/source/texk/texlive/linked_scripts/Makefile.in @@ -315,6 +315,8 @@ texmf_shell_scripts = \ typeoutfileinfo/typeoutfileinfo.sh \ wordcount/wordcount.sh + +# texmf_other_scripts = \ a2ping/a2ping.pl \ accfonts/mkt1font \ @@ -327,6 +329,7 @@ texmf_other_scripts = \ bib2gls/bib2gls.sh \ bib2gls/convertgls2bib.sh \ bibcop/bibcop.pl \ + bibtexperllibs/ltx2unitxt \ bundledoc/arlatex \ bundledoc/bundledoc \ cachepic/cachepic.tlu \ diff --git a/Build/source/texk/texlive/linked_scripts/bibtexperllibs/ltx2unitxt b/Build/source/texk/texlive/linked_scripts/bibtexperllibs/ltx2unitxt new file mode 100755 index 00000000000..661ea404171 --- /dev/null +++ b/Build/source/texk/texlive/linked_scripts/bibtexperllibs/ltx2unitxt @@ -0,0 +1,169 @@ +#!/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 = <<END; +Usage: $0 [-c CONFIG] [-o OUTPUT] [--html] [...] [INFILE]... + +Convert the LaTeX source in INFILE (or standard input) to plain text +using Unicode code points for accents and other special characters; or, +optionally, output HTML with simple translations for font changes and url +commands. + +Common accent sequences, special characters, and simple markup commands +are translated, but there is no attempt at completeness. Math, tables, +figures, sectioning, etc., are not handled in any way, and mostly left +in their TeX form in the output. The translations assume standard LaTeX +meanings for characters and control sequences; macros in the input are +not considered. + +The input can be a fragment of text, not a full document, as the purpose +of this script was to handle bibliography entries and abstracts (for the +ltx2crossrefxml script that is part of the crossrefware package). +Patches to extend this script are welcome. It uses the LaTeX::ToUnicode +Perl library for the conversion; see its documentation for details. + +Conversion is currently done line by line, so TeX constructs that cross +multiple lines are not handled properly. If it turns out to be useful, +conversion could be done by paragraph instead. + +The config file is read as a Perl source file. It can define a function +`LaTeX_ToUnicode_convert_hook()' which will be called early; the value +it returns (which must be a string) will then be subject to the standard +conversion. + +For an example of using this script and associated code, see the TUGboat +processing at +https://github.com/TeXUsersGroup/tugboat/tree/trunk/capsules/crossref. + +Options: + -c, --config=FILE read (Perl) config FILE for a hook, as explained above + -e, --entities output entities &#xNNNN; instead of literal characters + -g, --german handle some features of the german package + -h, --html output simplistic HTML instead of plain text + -o, --output=FILE output to FILE instead of stdout + -v, --verbose be verbose + -V, --version output version information and exit + -?, --help display this help and exit + +Options can be abbreviated unambiguously, and start with either - or --. + +Dev sources, bug tracker: https://github.com/borisveytsman/bibtexperllibs +Releases: https://ctan.org/pkg/bibtexperllibs +END + + my $VERSION = <<END; +ltx2unitxt (bibtexperllibs) 0.51 +Copyright 2023 Karl Berry. +This is free software: you can redistribute it and/or +modify it under the same terms as Perl itself. +END + + use Getopt::Long qw(:config no_ignore_case); # otherwise v|V is the same + + GetOptions( + "config|c=s" => \($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}; } diff --git a/Build/source/texk/texlive/linked_scripts/scripts.lst b/Build/source/texk/texlive/linked_scripts/scripts.lst index a455f39391a..3342efc3356 100644 --- a/Build/source/texk/texlive/linked_scripts/scripts.lst +++ b/Build/source/texk/texlive/linked_scripts/scripts.lst @@ -53,6 +53,7 @@ authorindex/authorindex bib2gls/bib2gls.sh bib2gls/convertgls2bib.sh bibcop/bibcop.pl +bibtexperllibs/ltx2unitxt bundledoc/arlatex bundledoc/bundledoc cachepic/cachepic.tlu |