#! /usr/bin/env perl
# texi2any: Texinfo converter.
#
# Copyright 2010-2021 Free Software Foundation, Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License,
# or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see .
#
# Original author: Patrice Dumas
# Parts (also from Patrice Dumas) come from texi2html.pl or texi2html.init.
# for POSIX::setlocale and File::Spec
require 5.00405;
use strict;
# for file names portability
use File::Spec;
# to determine the path separator and null file
use Config;
# for dirname and fileparse
use File::Basename;
#use Cwd;
use Getopt::Long qw(GetOptions);
# for carp
#use Carp;
Getopt::Long::Configure("gnu_getopt");
my ($real_command_name, $command_directory, $command_suffix);
# This big BEGIN block deals with finding modules and
# some dependencies that we ship
# * in source or
# * installed or
# * installed relative to the script
BEGIN
{
# emulate -w
$^W = 1;
($real_command_name, $command_directory, $command_suffix)
= fileparse($0, '.pl');
my $updir = File::Spec->updir();
# These are substituted by the Makefile to create "texi2any".
my $datadir = '@datadir@';
my $package = '@PACKAGE@';
my $packagedir = '@pkglibdir@';
if ($datadir eq '@' .'datadir@'
or defined($ENV{'TEXINFO_DEV_SOURCE'})
and $ENV{'TEXINFO_DEV_SOURCE'} ne '0')
{
# Use uninstalled modules
# To find Texinfo::ModulePath
if (defined($ENV{'top_builddir'})) {
unshift @INC, File::Spec->catdir($ENV{'top_builddir'}, 'tp');
} else {
unshift @INC, File::Spec->catdir($command_directory);
}
require Texinfo::ModulePath;
Texinfo::ModulePath::init(undef, undef, 'updirs' => 1);
} else {
# Look for modules in their installed locations.
my $lib_dir = File::Spec->catdir($datadir, $package);
unshift @INC, $lib_dir;
require Texinfo::ModulePath;
Texinfo::ModulePath::init($lib_dir, $packagedir, 'installed' => 1);
}
} # end BEGIN
# This allows disabling use of XS modules when Texinfo is built.
BEGIN {
my $enable_xs = '@enable_xs@';
if ($enable_xs eq 'no') {
package Texinfo::XSLoader;
our $disable_XS;
$disable_XS = 1;
}
}
use Locale::Messages;
use Texinfo::Common;
use Texinfo::Convert::Converter;
# this associates the command line options to the arrays set during
# command line parsing.
my @css_files = ();
my @css_refs = ();
my $cmdline_options = { 'CSS_FILES' => \@css_files,
'CSS_REFS' => \@css_refs };
# determine the path separators
my $path_separator = $Config{'path_sep'};
$path_separator = ':' if (!defined($path_separator));
my $quoted_path_separator = quotemeta($path_separator);
# Paths and file names
my $curdir = File::Spec->curdir();
my $updir = File::Spec->updir();
# set by configure, prefix for the sysconfdir and so on
# This could be used in the eval
my $prefix = '@prefix@';
my $datarootdir;
my $sysconfdir;
my $pkgdatadir;
my $datadir;
my $fallback_prefix = File::Spec->catdir(File::Spec->rootdir(), 'usr', 'local');
# We need to eval as $prefix has to be expanded. However when we haven't
# run configure @sysconfdir will be expanded as an array, thus we verify
# whether configure was run or not
if ('@sysconfdir@' ne '@' . 'sysconfdir@') {
$sysconfdir = eval '"@sysconfdir@"';
} else {
$sysconfdir = File::Spec->catdir($fallback_prefix, 'etc');
}
if ('@datarootdir@' ne '@' . 'datarootdir@') {
$datarootdir = eval '"@datarootdir@"';
} else {
$datarootdir = File::Spec->catdir($fallback_prefix, 'share');
}
if ('@datadir@' ne '@' . 'datadir@' and '@PACKAGE@' ne '@' . 'PACKAGE@') {
$datadir = eval '"@datadir@"';
my $package = '@PACKAGE@';
$pkgdatadir = File::Spec->catdir($datadir, $package);
} else {
$datadir = File::Spec->catdir($fallback_prefix, 'share');
$pkgdatadir = File::Spec->catdir($datadir, 'texinfo');
}
# work-around in case libintl-perl do not do it itself
# see http://www.gnu.org/software/gettext/manual/html_node/The-LANGUAGE-variable.html#The-LANGUAGE-variable
if ((defined($ENV{"LC_ALL"}) and $ENV{"LC_ALL"} =~ /^(C|POSIX)$/)
or (defined($ENV{"LANG"}) and $ENV{"LANG"} =~ /^(C|POSIX)$/)) {
delete $ENV{"LANGUAGE"} if defined($ENV{"LANGUAGE"});
}
#my $messages_textdomain = 'texinfo';
my $messages_textdomain = '@PACKAGE@';
$messages_textdomain = 'texinfo' if ($messages_textdomain eq '@'.'PACKAGE@');
my $strings_textdomain = '@PACKAGE@' . '_document';
$strings_textdomain = 'texinfo_document'
if ($strings_textdomain eq '@'.'PACKAGE@' . '_document');
# we want a reliable way to switch locale, so we don't use the system
# gettext.
Locale::Messages->select_package('gettext_pp');
if ($Texinfo::ModulePath::texinfo_uninstalled) {
my $locales_dir = File::Spec->catdir($Texinfo::ModulePath::builddir,
'LocaleData');
if (-d $locales_dir) {
Locale::Messages::bindtextdomain ($strings_textdomain, $locales_dir);
# the messages in this domain are not regenerated automatically,
# only when calling ./maintain/regenerate_perl_module_files.sh
Locale::Messages::bindtextdomain ($messages_textdomain, $locales_dir);
} else {
warn "Locales dir for document strings not found\n";
}
} else {
Locale::Messages::bindtextdomain ($strings_textdomain,
File::Spec->catdir($datadir, 'locale'));
Locale::Messages::bindtextdomain ($messages_textdomain,
File::Spec->catdir($datadir, 'locale'));
}
# Version setting is complicated, because we cope with
# * script with configure values substituted or not
# * script shipped as part of texinfo or as a standalone perl module
# When shipped as a perl modules, $hardcoded_version is set to undef here
# by a sed one liner. The consequence is that configure.ac is not used
# to retrieve the version number.
# Otherwise this is only used as a safety value, and should never be used
# in practice as a regexp extracts the version from configure.ac.
my $hardcoded_version = "0.00-hardcoded";
# Version set in configure.ac
my $configured_version = '@PACKAGE_VERSION@';
if ($configured_version eq '@' . 'PACKAGE_VERSION@') {
# if not configured, and $hardcoded_version is set search for the version
# in configure.ac
if (defined($hardcoded_version)) {
if (open (CONFIGURE,
"< ".File::Spec->catfile($Texinfo::ModulePath::top_srcdir,
'configure.ac'))) {
while () {
if (/^AC_INIT\(\[[^\]]+\]\s*,\s*\[([^\]]+)\]\s*,/) {
$configured_version = "$1+dev"; # +dev to distinguish from installed
last;
}
}
close (CONFIGURE);
}
# This should never be used, but is a safety value
$configured_version = $hardcoded_version if (!defined($configured_version));
} else {
# used in the standalone perl module, as $hardcoded_version is undef
# and it should never be configured in that setup
require Texinfo::Common;
$configured_version = $Texinfo::Common::VERSION;
}
}
# Compare the version of this file with the version of the modules
# it is using. If they are different, don't go any further. This
# can happen if multiple versions of texi2any are installed under a
# different names, e.g. with the --program-suffix option to 'configure'.
# The version in Common.pm is checked because that file has been present
# since Texinfo 5.0 (the first release with texi2any in Perl).
if ($configured_version ne $Texinfo::Common::VERSION
and $configured_version ne $Texinfo::Common::VERSION."+dev") {
warn "This is texi2any $configured_version but modules ".
"for texi2any $Texinfo::Common::VERSION found!\n";
die "Your installation of Texinfo is broken; aborting.\n";
}
my $configured_package = '@PACKAGE@';
$configured_package = 'Texinfo' if ($configured_package eq '@' . 'PACKAGE@');
my $configured_name = '@PACKAGE_NAME@';
$configured_name = $configured_package
if ($configured_name eq '@' .'PACKAGE_NAME@');
my $configured_name_version = "$configured_name $configured_version";
my $configured_url = '@PACKAGE_URL@';
$configured_url = 'http://www.gnu.org/software/texinfo/'
if ($configured_url eq '@' .'PACKAGE_URL@');
my $texinfo_dtd_version = '@TEXINFO_DTD_VERSION@';
# $hardcoded_version is undef for a standalone perl module
if ($texinfo_dtd_version eq '@' . 'TEXINFO_DTD_VERSION@') {
$texinfo_dtd_version = undef;
if (defined($hardcoded_version)) {
if (open (CONFIGURE,
"< ".File::Spec->catfile($Texinfo::ModulePath::top_srcdir,
'configure.ac'))) {
while () {
if (/^TEXINFO_DTD_VERSION=([0-9]\S*)/) {
$texinfo_dtd_version = "$1";
last;
}
}
close (CONFIGURE);
}
}
}
# Used in case it is not hardcoded in configure and for standalone perl module
$texinfo_dtd_version = $configured_version
if (!defined($texinfo_dtd_version));
# defaults for options relevant in the main program, not undef, and also
# defaults for all the converters.
# Other relevant options (undef) are NO_WARN FORCE OUTFILE
# Others are set in the converters (FORMAT_MENU).
my $converter_default_options = {
'ERROR_LIMIT' => 100,
'TEXI2DVI' => 'texi2dvi',
'PACKAGE_VERSION' => $configured_version,
'PACKAGE' => $configured_package,
'PACKAGE_NAME' => $configured_name,
'PACKAGE_AND_VERSION' => $configured_name_version,
'PACKAGE_URL' => $configured_url,
'PROGRAM' => $real_command_name,
'TEXINFO_DTD_VERSION' => $texinfo_dtd_version,
};
# determine configuration directories.
my $conf_file_name = 'Config' ;
# directories for texinfo configuration files
my @language_config_dirs = File::Spec->catdir($curdir, '.texinfo');
push @language_config_dirs, File::Spec->catdir($ENV{'HOME'}, '.texinfo')
if (defined($ENV{'HOME'}));
push @language_config_dirs, File::Spec->catdir($sysconfdir, 'texinfo')
if (defined($sysconfdir));
push @language_config_dirs, File::Spec->catdir($datadir, 'texinfo')
if (defined($datadir));
my @texinfo_config_dirs = ($curdir, @language_config_dirs);
my @program_config_dirs;
my @program_init_dirs;
my $program_name = 'texi2any';
@program_config_dirs = ($curdir, File::Spec->catdir($curdir, ".$program_name"));
push @program_config_dirs, File::Spec->catdir($ENV{'HOME'}, ".$program_name")
if (defined($ENV{'HOME'}));
push @program_config_dirs, File::Spec->catdir($sysconfdir, $program_name)
if (defined($sysconfdir));
push @program_config_dirs, File::Spec->catdir($datadir, $program_name)
if (defined($datadir));
@program_init_dirs = @program_config_dirs;
foreach my $texinfo_config_dir (@language_config_dirs) {
push @program_init_dirs, File::Spec->catdir($texinfo_config_dir, 'init');
}
# Namespace for configuration
{
package Texinfo::Config;
#use Carp;
# passed from main program
my $cmdline_options;
my $default_options;
# used in main program
our $options = {};
sub _load_config($$) {
$default_options = shift;
$cmdline_options = shift;
#print STDERR "cmdline_options: ".join('|',keys(%$cmdline_options))."\n";
}
sub _load_init_file($) {
my $file = shift;
require Texinfo::Convert::HTML;
eval { require($file) ;};
my $e = $@;
if ($e ne '') {
main::document_warn(sprintf(main::__("error loading %s: %s\n"),
$file, $e));
}
}
# FIXME: maybe use an opaque return status that can be used to retrieve
# an error message?
sub set_from_init_file($$) {
my $var = shift;
my $value = shift;
if (!Texinfo::Common::valid_option($var)) {
# carp may be better, but infortunately, it points to the routine that
# loads the file, and not to the init file.
main::document_warn(sprintf(main::__("%s: unknown variable %s"),
'set_from_init_file', $var));
return 0;
}
return 0 if (defined($cmdline_options->{$var}));
delete $default_options->{$var};
$options->{$var} = $value;
return 1;
}
sub set_from_cmdline($$) {
my $var = shift;
my $value = shift;
delete $options->{$var};
delete $default_options->{$var};
if (!Texinfo::Common::valid_option($var)) {
main::document_warn(sprintf(main::__("%s: unknown variable %s\n"),
'set_from_cmdline', $var));
return 0;
}
$cmdline_options->{$var} = $value;
return 1;
}
# This also could get and set some @-command results.
# FIXME But it does not take into account what happens during conversion,
# for that something like $converter->get_conf(...) has to be used.
sub get_conf($) {
my $var = shift;
if (exists($cmdline_options->{$var})) {
return $cmdline_options->{$var};
} elsif (exists($options->{$var})) {
return $options->{$var};
} elsif (exists($default_options->{$var})) {
return $default_options->{$var};
} else {
return undef;
}
}
# to dynamically add options from init files
sub texinfo_add_valid_option($)
{
my $option = shift;
return Texinfo::Common::add_valid_option($option);
}
}
# back in main program namespace
sub locate_and_load_init_file($$)
{
my $filename = shift;
my $directories = shift;
my $file = Texinfo::Common::locate_init_file($filename, $directories, 0);
if (defined($file)) {
Texinfo::Config::_load_init_file($file);
} else {
document_warn(sprintf(__("could not read init file %s"), $filename));
}
}
# read initialization files
foreach my $file (Texinfo::Common::locate_init_file($conf_file_name,
[ reverse(@program_config_dirs) ], 1)) {
Texinfo::Config::_load_init_file($file);
}
sub set_from_cmdline($$) {
return &Texinfo::Config::set_from_cmdline(@_);
}
sub set_from_init_file($$) {
return &Texinfo::Config::set_from_init_file(@_);
}
sub get_conf($) {
return &Texinfo::Config::get_conf(@_);
}
my @input_file_suffixes = ('.txi','.texinfo','.texi','.txinfo','');
my @texi2dvi_args = ();
my $format = 'info';
# this is the format associated with the output format, which is replaced
# when the output format changes. It may also be removed if there is the
# corresponding --no-ifformat.
my $default_expanded_format = [ $format ];
my @conf_dirs = ();
my @include_dirs = ();
my @prepend_dirs = ();
# options for all the files
my $parser_options = {'expanded_formats' => [],
'values' => {'txicommandconditionals' => 1}};
Texinfo::Config::_load_config($converter_default_options, $cmdline_options);
sub set_expansion($$) {
my $region = shift;
my $set = shift;
$set = 1 if (!defined($set));
if ($set) {
push @{$parser_options->{'expanded_formats'}}, $region
unless (grep {$_ eq $region} @{$parser_options->{'expanded_formats'}});
} else {
@{$parser_options->{'expanded_formats'}} =
grep {$_ ne $region} @{$parser_options->{'expanded_formats'}};
@{$default_expanded_format}
= grep {$_ ne $region} @{$default_expanded_format};
}
}
my $format_from_command_line = 0;
my %format_command_line_names = (
'xml' => 'texinfoxml',
);
my %formats_table = (
'info' => {
'nodes_tree' => 1,
'floats' => 1,
'module' => 'Texinfo::Convert::Info'
},
'plaintext' => {
'nodes_tree' => 1,
'floats' => 1,
'split' => 1,
'module' => 'Texinfo::Convert::Plaintext'
},
'html' => {
'nodes_tree' => 1,
'floats' => 1,
'split' => 1,
'internal_links' => 1,
'simple_menu' => 1,
'move_index_entries_after_items' => 1,
'relate_index_entries_to_table_entries' => 1,
'no_warn_non_empty_parts' => 1,
'module' => 'Texinfo::Convert::HTML'
},
'texinfoxml' => {
'nodes_tree' => 1,
'module' => 'Texinfo::Convert::TexinfoXML',
'floats' => 1,
},
'texinfosxml' => {
'nodes_tree' => 1,
'module' => 'Texinfo::Convert::TexinfoSXML',
'floats' => 1,
},
'ixinsxml' => {
'nodes_tree' => 1,
'module' => 'Texinfo::Convert::IXINSXML'
},
'docbook' => {
'move_index_entries_after_items' => 1,
'no_warn_non_empty_parts' => 1,
'module' => 'Texinfo::Convert::DocBook'
},
'pdf' => {
'texi2dvi_format' => 1,
},
'ps' => {
'texi2dvi_format' => 1,
},
'dvi' => {
'texi2dvi_format' => 1,
},
'dvipdf' => {
'texi2dvi_format' => 1,
},
'debugtree' => {
'split' => 1,
'module' => 'DebugTexinfo::DebugTree'
},
'textcontent' => {
'module' => 'Texinfo::Convert::TextContent'
},
'rawtext' => {
'module' => 'Texinfo::Convert::Text'
},
'plaintexinfo' => {
'module' => 'Texinfo::Convert::PlainTexinfo'
},
'parse' => {
},
'structure' => {
'nodes_tree' => 1,
'floats' => 1,
'split' => 1,
},
);
my $call_texi2dvi = 0;
# previous_format should be in argument if there is a possibility of error.
# as a fallback, the $format global variable is used.
sub set_format($;$$)
{
my $set_format = shift;
my $previous_format = shift;
$previous_format = $format if (!defined($previous_format));
my $do_not_override_command_line = shift;
my $new_format;
if ($format_command_line_names{$set_format}) {
$new_format = $format_command_line_names{$set_format};
} else {
$new_format = $set_format;
}
my $expanded_format = $set_format;
if (!$formats_table{$new_format}) {
document_warn(sprintf(__("ignoring unrecognized TEXINFO_OUTPUT_FORMAT value `%s'\n"),
$new_format));
$new_format = $previous_format;
} else {
if ($format_from_command_line and $do_not_override_command_line) {
$new_format = $previous_format;
} else {
if ($formats_table{$new_format}->{'texi2dvi_format'}) {
$call_texi2dvi = 1;
push @texi2dvi_args, '--'.$new_format;
$expanded_format = 'tex';
}
if ($Texinfo::Common::texinfo_output_formats{$expanded_format}) {
if ($expanded_format eq 'plaintext') {
$default_expanded_format = [$expanded_format, 'info']
} else {
$default_expanded_format = [$expanded_format]
}
}
$format_from_command_line = 1
unless ($do_not_override_command_line);
}
}
return $new_format;
}
sub set_global_format($)
{
my $set_format = shift;
$format = set_format($set_format);
}
sub document_warn($) {
return if (get_conf('NO_WARN'));
my $text = shift;
chomp ($text);
warn(sprintf(__p("program name: warning: warning_message",
"%s: warning: %s\n"), $real_command_name, $text));
}
sub _exit($$)
{
my $error_count = shift;
my $opened_files = shift;
if ($error_count and $opened_files and !get_conf('FORCE')) {
while (@$opened_files) {
my $opened_file = shift (@$opened_files);
unlink ($opened_file);
}
}
exit (1) if ($error_count and (!get_conf('FORCE')
or $error_count > get_conf('ERROR_LIMIT')));
}
sub handle_errors($$$)
{
my $self = shift;
my $error_count = shift;
my $opened_files = shift;
my ($errors, $new_error_count) = $self->errors();
$error_count += $new_error_count if ($new_error_count);
foreach my $error_message (@$errors) {
warn $error_message->{'error_line'} if ($error_message->{'type'} eq 'error'
or !get_conf('NO_WARN'));
}
_exit($error_count, $opened_files);
return $error_count;
}
sub _get_converter_default($)
{
my $option = shift;
return $Texinfo::Convert::Converter::all_converters_defaults{$option}
if (defined($Texinfo::Convert::Converter::all_converters_defaults{$option}));
return undef;
}
# translation related todo to be done when the string change anyway to
# avoid requiring translation
sub makeinfo_help()
{
# TODO: avoid \n in translated strings. Report from Benno Schulenberg
my $makeinfo_help =
sprintf(__("Usage: %s [OPTION]... TEXINFO-FILE...\n"),
$real_command_name . $command_suffix)
."\n".
__("Translate Texinfo source documentation to various other formats, by default
Info files suitable for reading online with Emacs or standalone GNU Info.
This program is commonly installed as both `makeinfo' and `texi2any';
the behavior is identical, and does not depend on the installed name.\n")
."\n";
# TODO: avoid \n in translated strings, split each option in a translatable
# string. Report from Benno Schulenberg
$makeinfo_help .= sprintf(__("General options:
--document-language=STR locale to use in translating Texinfo keywords
for the output document (default C).
--error-limit=NUM quit after NUM errors (default %d).
--force preserve output even if errors.
--help display this help and exit.
--no-validate suppress node cross-reference validation.
--no-warn suppress warnings (but not errors).
--conf-dir=DIR search also for initialization files in DIR.
--init-file=FILE load FILE to modify the default behavior.
-c, --set-customization-variable VAR=VAL set customization variable VAR
to value VAL.
-v, --verbose explain what is being done.
--version display version information and exit.\n"),
get_conf('ERROR_LIMIT'))
."\n";
# TODO: avoid \n in translated strings, split each option in a translatable
# string. Report from Benno Schulenberg
$makeinfo_help .= __("Output format selection (default is to produce Info):
--docbook output Docbook XML rather than Info.
--html output HTML rather than Info.
--plaintext output plain text rather than Info.
--xml output Texinfo XML rather than Info.
--dvi, --dvipdf, --ps, --pdf call texi2dvi to generate given output,
after checking validity of TEXINFO-FILE.\n")
."\n";
# TODO: avoid \n in translated strings, split each option in a translatable
# string. Report from Benno Schulenberg
$makeinfo_help .= __("General output options:
-E, --macro-expand=FILE output macro-expanded source to FILE,
ignoring any \@setfilename.
--no-headers suppress node separators, Node: lines, and menus
from Info output (thus producing plain text)
or from HTML (thus producing shorter output).
Also, if producing Info, write to
standard output by default.
--no-split suppress any splitting of the output;
generate only one output file.
--[no-]number-sections output chapter and sectioning numbers;
default is on.
-o, --output=DEST output to DEST.
With split output, create DEST as a directory
and put the output files there.
With non-split output, if DEST is already
a directory or ends with a /,
put the output file there.
Otherwise, DEST names the output file.\n")
."\n";
# TODO: avoid \n in translated strings, split each option in a translatable
# string. Report from Benno Schulenberg
$makeinfo_help .= sprintf(__("Options for Info and plain text:
--disable-encoding do not output accented and special characters
in Info output based on \@documentencoding.
--enable-encoding override --disable-encoding (default).
--fill-column=NUM break Info lines at NUM characters (default %d).
--footnote-style=STYLE output footnotes in Info according to STYLE:
`separate' to put them in their own node;
`end' to put them at the end of the node, in
which they are defined (this is the default).
--paragraph-indent=VAL indent Info paragraphs by VAL spaces (default %d).
If VAL is `none', do not indent; if VAL is
`asis', preserve existing indentation.
--split-size=NUM split Info files at size NUM (default %d).\n"),
_get_converter_default('fillcolumn'),
_get_converter_default('paragraphindent'),
_get_converter_default('SPLIT_SIZE'))
."\n";
# TODO: avoid \n in translated strings, split each option in a translatable
# string. Report from Benno Schulenberg
$makeinfo_help .= __("Options for HTML:
--css-include=FILE include FILE in HTML