#!/usr/bin/env perl
################################################################################
# texlogfilter - filter latex engines output or log file
# Julien Labbé, 2021-2026
#
# This work may be distributed and/or modified under the conditions of the LaTeX
# Project Public License, either version 1.3 of this license or (at your option)
# any later version. The latest version of this license is in
#   http://www.latex-project.org/lppl.txt and version 1.3
# or later is part of all distributions of LaTeX version 2005/12/01 or later.
################################################################################

use strict;
use warnings;
use Getopt::Long;
use Pod::Usage;
use Term::ANSIColor qw(:DEFAULT colorvalid);

# options
my @userfilters;
my $showhelp      = 0;
my $showman       = 0;
my $showversion   = 0;
my $showrefmsg    = 1;
my $showboxmsg    = 1;
my $showinfomsg   = 0;
my $showskips     = 0;
my $skipmarker    = "...";
my $extpattern    = "tex|sty|cls|bib|fd|bst|bbx|cbx|def|clo";
my $fulllog       = 0;
my $printfilename = -1; # -1: value not modified by options
my $errcolor      = "red";
my $wrncolor      = "yellow";
my $infcolor      = "blue";
my $skpcolor      = "bright_black";
my $usrcolor      = "reset";

GetOptions ("box!"              => \$showboxmsg,    # show box warnings
            "ref!"              => \$showrefmsg,    # show reference and citation warnings
            "info!"             => \$showinfomsg,   # show latex info messages
            "skips!"            => \$showskips,     # show that lines have been skipped
            "skip-marker=s"     => \$skipmarker,    # how to indicate skipped lines
            "files-ext=s"       => \$extpattern,    # file extension regex pattern
            "add-filter=s"      => \@userfilters,   # user filters list
            "errors-color=s"    => \$errcolor,      # color for errors
            "warnings-color=s"  => \$wrncolor,      # color for warnings
            "infos-color=s"     => \$infcolor,      # color for informations
            "skips-color=s"     => \$skpcolor,      # color for skipped lines marker
            "user-color=s"      => \$usrcolor,      # color for content filtered by pattern added with --add-filter
            "filename!"         => \$printfilename, # print current file name
            "full-log!"         => \$fulllog,       # show full log (don't filter - only colorize latex engine output)
            "help"              => \$showhelp,
            "manual"            => \$showman,
            "version"           => \$showversion)
    or die "Try --help.\n";

if ($printfilename == -1 && $fulllog) { $printfilename = 0; } # don't show filename for full log, if not explicitly required

# validate colors
for my $c ($errcolor, $wrncolor, $infcolor, $skpcolor, $usrcolor) {
    colorvalid($c) or die "Invalid color: '$c'\n";
}

# counters
my $nerr = 0;
my $nwrn = 0;

# iteration state
my $nextlines      = 0;
my $currentpackage = "";
my $currentfile    = "";
my $lastfile       = "";
my $samecontext    = -1;   # expected values: 0 (continuing previous line) / 1 (lines skipped) / -1 (initial value)

# functions

sub print_filename {
    # print current file name if new
    if ($printfilename && $currentfile ne $lastfile) {
        print color("bold"), color($infcolor), "\nFile: ", $currentfile, "\n", color("reset");
        $lastfile = $currentfile;
    }
}

sub print_skips {
    # show if some lines have benn skipped
    if ($showskips && !$samecontext) { print color("reset"), color($skpcolor), $skipmarker, "\n", color("reset") ; }
    $samecontext = 1;
}

sub handle_error {
    # handle error
    #  nl (optional)         : nextline value
    #  nofilename (optional) : if set, don't print filename
    my ( $nl, $nofilename ) = @_;
    $nerr++;
    if (defined $nl) { $nextlines = $nl ; }
    print_skips();
    if (! defined $nofilename) {
        print_filename();
    }
    print color($errcolor), $_, ;
}

sub handle_warning {
    # handle warning
    #  nl (optional)         : nextline value
    #  nofilename (optional) : if set, don't print filename
    my ( $nl, $nofilename ) = @_;
    $nwrn++;
    if (defined $nl) { $nextlines = $nl ; }
    print_skips();
    if (! defined $nofilename) {
        print_filename();
    }
    print color($wrncolor), $_, ;
}

