#!/home/grinnz/projects/cpandoc-browser/perls/5.40.0/bin/perl -w #======================================================================== # # tpage # # DESCRIPTION # Script for processing and rendering a template document using the # Perl Template Toolkit. # # AUTHOR # Andy Wardley # # COPYRIGHT # Copyright (C) 1996-2000 Andy Wardley. All Rights Reserved. # Copyright (C) 1998-2000 Canon Research Centre Europe Ltd. # # This module is free software; you can redistribute it and/or # modify it under the same terms as Perl itself. # #------------------------------------------------------------------------ # # $Id$ # #======================================================================== use strict; use Template; use Template::Directive; use AppConfig; my $NAME = "tpage"; my $VERSION = 2.70; my $HOME = $ENV{ HOME } || ''; my $RCFILE = $ENV{"\U${NAME}rc"} || "$HOME/.${NAME}rc"; my $TTMODULE = 'Template'; # read .tpagerc file and any command line arguments my $config = read_config($RCFILE); # unshift any perl5lib directories onto front of INC unshift(@INC, @{ $config->perl5lib }); # get all template_directive_* options from the config my %ttdirectiveopts = $config->varlist('^template_directive_', 1); # get all template_* (except template_directive_*) options from the config and fold keys to UPPER CASE my %ttopts = $config->varlist('^template_(?!directive_)', 1); my $ttmodule = delete($ttopts{ module }); my $ttenvvars = delete($ttopts{ envvars }); my $ucttopts = { map { my $v = $ttopts{ $_ }; defined $v ? (uc $_, $v) : () } keys %ttopts, }; # load custom template module if ($ttmodule) { my $ttpkg = $ttmodule; $ttpkg =~ s[::][/]g; $ttpkg .= '.pm'; require $ttpkg; } else { $ttmodule = $TTMODULE; } # load custom Template::Directive configuration map { my $v = $ttdirectiveopts{ $_ }; if( defined $v ) { ${ $Template::Directive::{ uc $_ } } = $ttdirectiveopts{ $_ } } } keys %ttdirectiveopts; # add current directory to INCLUDE_PATH unshift(@{ $ucttopts->{ INCLUDE_PATH } }, '.'); # read from STDIN if no files specified push(@ARGV, '-') unless @ARGV; my $template = $ttmodule->new($ucttopts) || die $ttmodule->error(); my %ttvars = (); if ($ttenvvars) { $ttvars{'env'} = \%ENV; } # process each input file foreach my $file (@ARGV) { $file = \*STDIN if $file eq '-'; $template->process($file,\%ttvars) || die $template->error(); } sub read_config { my $file = shift; my $config = AppConfig->new( { ERROR => sub { die(@_, "\ntry `$NAME --help'\n") } }, 'help|h' => { ACTION => \&help }, 'template_absolute|absolute' => { DEFAULT => 1 }, 'template_relative|relative' => { DEFAULT => 1 }, 'template_module|module=s', 'template_anycase|anycase', 'template_eval_perl|eval_perl', 'template_load_perl|load_perl', 'template_interpolate|interpolate', 'template_pre_chomp|pre_chomp|prechomp', 'template_post_chomp|post_chomp|postchomp', 'template_trim|trim', 'template_variables|variables|define=s%', 'template_include_path|include_path|include|I=s@', 'template_pre_process|pre_process|preprocess=s@', 'template_post_process|post_process|postprocess=s@', 'template_process|process=s', 'template_wrapper|wrapper=s', 'template_recursion|recursion', 'template_expose_blocks|expose_blocks', 'template_default|default=s', 'template_error|error=s', 'template_debug|debug=s', 'template_start_tag|start_tag|starttag=s', 'template_end_tag|end_tag|endtag=s', 'template_tag_style|tag_style|tagstyle=s', 'template_compile_ext|compile_ext=s', 'template_compile_dir|compile_dir=s', 'template_plugin_base|plugin_base|pluginbase=s@', 'perl5lib|perllib=s@', 'template_envvars|envvars', 'template_directive_debug', 'template_directive_pretty', 'template_directive_while_max|while_max=i' ); # add the 'file' option now that we have a $config object that we # can reference in a closure $config->define( 'file|f=s@' => { EXPAND => AppConfig::EXPAND_ALL, ACTION => sub { my ($state, $item, $file) = @_; $file = $state->cfg . "/$file" unless $file =~ /^[\.\/]|(?:\w:)/; $config->file($file) } } ); # process main config file, then command line args $config->file($file) if -f $file; $config->args(); return $config; } sub help { print< script is a simple wrapper around the Template Toolkit processor. Files specified by name on the command line are processed in turn by the template processor and the resulting output is sent to STDOUT and can be redirected accordingly. e.g. tpage myfile > myfile.out tpage header myfile footer > myfile.html If no file names are specified on the command line then B will read STDIN for input. The C<--define> option can be used to set the values of template variables. e.g. tpage --define author="Andy Wardley" skeleton.pm > MyModule.pm =head2 The F<.tpagerc> Configuration File You can use a F<.tpagerc> file in your home directory. The purpose of this file is to set any I configuration options that you want applied I time F is run. For example, you can use the C to use template files from a generic template directory. Run C for a summary of the options available. See L