Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100755 282 lines (223 sloc) 8.836 kB
3ebfa07 @abw Initial revision
authored
1 #!/usr/bin/perl -w
2 #========================================================================
3 #
4 # tpage
5 #
6 # DESCRIPTION
7 # Script for processing and rendering a template document using the
8 # Perl Template Toolkit.
9 #
10 # AUTHOR
82cbea8 @abw version 2.00
authored
11 # Andy Wardley <abw@kfs.org>
3ebfa07 @abw Initial revision
authored
12 #
13 # COPYRIGHT
82cbea8 @abw version 2.00
authored
14 # Copyright (C) 1996-2000 Andy Wardley. All Rights Reserved.
15 # Copyright (C) 1998-2000 Canon Research Centre Europe Ltd.
3ebfa07 @abw Initial revision
authored
16 #
17 # This module is free software; you can redistribute it and/or
18 # modify it under the same terms as Perl itself.
19 #
20 #------------------------------------------------------------------------
21 #
22 # $Id$
23 #
24 #========================================================================
25
26 use strict;
27 use Template;
1914844 @mremy Add $Template::Directive::WHILE_MAX option
mremy authored
28 use Template::Directive;
6b6c081 @abw * added full range of command line options
authored
29 use AppConfig;
3ebfa07 @abw Initial revision
authored
30
6b6c081 @abw * added full range of command line options
authored
31 my $NAME = "tpage";
f77d28d @abw hard-coded version number for ttree and tpage now that we no longer h…
authored
32 my $VERSION = 2.70;
6b6c081 @abw * added full range of command line options
authored
33 my $HOME = $ENV{ HOME } || '';
34 my $RCFILE = $ENV{"\U${NAME}rc"} || "$HOME/.${NAME}rc";
35 my $TTMODULE = 'Template';
36
37 # read .tpagerc file and any command line arguments
38 my $config = read_config($RCFILE);
39
40 # unshift any perl5lib directories onto front of INC
41 unshift(@INC, @{ $config->perl5lib });
42
1914844 @mremy Add $Template::Directive::WHILE_MAX option
mremy authored
43 # get all template_directive_* options from the config
44 my %ttdirectiveopts = $config->varlist('^template_directive_', 1);
45
46 # get all template_* (except template_directive_*) options from the config and fold keys to UPPER CASE
47 my %ttopts = $config->varlist('^template_(?!directive_)', 1);
6b6c081 @abw * added full range of command line options
authored
48 my $ttmodule = delete($ttopts{ module });
49 my $ucttopts = {
50 map { my $v = $ttopts{ $_ }; defined $v ? (uc $_, $v) : () }
51 keys %ttopts,
52 };
53
54 # load custom template module
55 if ($ttmodule) {
56 my $ttpkg = $ttmodule;
57 $ttpkg =~ s[::][/]g;
58 $ttpkg .= '.pm';
59 require $ttpkg;
3ebfa07 @abw Initial revision
authored
60 }
6b6c081 @abw * added full range of command line options
authored
61 else {
62 $ttmodule = $TTMODULE;
d52480d @abw *** empty log message ***
authored
63 }
3ebfa07 @abw Initial revision
authored
64
1914844 @mremy Add $Template::Directive::WHILE_MAX option
mremy authored
65 # load custom Template::Directive configuration
66 map {
67 my $v = $ttdirectiveopts{ $_ };
68 if( defined $v ) {
69 ${ $Template::Directive::{ uc $_ } } = $ttdirectiveopts{ $_ }
70 }
71 } keys %ttdirectiveopts;
72
6b6c081 @abw * added full range of command line options
authored
73 # add current directory to INCLUDE_PATH
74 unshift(@{ $ucttopts->{ INCLUDE_PATH } }, '.');
75
3ebfa07 @abw Initial revision
authored
76 # read from STDIN if no files specified
77 push(@ARGV, '-') unless @ARGV;
78
6b6c081 @abw * added full range of command line options
authored
79 my $template = $ttmodule->new($ucttopts)
80 || die $ttmodule->error();
3ebfa07 @abw Initial revision
authored
81
82 # process each input file
83 foreach my $file (@ARGV) {
84 $file = \*STDIN if $file eq '-';
6b6c081 @abw * added full range of command line options
authored
85 $template->process($file)
3ebfa07 @abw Initial revision
authored
86 || die $template->error();
87 }
88
6b6c081 @abw * added full range of command line options
authored
89
90 sub read_config {
91 my $file = shift;
92
93 my $config = AppConfig->new(
94 {
95 ERROR => sub { die(@_, "\ntry `$NAME --help'\n") }
96 },
97 'help|h' => { ACTION => \&help },
98 'template_absolute|absolute' => { DEFAULT => 1 },
99 'template_relative|relative' => { DEFAULT => 1 },
100 'template_module|module=s',
101 'template_anycase|anycase',
102 'template_eval_perl|eval_perl',
103 'template_load_perl|load_perl',
104 'template_interpolate|interpolate',
105 'template_pre_chomp|pre_chomp|prechomp',
106 'template_post_chomp|post_chomp|postchomp',
107 'template_trim|trim',
108 'template_variables|variables|define=s%',
109 'template_include_path|include_path|include|I=s@',
110 'template_pre_process|pre_process|preprocess=s@',
111 'template_post_process|post_process|postprocess=s@',
112 'template_process|process=s',
113 'template_wrapper|wrapper=s',
114 'template_recursion|recursion',
115 'template_expose_blocks|expose_blocks',
116 'template_default|default=s',
117 'template_error|error=s',
118 'template_debug|debug=s',
119 'template_start_tag|start_tag|starttag=s',
120 'template_end_tag|end_tag|endtag=s',
121 'template_tag_style|tag_style|tagstyle=s',
122 'template_compile_ext|compile_ext=s',
123 'template_compile_dir|compile_dir=s',
124 'template_plugin_base|plugin_base|pluginbase=s@',
1914844 @mremy Add $Template::Directive::WHILE_MAX option
mremy authored
125 'perl5lib|perllib=s@',
126 'template_directive_debug',
127 'template_directive_pretty',
128 'template_directive_while_max|while_max=i'
6b6c081 @abw * added full range of command line options
authored
129 );
130
131 # add the 'file' option now that we have a $config object that we
132 # can reference in a closure
133 $config->define(
134 'file|f=s@' => {
135 EXPAND => AppConfig::EXPAND_ALL,
136 ACTION => sub {
137 my ($state, $item, $file) = @_;
138 $file = $state->cfg . "/$file"
139 unless $file =~ /^[\.\/]|(?:\w:)/;
140 $config->file($file) }
141 }
142 );
143
144 # process main config file, then command line args
145 $config->file($file) if -f $file;
146 $config->args();
147 return $config;
148 }
149
150
151 sub help {
152 print<<END_OF_HELP;
153 $NAME $VERSION (Template Toolkit version $Template::VERSION)
154
155 usage: $NAME [options] [files]
156
157 Options:
158 --define var=value Define template variable
159 --interpolate Interpolate '\$var' references in text
160 --anycase Accept directive keywords in any case.
161 --pre_chomp Chomp leading whitespace
162 --post_chomp Chomp trailing whitespace
163 --trim Trim blank lines around template blocks
164 --eval_perl Evaluate [% PERL %] ... [% END %] code blocks
165 --load_perl Load regular Perl modules via USE directive
166 --absolute Allow ABSOLUTE directories (enabled by default)
167 --relative Allow RELATIVE directories (enabled by default)
168 --include_path=DIR Add directory to INCLUDE_PATH
169 --pre_process=TEMPLATE Process TEMPLATE before each main template
170 --post_process=TEMPLATE Process TEMPLATE after each main template
171 --process=TEMPLATE Process TEMPLATE instead of main template
172 --wrapper=TEMPLATE Process TEMPLATE wrapper around main template
173 --default=TEMPLATE Use TEMPLATE as default
174 --error=TEMPLATE Use TEMPLATE to handle errors
175 --debug=STRING Set TT DEBUG option to STRING
176 --start_tag=STRING STRING defines start of directive tag
177 --end_tag=STRING STRING defined end of directive tag
178 --tag_style=STYLE Use pre-defined tag STYLE
179 --plugin_base=PACKAGE Base PACKAGE for plugins
180 --compile_ext=STRING File extension for compiled template files
181 --compile_dir=DIR Directory for compiled template files
182 --perl5lib=DIR Specify additional Perl library directories
183 --template_module=MODULE Specify alternate Template module
1914844 @mremy Add $Template::Directive::WHILE_MAX option
mremy authored
184 --while_max=INTEGER Change '\$Template::Directive::WHILE_MAX' default
6b6c081 @abw * added full range of command line options
authored
185
186 See 'perldoc tpage' for further information.
187
188 END_OF_HELP
189
190 exit(0);
191 }
192
c8d76d4 @abw *** empty log message ***
authored
193 __END__
3ebfa07 @abw Initial revision
authored
194
195
c8d76d4 @abw *** empty log message ***
authored
196 #------------------------------------------------------------------------
197 # IMPORTANT NOTE
198 # This documentation is generated automatically from source
199 # templates. Any changes you make here may be lost.
200 #
201 # The 'docsrc' documentation source bundle is available for download
911a5e3 @abw *** empty log message ***
authored
202 # from http://www.template-toolkit.org/docs.html and contains all
c8d76d4 @abw *** empty log message ***
authored
203 # the source templates, XML files, scripts, etc., from which the
204 # documentation for the Template Toolkit is built.
205 #------------------------------------------------------------------------
3ebfa07 @abw Initial revision
authored
206
207 =head1 NAME
208
c8d76d4 @abw *** empty log message ***
authored
209 Template::Tools::tpage - Process templates from command line
3ebfa07 @abw Initial revision
authored
210
211 =head1 USAGE
212
d52480d @abw *** empty log message ***
authored
213 tpage [ --define var=value ] file(s)
3ebfa07 @abw Initial revision
authored
214
215 =head1 DESCRIPTION
216
217 The B<tpage> script is a simple wrapper around the Template Toolkit processor.
218 Files specified by name on the command line are processed in turn by the
219 template processor and the resulting output is sent to STDOUT and can be
d52480d @abw *** empty log message ***
authored
220 redirected accordingly. e.g.
3ebfa07 @abw Initial revision
authored
221
222 tpage myfile > myfile.out
223 tpage header myfile footer > myfile.html
224
225 If no file names are specified on the command line then B<tpage> will read
226 STDIN for input.
227
c8d76d4 @abw *** empty log message ***
authored
228 The C<--define> option can be used to set the values of template variables.
d52480d @abw *** empty log message ***
authored
229 e.g.
230
231 tpage --define author="Andy Wardley" skeleton.pm > MyModule.pm
232
d9a1a79 @mremy update tpage documentation
mremy authored
233 =head2 The F<.tpagerc> Configuration File
234
235 You can use a F<.tpagerc> file in your home directory.
236
237 The purpose of this file is to set any I<global> configuration options
238 that you want applied I<every> time F<tpage> is run. For example, you
239 can use the C<include_path> to use template files from a generic template
240 directory.
241
242 Run C<tpage -h> for a summary of the options available.
243
3ebfa07 @abw Initial revision
authored
244 See L<Template> for general information about the Perl Template
245 Toolkit and the template language and features.
246
247 =head1 AUTHOR
248
c942c1b @abw Version 2.15
authored
249 Andy Wardley E<lt>abw@wardley.orgE<gt>
3ebfa07 @abw Initial revision
authored
250
c942c1b @abw Version 2.15
authored
251 L<http://wardley.org/|http://wardley.org/>
c8d76d4 @abw *** empty log message ***
authored
252
f4df3b6 @abw approach v2.03
authored
253
254
255
c8d76d4 @abw *** empty log message ***
authored
256 =head1 VERSION
3ebfa07 @abw Initial revision
authored
257
d76e841 @abw * Fixed all the version numbers
authored
258 2.68, distributed as part of the
f77d28d @abw hard-coded version number for ttree and tpage now that we no longer h…
authored
259 Template Toolkit version 2.19, released on 27 April 2007.
3ebfa07 @abw Initial revision
authored
260
261 =head1 COPYRIGHT
262
76e8c08 @abw release version 2.18
authored
263 Copyright (C) 1996-2007 Andy Wardley. All Rights Reserved.
264
3ebfa07 @abw Initial revision
authored
265
266 This module is free software; you can redistribute it and/or
267 modify it under the same terms as Perl itself.
268
269 =head1 SEE ALSO
270
e6332cd @abw prep for version 2.12
authored
271 L<ttree|Template::Tools::ttree>
0958102 @abw version 2.12
authored
272
e6332cd @abw prep for version 2.12
authored
273 =cut
274
275 # Local Variables:
276 # mode: perl
277 # perl-indent-level: 4
278 # indent-tabs-mode: nil
279 # End:
280 #
281 # vim: expandtab shiftwidth=4:
Something went wrong with that request. Please try again.