sub handle_info {
    # handle info
    #  nl (optional)         : nextline value
    #  colorattr             : additional color attribute
    my ( $nl, $colorattr ) = @_;
    if (defined $nl) { $nextlines = $nl ; }
    print_skips();
    if (defined $colorattr) {print color($infcolor, $colorattr), $_, ; }
    else { print color($infcolor), $_, ; }
}

# print help
pod2usage(-verbose => 1, -exitval => 0) if $showhelp;

# print manual
pod2usage(-verbose => 2, -exitval => 0, -noperldoc => 1) if $showman;

# print version
pod2usage(-verbose => 99, -exitval => 0, -sections => ["VERSION"])
    if $showversion;

# main loop
while ( <<>> ) {

    # assume currentfile is given in the last open parenthesis
    # (works badly for packages)
    if (/^[^(]*\)/) {$currentfile = ""; $lastfile = "";}
    if (/\(([^)]+($extpattern))[^)]*$/) {$currentfile = $1;}

    # try to show usefull lines following some warnings or errors (starting with
    # whitespaces or package name)
    if ($currentpackage ne ""){
        if (/^(\s{2}|\(\Q$currentpackage\E\))/){$nextlines = 1;}
        else {$currentpackage = ""; }
    }

    # print above lines, if required
    if ($nextlines > 0) { $nextlines--; print $_; }

    # find errors
    elsif (/^(!\s+|.*?:\d+:\s+)?(Class|Package)\s+(\S+)?\s*Error/i)     { if ($3){$currentpackage=$3}; handle_error(0, "nofilename"); }
    elsif (/^(!\s+)?LaTeX (?:Error)/i)                                  { handle_error(); }
    elsif (/^(!|.*?:\d+:) Undefined control sequence\./i)               { chomp; handle_error(4); }
    elsif (/^(!|.*?:\d+:) Use of (.*) doesn't match its definition\./i) { handle_error(3); }
    elsif (/^(!|.*?:\d+:) Missing/i)                                    { handle_error(3); }
    elsif (/^(!|.*?:\d+:)/i)                                            { handle_error(); }
    elsif (/^No pages of output/i)                                      { handle_error(); }

    # find warnings
    elsif (/^(!\s+)?(Class|Package)\s+(\S+)?\s*Warning/i)               { if ($3){$currentpackage=$3}; handle_warning(0, "nofilename"); }
    elsif (/^(!\s+)?(LaTeX|\* LaTeX) Warning: (Citation|Reference)/i)   { if ($showrefmsg) {handle_warning();} }
    elsif (/^(!\s+)?(LaTeX|\* LaTeX) Font Warning/i)                    { $currentpackage="Font"; handle_warning(); }
    elsif (/^(!\s+)?(LaTeX|\* LaTeX) Warning/i)                         { handle_warning(); }
    elsif (/Runaway (definition|argument|preamble|text)\?/i)            { handle_warning(1); }
    elsif (/Missing character: There is no /i)                          { handle_warning(); }
    elsif (/(overfull|underfull|badbox)/i)                              { if ($showboxmsg){handle_warning(1);} }

    # find infos
    elsif (/^(!\s+)?(Class|Package)\s+(\S+)?\s*Info/i)                  { if ($showinfomsg){ if ($3){$currentpackage=$3}; handle_info(); } }
    elsif (/^(!\s+)?(LaTeX|\* LaTeX) Font Info/i)                       { if ($showinfomsg){ $currentpackage="Font"; handle_info(); } }
    elsif (/^(LaTeX) (\w+ )?Info/i)                                     { if ($showinfomsg){handle_info();} }
    elsif (/^(LaTeX)/i)                                                 { handle_info(); }
    elsif (/^Document Class/i)                                          { handle_info(); }
    elsif (/^\\input/)                                                  { handle_info(); }
    elsif (/^Output written/i)                                          { print "\n"; handle_info(0, "bold"); }

    else {
        # user added filters
        my $line=$_;
        my $usrmatch=0;
        for my $filter (@userfilters){
            if ($line =~ m/$filter/ && ! $usrmatch) {
                $usrmatch = 1;
                print_skips();
                print color($usrcolor), $line;
            }
        }
        # if not, skip line
        if (! $usrmatch){
            $samecontext=0;
            print color("reset");
            if ($fulllog ){ print $line, ; }
        }
    }
}

