Skip to content

Commit

Permalink
initial implementation of -recorder (for latexml)
Browse files Browse the repository at this point in the history
  • Loading branch information
xworld21 committed Jan 7, 2024
1 parent 169d04c commit 340840b
Show file tree
Hide file tree
Showing 11 changed files with 70 additions and 2 deletions.
15 changes: 14 additions & 1 deletion bin/latexml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use Encode;
# Parse command line
my ($verbosity, $strict, $comments, $noparse, $includestyles) = (0, 0, 1, 0, 0);
my ($format, $destination, $help, $showversion) = ('xml', '');
my ($preamble, $postamble, $logfile) = (undef, undef, undef);
my ($preamble, $postamble, $logfile, $recorder, $recordfile) = (undef, undef, undef, undef, undef);
my ($documentid);
my $inputencoding;
my $mode = undef;
Expand All @@ -42,6 +42,8 @@ GetOptions("destination=s" => \$destination,
"verbose" => sub { $verbosity++; },
"strict" => \$strict,
"log=s" => \$logfile,
"recorder" => \$recorder,
"recordfile=s" => \$recordfile,
"xml" => sub { $format = 'xml'; },
"tex" => sub { $format = 'tex'; },
"box" => sub { $format = 'box'; },
Expand Down Expand Up @@ -77,6 +79,7 @@ my $latexml;
my $digested;
my $serialized;
eval { # Catch errors
UseRecordFile($recordfile);
UseLog($logfile);
Note("$LaTeXML::IDENTITY processing $source");

Expand Down Expand Up @@ -128,12 +131,15 @@ UseLog(undef);
if (!$serialized) { }
elsif ($destination) {
my $OUTPUT;
RecordOutput($destination);
open($OUTPUT, ">", $destination) or die "Couldn't open output file $destination: $!";
print $OUTPUT $serialized;
close($OUTPUT); }
else {
print $serialized; }

UseRecordFile(undef);

# ========================================
# Now, unbind stuff, so we can clear memory
$latexml = undef;
Expand Down Expand Up @@ -186,6 +192,13 @@ sub validate_args {
$logfile = pathname_make(dir => pathname_cwd(), name => $name, type => 'latexml.log'); } }
$logfile = 'latexml.log' unless $logfile;

# Optionally create a .fls file for tracking the opening of files (option --recorder)
# Use $jobname.latexml.fls, latexml.fls, or argument of --recordfile
$recordfile = $recorder ? $recordfile : undef;
if ($pathname && $recorder && !$recordfile) {
my ($dir, $name, $ext) = pathname_split($pathname);
$recordfile = $name ? pathname_make(dir => pathname_cwd(), name => $name, type => 'latexml.fls') : 'latexml.fls'; }

# Check that destination is valid before wasting any time...
if ($destination) {
$destination = pathname_canonical($destination);
Expand Down
40 changes: 40 additions & 0 deletions lib/LaTeXML/Common/Error.pm
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ our @EXPORT = (
qw(&MergeStatus),
# Run time reporting
qw(&StartTime &RunTime),
# Recording API
qw(&UseRecordFile &RecordInput &RecordOutput),
);

our $VERBOSITY = 0;
Expand Down Expand Up @@ -134,11 +136,49 @@ sub UseLog {
$log_count++;
return if $LOG or not($path); # already opened?
pathname_mkdir(pathname_directory($path)); # and hopefully no errors! :>
RecordOutput($path) if !$append || $log_count == 1;
open($LOG, ($append ? '>>' : '>'), $path) or die "Cannot open log file $path for writing: $!";
$LOG_PATH = $path;
binmode($LOG, ":encoding(UTF-8)"); }
return; }

#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
# Record file
our $RECORDFILE;
our $RECORDFILE_PATH;
# NOTE: same strategy as for $LOG
our $recordfile_count = 0;

sub UseRecordFile {
my ($path, $append) = @_;
if (!$path) { # Single false argument? Turn OFF and Close
$recordfile_count--;
return if !$RECORDFILE || $recordfile_count;
close($RECORDFILE) or die "Cannot close record file: $!";
$RECORDFILE = undef; }
else {
$recordfile_count++;
return if $RECORDFILE or not($path); # already opened?
pathname_mkdir(pathname_directory($path)); # and hopefully no errors! :>
my $is_empty = -z $path || !$append;
open($RECORDFILE, ($append ? '>>' : '>'), $path) or die "Cannot open record file $path for writing: $!";
$RECORDFILE_PATH = $path;
binmode($RECORDFILE, ":encoding(UTF-8)");
# first line must be current working directory
print $RECORDFILE "PWD " . pathname_cwd() . "\n" if $is_empty; }
return; }

# Recording API
sub RecordInput {
my ($filename) = @_;
print $RECORDFILE "INPUT $filename\n" unless !$RECORDFILE;
return; }

sub RecordOutput {
my ($filename) = @_;
print $RECORDFILE "OUTPUT $filename\n" unless !$RECORDFILE;
return; }

#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
# Low-level I/O

Expand Down
1 change: 1 addition & 0 deletions lib/LaTeXML/Common/Font/Metric.pm
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ sub read_tfm {
my $pathname = $$self{file};
# Read the TFM raw data.
my $TFM;
RecordInput($pathname);
if (!open($TFM, '<', $pathname)) {
Error('expected', $pathname, undef,
"Couldn't open TFM files '$pathname': $!");
Expand Down
1 change: 1 addition & 0 deletions lib/LaTeXML/Common/Model.pm
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ sub loadCompiledSchema {
my ($self, $file) = @_;
ProgressSpinup("Loading compiled schema $file");
my $MODEL;
RecordInput($file);
open($MODEL, '<', $file) or Fatal('I/O', $file, undef, "Cannot open Compiled Model $file for reading", $!);
my $line;
while ($line = <$MODEL>) {
Expand Down
1 change: 1 addition & 0 deletions lib/LaTeXML/Common/XML.pm
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ use strict;
use warnings;
use XML::LibXML qw(:all);
use XML::LibXML::XPathContext;
use LaTeXML::Common::Error;
use LaTeXML::Util::Pathname;
use Encode;
use Carp;
Expand Down
6 changes: 6 additions & 0 deletions lib/LaTeXML/Common/XML/Parser.pm
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,18 @@
package LaTeXML::Common::XML::Parser;
use strict;
use warnings;
use LaTeXML::Common::Error;
use URI;
use URI::Escape;
use XML::LibXML;

sub new {
my ($class) = @_;
my $parser = XML::LibXML->new();
$parser->validation(0);
my $input_callbacks = XML::LibXML::InputCallback->new();
$input_callbacks->register_callbacks([sub { RecordInput(uri_unescape(URI->new($_[0])->path)); return 0; }, undef, undef, undef]);
$parser->input_callbacks($input_callbacks);
return bless { parser => $parser }, $class; }

sub parseFile {
Expand Down
3 changes: 2 additions & 1 deletion lib/LaTeXML/Core/Mouth/Binding.pm
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ sub new {
my ($class, $pathname) = @_;
my ($dir, $name, $ext) = pathname_split($pathname);
my $self = bless { source => $pathname, shortsource => "$name.$ext" }, $class;
RecordInput($pathname);
ProgressSpinup("Loading $$self{source}");
return $self; }

Expand Down Expand Up @@ -65,7 +66,7 @@ sub stringify {

__END__
=pod
=pod
=head1 NAME
Expand Down
1 change: 1 addition & 0 deletions lib/LaTeXML/Core/Mouth/file.pm
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ sub new {
sub openFile {
my ($self, $pathname) = @_;
my $IN;
RecordInput($pathname);
if (!-r $pathname) {
Error('I/O', 'unreadable', $self, "File $pathname is not readable. Ignoring."); }
elsif ((!-z $pathname) && (-B $pathname)) {
Expand Down
1 change: 1 addition & 0 deletions lib/LaTeXML/Package.pm
Original file line number Diff line number Diff line change
Expand Up @@ -2660,6 +2660,7 @@ sub maybeRequireDependencies {
if (my $path = FindFile($file, type => $type, noltxml => 1)) {
local $/ = undef;
my $IN;
RecordInput($path);
if (open($IN, '<', $path)) {
my $code = <$IN>;
close($IN);
Expand Down
2 changes: 2 additions & 0 deletions lib/LaTeXML/Package/listings.sty.ltxml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ package LaTeXML::Package::Pool;
use strict;
use warnings;
use LaTeXML::Package;
use LaTeXML::Common::Error;
use MIME::Base64;
use Encode qw(is_utf8 encode);

Expand Down Expand Up @@ -321,6 +322,7 @@ sub listingsReadRawFile {
my $path = FindFile($filename);
my $text;
my $LST_FH;
RecordInput($filename) if $path;
if ($path && open($LST_FH, '<', $path)) {
{ local $/ = undef;
$text = <$LST_FH>;
Expand Down
1 change: 1 addition & 0 deletions lib/LaTeXML/Pre/BibTeX.pm
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ sub newFromFile {
"SEACHPATHS is " . join(', ', @$paths)) unless $file;
my $BIB;
$$self{file} = $bibname;
RecordInput($file);
open($BIB, '<', $file) or Fatal('I/O', $file, undef, "Can't open BibTeX $file for reading", $!);
$$self{lines} = [<$BIB>];
close($BIB);
Expand Down

0 comments on commit 340840b

Please sign in to comment.