LaTeX-Easy-Templates
view release on metacpan or search on metacpan
lib/LaTeX/Easy/Templates.pm view on Meta::CPAN
use File::Copy::Recursive qw/fmove rcopy/;
use Filesys::DiskUsage qw/du/;
use File::Which;
use Cwd qw/abs_path/;
use Data::Roundtrip qw/perl2dump no-unicode-escape-permanently/;
# these are the allowed parameters to be passed to
# LaTeX::Driver's constructor by the user of current module:
my @OPTIONS_ALLOWED_TO_BE_PASSED_TO_LATEX_DRIVER = qw/
format paths maxruns extraruns timeout indexstyle
indexoptions DEBUG DEBUGPREFIX
/;
# constructor,
sub new {
my $class = $_[0];
my $params = $_[1] // {};
my $parent = ( caller(1) )[3] || "N/A";
my $whoami = ( caller(0) )[3];
my $self = {
'_private' => {
'processors' => {
'templater_object' => undef, # in init
# stores all templates loaded into the templater and are known and need not be loaded (read) again
'loaded-info' => {},
},
# we use these defaults if anybody is missing this information
# for example 'processors' needs a latex output filename
# if that is missing, then we take it from here.
# these defaults can be set by the caller e.g. with params->{latex}->{...}
'options' => {
'latex' => {
# these will be used only if 'processor'->{id} does not define them
# default latex OUTPUT file
# produced via the latex template
'filename' => 'main.tex',
'latex-driver-parameters' => {
'format' => 'pdf(pdflatex)',
},
},
'debug' => {
'verbosity' => 0, # zero is mute
'cleanup' => 1, # 1: cleanup all tempfiles after exit including LaTeX::Driver's, 0: leave tempfiles after exit
},
'tempdir' => undef, # we will use a standard tempdir if none specified later
# if any of auxfiles' or basedir's total (recursively calculated) file size
# exceeds this limit, file copy will be aborted and untemplate() will fail
'max-size-for-filecopy' => 3*1024*1024, # bytes
# Saved parameters to be passed
# to the constructor of the templater (e.g. Text::Xslate)
# there are defaults below
# they can be overwritten by param: 'templater-parameters'
'templater-parameters' => {},
},
'log' => {
'logger_object' => undef,
},
},
};
bless $self => $class;
# NOTE: up until now we do not have a logger, we either use STDERR or die()
# do we have a logger specified in params?
if( exists($params->{'logfile'}) && defined($params->{'logfile'}) ){
my $adir = File::Basename::dirname($params->{'logfile'});
if( ! -d $adir ){ make_path($adir); if( ! -d $adir ){ print STDERR "${whoami} (via $parent), line ".__LINE__." : error, logfile directory '$adir' is not a dir or failed to be created.\n"; return undef } }
$self->log( Mojo::Log->new(path => $params->{'logfile'} ) )
} elsif( exists($params->{'logger_object'}) && defined($params->{'logger_object'}) ){
$self->log( $params->{'logger_object'} )
} else { $self->log( Mojo::Log->new() ) }
# Now we have a logger
my $log = $self->log();
my $options = $self->options();
# check for some required fields in params:
if( ! defined($self->options($params)) ){ $log->error(perl2dump($params)."${whoami} (via $parent), line ".__LINE__." : error, failed to parse input parameters, see above."); return undef }
my $verbosity = $self->verbosity();
if( $verbosity > 0 ){ $log->info("${whoami} (via $parent), line ".__LINE__." : called ...") }
# this will instantiate objects we store etc. (if any)
if( $self->init() ){ $log->error("${whoami} (via $parent), line ".__LINE__." : error, call to init() has failed."); return undef }
# user can set Text::Xslate constructor parameters via 'templater-parameters'
# these are our defaults
$options->{'templater-parameters'}->{'warn_handler'} = sub { $log->warn($_[0]) };
$options->{'templater-parameters'}->{'die_handler'} = sub { $log->error($_[0]); die $_[0] };
$options->{'templater-parameters'}->{'verbose'} = $verbosity;
# Text::Xslate syntax to use, it is one of Kolon or TTerse
# Kolon is used in all our tests!
$options->{'templater-parameters'}->{'syntax'} = 'Kolon';
# the suffix of template files
$options->{'templater-parameters'}->{'suffix'} = '.tx';
# stop silly-escaping for html or xml,
# this makes the use of mark_raw redundant ouph!
$options->{'templater-parameters'}->{'type'} = 'text';
# note: you can specify own functions or perl builtins
# to be called from inside the template
#'function' => {
# 'templatedir' => sub { # it takes an input param as input $args }
#}
# and make shallow copies of whatever params the user specified:
if( exists($params->{'templater-parameters'}) && defined($params->{'templater-parameters'}) ){
if( ref($params->{'templater-parameters'}) ne 'HASH' ){ $log->error("${whoami} (via $parent), line ".__LINE__." : error, input parameter 'templater-parameters' must be a HASHref."); return undef }
# warning: shallow copy!
for my $k (keys %{ $params->{'templater-parameters'} }){ $options->{'templater-parameters'}->{$k} = $params->{'templater-parameters'}->{$k} }
}
# required input parameter 'processors' must be a hash
# keys: id of the processor, just a name.
# values: ...
# this parameter contains all the template processors
if( ! exists($params->{'processors'}) || ! defined($params->{'processors'})
|| (ref($params->{'processors'})ne'HASH')
|| (scalar(keys %{ $params->{'processors'} })==0)
){ $log->error("${whoami} (via $parent), line ".__LINE__." : error, input parameter 'processors' was not specified or it was not a HASH or it was empty."); return undef }
( run in 2.229 seconds using v1.01-cache-2.11-cpan-b16cb0d3907 )