# end message
print "\n";
if ($nerr > 0) {print color("bold"), color($errcolor), "Found ", $nerr, " error(s)\n", color("reset") ;}
if ($nwrn > 0) {print color("bold"), color($wrncolor), "Found ", $nwrn, " warning(s)\n", color("reset") ;}

__END__

=pod

=encoding utf8

=head1 NAME

texlogfilter - filter latex engines output or log file

=head1 SYNOPSIS

    texlogfilter [options] file.log

=head1 DESCRIPTION

Filter latex engines output or log file. For latex, pdflatex, lualatex or
xelatex output or log file.

Without input file, standard input is used.  Use on latex engine output with:

    latex file.tex < /dev/null | texlogfilter

=head1 OPTIONS

    --help                 : print this help and exit
    --manual               : print the manual and exit
    --version              : print version and exit

    --box                  : show box warnings
    --no-box               : mask box warnings

    --ref                  : show reference/citation warnings
    --no-ref               : mask reference/citation warnings

    --info                 : show latex info messages
    --no-info              : mask latex info messages

    --filename             : print current file name
    --no-filename          : do not print current file name
    --files-ext=string     : regex pattern used to match files extension
                             (default: tex|sty|cls|bib|fd|bst|bbx|cbx|def|clo)

    --skips                : indicate skipped lines
    --no-skips             : mask skipped lines
    --skip-marker=string   : marker used to indicate skipped lines
                             (default: ...)

    --add-filter=string    : add user filter pattern (as perl regular
                             expression)

    --full-log             : show full log (don't filter - only colorize
                             latex engine output)

    --errors-color=string
    --warnings-color=string
    --infos-color=string
    --skips-color=string
    --user-color=string    : colors used for errors, warnings, informations
                             and skipped lines

=head1 ALTERNATIVES

=over

=item * B<texlog-extract>: L<https://www.ctan.org/pkg/texlog-extract>

=item * B<texloganalyser>: L<https://www.ctan.org/pkg/texloganalyser>

=item * B<texlogsieve>: L<https://ctan.org/pkg/texlogsieve>

=item * B<texfot>: L<https://ctan.org/pkg/texfot>

=back

=head1 AUTHOR

Written by Julien Labbé

=over

L<https://github.com/julienlabbe/latex-packages>.

=back

Inspired from a script given by Martin Scharrer on Stack Exchange network (see
L<https://tex.stackexchange.com/a/10564>).

=head1 LICENCE

This work may be distributed and/or modified under the conditions of the LaTeX
Project Public License, either version 1.3 of this license or (at your option)
any later version. The latest version of this license is in
L<http://www.latex-project.org/lppl.txt> and version 1.3 or later is part of all
distributions of LaTeX version 2005/12/01 or later.

=head1 VERSION

1.5

=head1 HISTORY

=over

=item * 2026, November, version 1.5: rewrite documentation with Pod::Usage;
add --manual option; improve script security (exit for unknown option, validate
colors, escape package names in regex, use safe file operator).

=item * 2024, June, version 1.4: add warning patterns.

=item * 2024, February, versions 1.3 and 1.3.1: better detect the current file name.

=item * 2024, February, version 1.2: fix duplicate or missing lines; handle
package and class names with hyphen; add info and warning patterns for fonts.

=item * 2022, March, version 1.1: add --info and --no-info options.

=item * 2021, November, version 1.0: initial version.

=back

=cut

=head1 BUG TRACKER

L<https://github.com/julienlabbe/latex-packages/issues>

=head1 DOCUMENTATION

The documentation is integrated, written in Plain Old Documentation (POD)
format. Exported versions are available, in the following files:

=over

=item * F<README>: created with perldoc.

=item * F<texlogfilter.html>: created with pod2html.

=back

=cut
