Skip to content

Commit

Permalink
initial implementation of -recorder (for latexmlpost)
Browse files Browse the repository at this point in the history
  • Loading branch information
xworld21 committed Jan 7, 2024
1 parent 340840b commit e2a1eb1
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 7 deletions.
23 changes: 17 additions & 6 deletions bin/latexmlpost
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ use LaTeXML::Util::ObjectDB;
#======================================================================
# undef => unspecified; 0 = NO, 1 = YES
my ($help, $showversion, $verbosity, $validate, $omit_doctype) = (0, 0, 0, 1, 0);
my ($sourcedir, $destination, $logfile) = (undef, undef, undef);
my ($sourcedir, $destination, $logfile, $recorder, $recordfile) = (undef, undef, undef, undef, undef);
my @paths = ();
my ($format, $extension, $is_html, $urlstyle) = (undef, undef, undef, 'server');
my ($numbersections) = (1);
Expand Down Expand Up @@ -58,11 +58,13 @@ my $splitback = [qw(bibliography appendix index)];

# Get the command line arguments.
GetOptions("quiet" => sub { $verbosity--; },
"verbose" => sub { $verbosity++; },
"VERSION" => \$showversion,
"help|?" => \$help,
"log=s" => \$logfile,
"debug=s" => sub { no strict 'refs'; $LaTeXML::DEBUG{ lc($_[1]) } = 1; },
"verbose" => sub { $verbosity++; },
"VERSION" => \$showversion,
"help|?" => \$help,
"log=s" => \$logfile,
"recorder" => \$recorder,
"recordfile=s" => \$recordfile,
"debug=s" => sub { no strict 'refs'; $LaTeXML::DEBUG{ lc($_[1]) } = 1; },
# Source Options
"sourcedirectory=s" => \$sourcedir,
"validate!" => \$validate,
Expand Down Expand Up @@ -165,6 +167,7 @@ if (my @fails = validate_args()) {
my $starttime = StartTime();
my $latexmlpost;
eval { # Catch errors
UseRecordFile($recordfile);
UseLog($logfile);
Note($LaTeXML::IDENTITY . ($prescan ? " scanning " : " paginating ") . $xmlfile);

Expand Down Expand Up @@ -214,6 +217,7 @@ Note("Postprocessing " . ($code == 3 ? 'failed' : 'complete') . " " . $status .
UseLog(undef);
CheckDebuggable();
UseSTDERR(undef);
UseRecordFile(undef);
exit($code == 3 ? 1 : 0);

#======================================================================
Expand Down Expand Up @@ -247,6 +251,13 @@ sub validate_args {
$logfile = pathname_make(dir => pathname_cwd(), name => $name, type => 'latexmlpost.log'); } }
$logfile = 'latexmlpost.log' unless $logfile;

# Optionally create a .fls file for tracking the opening of files (option --recorder)
# Use $jobname.latexmlpost.fls, latexmlpost.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 => 'latexmlpost.fls') : 'latexmlpost.fls'; }

#======================================================================
# Sanity check and Completion of options.
#======================================================================
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 @@ -228,6 +228,7 @@ sub initialize_catalogs {
return if $catalogs_initialized;
$catalogs_initialized = 1;
foreach my $catalog (pathname_findall('LaTeXML.catalog', installation_subdir => '.')) {
RecordInput($catalog);
XML::LibXML->load_catalog($catalog); }
return; }

Expand Down
11 changes: 11 additions & 0 deletions lib/LaTeXML/Common/XML/XSLT.pm
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
package LaTeXML::Common::XML::XSLT;
use strict;
use warnings;
use LaTeXML::Common::Error;
use URI;
use URI::Escape;
use XML::LibXSLT;

sub new {
Expand All @@ -20,6 +23,10 @@ sub new {
LaTeXML::Common::XML::initialize_catalogs();
# LaTeXML::Common::XML::initialize_input_callbacks($xslt,installation_subdir => 'resources/XSLT');
# Do we still need this logic, if callbacks work?
my $input_callbacks = XML::LibXML::InputCallback->new();
$input_callbacks->register_callbacks([sub { RecordInput(uri_unescape(URI->new($_[0])->path)); return 0; }, undef, undef, undef]);
$xslt->input_callbacks($input_callbacks);

if (!ref $stylesheet) {
$stylesheet = LaTeXML::Common::XML::Parser->new()->parseFile($stylesheet); }
# $stylesheet = $xslt->parse_stylesheet_file($stylesheet); }
Expand All @@ -31,5 +38,9 @@ sub transform {
my ($self, $document, %params) = @_;
return $$self{stylesheet}->transform($document, %params); }

sub security_callbacks {
my ($self, $security) = @_;
return $$self{stylesheet}->security_callbacks($security); }

#======================================================================
1;
2 changes: 2 additions & 0 deletions lib/LaTeXML/Post/MakeBibliography.pm
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ package LaTeXML::Post::MakeBibliography;
use strict;
use warnings;
use LaTeXML::Util::Pathname;
use LaTeXML::Common::Error;
use LaTeXML::Common::XML;
use LaTeXML::Util::Radix;
use charnames qw(:full);
Expand Down Expand Up @@ -155,6 +156,7 @@ sub getBibliographies {
$raw .= $rawbib; }
else {
# TODO: Is this a memory concern for large bib files?
RecordInput($rawbib);
if (open(my $bibfh, '<', $rawbib)) {
$raw .= join("", <$bibfh>);
close $bibfh; }
Expand Down
2 changes: 2 additions & 0 deletions lib/LaTeXML/Post/Writer.pm
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ package LaTeXML::Post::Writer;
use strict;
use warnings;
use LaTeXML::Util::Pathname;
use LaTeXML::Common::Error;
use LaTeXML::Common::XML;
use LaTeXML::Post;
use base qw(LaTeXML::Post::Processor);
Expand Down Expand Up @@ -54,6 +55,7 @@ sub process {
or return Fatal('I/O', $destdir, undef, "Couldn't create directory '$destdir'",
"Response was: $!");
my $OUT;
RecordOutput($destination);
open($OUT, '>', $destination)
or return Fatal('I/O', $destdir, undef, "Couldn't write '$destination'", "Response was: $!");
print $OUT $serialized;
Expand Down
11 changes: 10 additions & 1 deletion lib/LaTeXML/Post/XSLT.pm
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,12 @@ sub process {
if (my $icon = $params{ICON}) {
# Hmm.... what type? could be various image types
$params{ICON} = '"' . $self->copyResource($doc, $icon, undef) . '"'; }

my $security = XML::LibXSLT::Security->new;
$security->register_callback(write_file => sub { RecordOutput($_[1]); return 1; });
$security->register_callback(create_dir => sub { RecordOutput($_[1]); return 1; });
$$self{stylesheet}->security_callbacks($security);

my $newdoc = $doc->new($$self{stylesheet}->transform($doc->getDocument, %params));
return $newdoc; }

Expand Down Expand Up @@ -115,7 +121,10 @@ sub copyResource {
if (!pathname_is_contained($dest, $doc->getSiteDirectory)) {
$dest = pathname_make(dir => $doc->getSiteDirectory, name => $name, type => $ex); } }
# Now, copy (unless in same place! happens a lot during testing!!!!)
pathname_copy($path, $dest) unless $path eq $dest;
if (!($path eq $dest)) {
RecordInput($path);
pathname_copy($path, $dest);
RecordOutput($dest); }
# and return the relative path from the dest doc to the resource
return pathname_relative($dest, $doc->getDestinationDirectory); }
else {
Expand Down

0 comments on commit e2a1eb1

Please sign in to comment.