Skip to content

Commit

Permalink
TeX: use charsets, and nicer error message when input file is not found
Browse files Browse the repository at this point in the history
This fixes #421 in a lazy way:
it would be better to not even try to parse \input commands that are
located within a verbatim environment, but it is incredibly hard while
this trick is almost trivial: I'm just pointing to the right piece of
code to allow users to circumvent the issue.
  • Loading branch information
mquinson committed Jan 4, 2024
1 parent 937c32a commit d56dfa7
Show file tree
Hide file tree
Showing 11 changed files with 130 additions and 7 deletions.
6 changes: 6 additions & 0 deletions MANIFEST
Original file line number Diff line number Diff line change
Expand Up @@ -947,6 +947,12 @@ t/fmt/tex/basic.pot
t/fmt/tex/basic.tex
t/fmt/tex/basic.trans
t/fmt/tex/basic.trans.stderr
t/fmt/tex/input-in-verbatim.norm
t/fmt/tex/input-in-verbatim.po
t/fmt/tex/input-in-verbatim.pot
t/fmt/tex/input-in-verbatim.stderr-without-ignore
t/fmt/tex/input-in-verbatim.tex
t/fmt/tex/input-in-verbatim.trans
t/fmt/texinfo/comments.norm
t/fmt/texinfo/comments.po
t/fmt/texinfo/comments.pot
Expand Down
2 changes: 2 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ Asciidoc:
Tex:
* Allow verbatim environment declaration without trailing space
(GitHub's #407). Thanks Mayeul Cantan for the report & the fix.
* Informative error message when file not found for \input, indicating
how to skip this include (GitHub's #421) [mquinson].

Texinfo:
* Translate partial menu node names (GitHub's #437) [Apteryks].
Expand Down
24 changes: 17 additions & 7 deletions lib/Locale/Po4a/TeX.pm
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ use vars qw(@ISA @EXPORT);
use Locale::Po4a::TransTractor;
use Locale::Po4a::Common;
use File::Basename qw(dirname);
use Carp qw(croak);
use Carp qw(croak);

use Encode;
use Encode::Guess;
Expand Down Expand Up @@ -926,15 +926,16 @@ Overloads Transtractor's read().
=cut

sub read {
sub read ($$$$) {
my $self = shift;
my $filename = shift;
my $refname = shift;
my $charset = shift;

# keep the directory name of the main file.
$my_dirname = dirname($filename);

push @{ $self->{TT}{doc_in} }, read_file( $self, $filename, $refname );
push @{ $self->{TT}{doc_in} }, read_file( $self, $filename, $refname, $charset );
}

=item B<read_file>
Expand All @@ -954,10 +955,11 @@ sub read_file {
my $filename = shift
or croak wrap_mod( "po4a::tex", dgettext( "po4a", "Cannot read from file without having a filename" ) );
my $refname = shift // $filename;
my $charset = shift || 'UTF-8';
my $linenum = 0;
my @entries = ();

open( my $in, $filename )
open( my $in, "<:encoding($charset)", $filename )
or croak wrap_mod( "po4a::tex", dgettext( "po4a", "Cannot read from %s: %s" ), $filename, $! );
while ( defined( my $textline = <$in> ) ) {
$linenum++;
Expand Down Expand Up @@ -995,11 +997,19 @@ sub read_file {
open( KPSEA, "kpsewhich " . $newfilename . " |" );
my $newfilepath = <KPSEA>;

if ( $newfilename ne "" and $newfilepath eq "" ) {
die wrap_mod( "po4a::tex", dgettext( "po4a", "Cannot find %s with kpsewhich" ), $filename );
if ( $newfilename ne "" and ( $newfilepath // '' ) eq '' ) {
die wrap_mod(
"po4a::tex",
dgettext(
"po4a",
"Cannot find '%s' with kpsewhich. To prevent this file to be included, add '-o exclude_include=%s' to the options, either on the command line or in your po4a.conf file."
),
$newfilename,
$newfilename
);
}

push @entries, read_file( $self, $newfilepath, $newfilename );
push @entries, read_file( $self, $newfilepath, $newfilename, $charset );
if ( $tag eq "include" ) {
$textline = "\\clearpage" . $end;
} else {
Expand Down
6 changes: 6 additions & 0 deletions t/Testhelper.pm
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,12 @@ sub run_one_format {
} elsif ( $error != 0 && $exit_status != 0 ) {
pass("Expected error detected in $doc");
note(" Failing as expected: $cmd (retcode: $exit_status)");
note("Produced output:");
open FH, $real_stderr || die "Cannot open output file that I just created, I'm puzzled";
while (<FH>) {
note(" $_");
}
note("(end of command output)\n");
} else {
fail("Normalizing $doc: $exit_status");
note(" FAIL: $cmd");
Expand Down
15 changes: 15 additions & 0 deletions t/fmt-tex.t
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,20 @@ foreach my $t (qw(basic theorem)) {
};
}

push @tests,
{
'doc' => 'invalid input command, without ignore',
'format' => 'latex',
'input' => "fmt/tex/input-in-verbatim.tex",
'error' => 1,
'norm_stderr' => 'fmt/tex/input-in-verbatim.stderr-without-ignore',
},
{
'doc' => 'invalid input command, with exclude_include',
'format' => 'latex',
'input' => "fmt/tex/input-in-verbatim.tex",
'options' => '-o exclude_include=main.tex'
};

run_all_tests(@tests);
0;
11 changes: 11 additions & 0 deletions t/fmt/tex/input-in-verbatim.norm
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
% This file was generated with po4a. Translate the source file.
%
\documentclass[12pt]{article}
\begin{document}

\begin{verbatim}
\documentclass[ignorenonframetext]{beamer}
\input{main.tex}
\end{verbatim}

\end{document}
27 changes: 27 additions & 0 deletions t/fmt/tex/input-in-verbatim.po
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# SOME DESCRIPTIVE TITLE
# Copyright (C) YEAR Free Software Foundation, Inc.
# This file is distributed under the same license as the PACKAGE package.
# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
#
#, fuzzy
msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"POT-Creation-Date: 2024-01-04 01:52+0100\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <[email protected]>\n"
"Language: \n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"

#. type: verbatim
#: input-in-verbatim.tex:7
#, no-wrap
msgid ""
"\\documentclass[ignorenonframetext]{beamer}\n"
"\\input{main.tex}"
msgstr ""
"\\documentclass[ignorenonframetext]{beamer}\n"
"\\input{main.tex}"
25 changes: 25 additions & 0 deletions t/fmt/tex/input-in-verbatim.pot
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# SOME DESCRIPTIVE TITLE
# Copyright (C) YEAR Free Software Foundation, Inc.
# This file is distributed under the same license as the PACKAGE package.
# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
#
#, fuzzy
msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"POT-Creation-Date: 2024-01-04 01:50+0100\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <[email protected]>\n"
"Language: \n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"

#. type: verbatim
#: input-in-verbatim.tex:7
#, no-wrap
msgid ""
"\\documentclass[ignorenonframetext]{beamer}\n"
"\\input{main.tex}"
msgstr ""
1 change: 1 addition & 0 deletions t/fmt/tex/input-in-verbatim.stderr-without-ignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
po4a::tex: Cannot find 'main.tex' with kpsewhich. To prevent this file to be included, add '-o exclude_include=main.tex' to the options, either on the command line or in your po4a.conf file.
9 changes: 9 additions & 0 deletions t/fmt/tex/input-in-verbatim.tex
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
\documentclass[12pt]{article}
\begin{document}

\begin{verbatim}
\documentclass[ignorenonframetext]{beamer}
\input{main.tex}
\end{verbatim}

\end{document}
11 changes: 11 additions & 0 deletions t/fmt/tex/input-in-verbatim.trans
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
% This file was generated with po4a. Translate the source file.
%
\documentclass[12pt]{article}
\begin{document}

\begin{verbatim}
\documentclass[ignorenonframetext]{beamer}
\input{main.tex}
\end{verbatim}

\end{document}

0 comments on commit d56dfa7

Please sign in to comment.