Re: [Templates] tpage extension

[prev] [thread] [next] [Date index for 2004/08/29]

From: Myk Melez
Subject: Re: [Templates] tpage extension
Date: 14:00 on 29 Aug 2004
This is a multi-part message in MIME format.
--------------050002090503050009010807
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit

Andy,

Any chance of rolling this patch into the next TT2 release?  It would 
prove very useful for a project I'm working on.  The patch adds 
command-line Template configuration options to tpage and applies cleanly 
to the current version of tpage in CVS, except for the last hunk, which 
just adds a newline to the end of the file and is no longer necessary.  
I've regenerated the patch without that last hunk and attached it.

-myk


Slaven Rezic wrote:

>Here's an extension for tpage to accept a number of Template config
>options. With this change one is able to say
>
>    tpage --pre_chomp=1 --post_chomp==1 -I/another/include/dir --debug=all ...
>
>Note that the command line arguments are lowercase and will be converted
>to uppercase options in the script.
>  
>


--------------050002090503050009010807
Content-Type: text/x-patch;
 name="tpage-options.diff"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
 filename="tpage-options.diff"

--- /usr/local/dist/cpan/build/Template-Toolkit-2.10/bin/tpage	2003-07-24 18:02:05.000000000 +0200
+++ tpage	2003-10-21 15:11:47.000000000 +0200
@@ -25,31 +25,54 @@
 
 use strict;
 use Template;
+use Getopt::Long;
 
-# look for -h or --help option, print usage and exit
-if (grep /^--?h(elp)?/, @ARGV) {
-    print "usage: tpage file [ file [...] ]\n";
-    exit 0;
+my %opt = (absolute => 1,
+	   relative => 1,
+	  );
+if (!GetOptions(\%opt, "h|help", "d|define=s@",
+		'start_tag=s', 'end_tag=s', 'tag_style=s',
+		'pre_chomp=s', 'post_chomp=s', 'trim=i',
+		'interpolate=i', 'anycase=i',
+		'include_path=s@', 'I=s@',
+		'delimiter=s',
+		'absolute=s', 'relative=s',
+		'default=s', 'recursion=s',
+		'eval_perl=s', 'pre_process=s@', 'post_process=s@',
+		'process=s', 'error=s', 'output=s', 'output_path=s',
+		'debug=s', 'debug_format=s', 'cache_size=s',
+		'compile_ext=s', 'compile_dir=s', 'plugin_base=s',
+		'load_perl=s', 'v1dollar=s', 'tolerant=s',
+		# BLOCKS, AUTO_RESET, VARIABLES, PRE_DEFINE, CONSTANTS,
+		# CONSTANT_NAMESPACE, NAMESPACE, ERRORS, PLUGINS,
+		# FILTERS, LOAD_TEMPLATES, LOAD_PLUGINS, LOAD_FILTERS,
+		# SERVICE, CONTEXT, STASH, PARSER, GRAMMAR missing
+	       )) {
+    usage();
 }
-my $vars = { };
-my ($var, $val);
+usage() if $opt{h};
 
-while ($ARGV[0] && $ARGV[0] =~ /^--?d(efine)?/) {
-    shift(@ARGV);
-    die "--define expect a 'variable=value' argument\n" 
-	unless defined ($var = shift(@ARGV));
-    ($var, $val) = split(/\s*=\s*/, $var, 2);
+my $vars = { };
+for (@{ $opt{d} }) {
+    my($var, $val) = split(/\s*=\s*/, $_, 2);
     $vars->{ $var } = $val;
 }
+delete $opt{d};
+
+# -I is an alias for -include_path
+push @{$opt{include_path}}, @{$opt{I}} if $opt{I} && @{$opt{I}};
+delete $opt{I};
+
+my %tmpl_config;
+while(my($k,$v) = each %opt) {
+    $tmpl_config{uc($k)} = $v;
+}
 
 # read from STDIN if no files specified
 push(@ARGV, '-') unless @ARGV;
 
-# create a template processor 
-my $template = Template->new({
-    ABSOLUTE => 1,
-    RELATIVE => 1,
-});
+# create a template processor
+my $template = Template->new(\%tmpl_config);
 
 # process each input file 
 foreach my $file (@ARGV) {
@@ -58,6 +81,11 @@ foreach my $file (@ARGV) {
 	|| die $template->error();
 }
 
+sub usage {
+    print "usage: tpage [--define var=val ...] [Template options] file [ file [...] ]\n";
+    exit 0;
+}
+
 __END__
 
 

--------------050002090503050009010807--

_______________________________________________
templates mailing list
templates@xxxxxxxxxxxxxxxx.xxx
http://lists.template-toolkit.org/mailman/listinfo/templates

(message missing)

Re: [Templates] tpage extension
Myk Melez 14:00 on 29 Aug 2004

Re: [Templates] tpage extension
Johan Vromans 15:07 on 29 Aug 2004

Re: [Templates] tpage extension
Andy Wardley 07:03 on 02 Sep 2004

Re: [Templates] tpage extension
Andy Wardley 07:42 on 02 Sep 2004

Re: [Templates] tpage extension
slaven 08:37 on 02 Sep 2004

Re: [Templates] tpage extension
Dylan Hardison 13:06 on 03 Sep 2004

[Templates] correct usage of Dumper plugin
Jamie Echlin 16:04 on 02 Sep 2004

Re: [Templates] correct usage of Dumper plugin
Jamie Echlin 16:39 on 02 Sep 2004

Re: [Templates] correct usage of Dumper plugin
Darren Chamberlain 16:45 on 02 Sep 2004

Re: [Templates] correct usage of Dumper plugin
Jamie Echlin 17:33 on 02 Sep 2004

Generated at 08:56 on 15 Mar 2005 by mariachi v0.52