[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

SF.net SVN: ledger-smb: [1612] trunk



Revision: 1612
          http://ledger-smb.svn.sourceforge.net/ledger-smb/?rev=1612&view=rev
Author:   tetragon
Date:     2007-09-15 15:22:41 -0700 (Sat, 15 Sep 2007)

Log Message:
-----------
Adjust templating to allow for no output file.
Due to their nature of the modules, PDF and PS will supply one if needed.

Modified Paths:
--------------
    trunk/LedgerSMB/Template/CSV.pm
    trunk/LedgerSMB/Template/HTML.pm
    trunk/LedgerSMB/Template/PDF.pm
    trunk/LedgerSMB/Template/PS.pm
    trunk/LedgerSMB/Template/TXT.pm
    trunk/LedgerSMB/Template.pm
    trunk/t/04-template-handling.t

Added Paths:
-----------
    trunk/t/var/

Modified: trunk/LedgerSMB/Template/CSV.pm
===================================================================
--- trunk/LedgerSMB/Template/CSV.pm	2007-09-15 16:04:59 UTC (rev 1611)
+++ trunk/LedgerSMB/Template/CSV.pm	2007-09-15 22:22:41 UTC (rev 1612)
@@ -75,7 +75,13 @@
 	my $parent = shift;
 	my $cleanvars = shift;
 	my $template;
+	my $output;
 
+	if ($parent->{outputfile}) {
+		$output = "$parent->{outputfile}.csv";
+	} else {
+		$output = \$parent->{output};
+	}
 	$template = Template->new({
 		INCLUDE_PATH => $parent->{include_path},
 		START_TAG => quotemeta('<?lsmb'),
@@ -89,7 +95,7 @@
 		get_template($parent->{template}), 
 		{%$cleanvars, %$LedgerSMB::Template::TTI18N::ttfuncs,
 			'escape' => \&preprocess},
-		"$parent->{outputfile}.csv", binmode => ':utf8')) {
+		$output, binmode => ':utf8')) {
 		throw Error::Simple $template->error();
 	}
 	$parent->{mimetype} = 'text/csv';
@@ -97,8 +103,8 @@
 
 sub postprocess {
 	my $parent = shift;
-	$parent->{rendered} = "$parent->{outputfile}.csv";
-	return "$parent->{outputfile}.csv";
+	$parent->{rendered} = "$parent->{outputfile}.csv" if $parent->{outputfile};
+	return $parent->{rendered};
 }
 
 1;

Modified: trunk/LedgerSMB/Template/HTML.pm
===================================================================
--- trunk/LedgerSMB/Template/HTML.pm	2007-09-15 16:04:59 UTC (rev 1611)
+++ trunk/LedgerSMB/Template/HTML.pm	2007-09-15 22:22:41 UTC (rev 1612)
@@ -75,7 +75,13 @@
 	my $parent = shift;
 	my $cleanvars = shift;
 	my $template;
+	my $output;
 	
+	if ($parent->{outputfile}) {
+		$output = "$parent->{outputfile}.html";
+	} else {
+		$output = \$parent->{output};
+	}
 	$template = Template->new({
 		INCLUDE_PATH => $parent->{include_path},
 		START_TAG => quotemeta('<?lsmb'),
@@ -89,7 +95,7 @@
 		get_template($parent->{template}), 
 		{%$cleanvars, %$LedgerSMB::Template::TTI18N::ttfuncs,
 			'escape' => \&preprocess},
-		"$parent->{outputfile}.html", binmode => ':utf8')) {
+		$output, binmode => ':utf8')) {
 		throw Error::Simple $template->error();
 	}
 	$parent->{mimetype} = 'text/html';
@@ -97,8 +103,8 @@
 
 sub postprocess {
     my $parent = shift;
-    $parent->{rendered} = "$parent->{outputfile}.html";
-    return "$parent->{outputfile}.html";
+    $parent->{rendered} = "$parent->{outputfile}.html" if $parent->{outputfile};
+    return $parent->{rendered};
 }
 
 1;

Modified: trunk/LedgerSMB/Template/PDF.pm
===================================================================
--- trunk/LedgerSMB/Template/PDF.pm	2007-09-15 16:04:59 UTC (rev 1611)
+++ trunk/LedgerSMB/Template/PDF.pm	2007-09-15 22:22:41 UTC (rev 1612)
@@ -74,6 +74,8 @@
 	my $parent = shift;
 	my $cleanvars = shift;
 	my $template;
+	$parent->{outputfile} ||=
+		"${LedgerSMB::Sysconfig::tempdir}/$parent->{template}-output-$$";
 
 	$template = Template::Latex->new({
 		LATEX_FORMAT => 'pdf',

Modified: trunk/LedgerSMB/Template/PS.pm
===================================================================
--- trunk/LedgerSMB/Template/PS.pm	2007-09-15 16:04:59 UTC (rev 1611)
+++ trunk/LedgerSMB/Template/PS.pm	2007-09-15 22:22:41 UTC (rev 1612)
@@ -74,6 +74,8 @@
 	my $parent = shift;
 	my $cleanvars = shift;
 	my $template;
+	$parent->{outputfile} ||=
+		"${LedgerSMB::Sysconfig::tempdir}/$parent->{template}-output-$$";
 
 	$template = Template::Latex->new({
 		LATEX_FORMAT => 'ps',

Modified: trunk/LedgerSMB/Template/TXT.pm
===================================================================
--- trunk/LedgerSMB/Template/TXT.pm	2007-09-15 16:04:59 UTC (rev 1611)
+++ trunk/LedgerSMB/Template/TXT.pm	2007-09-15 22:22:41 UTC (rev 1612)
@@ -56,7 +56,13 @@
 	my $parent = shift;
 	my $cleanvars = shift;
 	my $template;
+	my $output;
 
+	if ($parent->{outputfile}) {
+		$output = "$parent->{outputfile}.txt";
+	} else {
+		$output = \$parent->{output};
+	}
 	$template = Template->new({
 		INCLUDE_PATH => $parent->{include_path},
 		START_TAG => quotemeta('<?lsmb'),
@@ -70,7 +76,7 @@
 		get_template($parent->{template}), 
 		{%$cleanvars, %$LedgerSMB::Template::TTI18N::ttfuncs,
 			'escape' => \&preprocess},
-		"$parent->{outputfile}.txt", binmode => ':utf8')) {
+		$output, binmode => ':utf8')) {
 		throw Error::Simple $template->error();
 	}
 	$parent->{mimetype} = 'text/plain';
@@ -78,8 +84,8 @@
 
 sub postprocess {
     my $parent = shift;
-    $parent->{rendered} = "$parent->{outputfile}.txt";
-    return "$parent->{outputfile}.txt";
+    $parent->{rendered} = "$parent->{outputfile}.txt" if $parent->{outputfile};
+    return $parent->{rendered};
 }
 
 1;

Modified: trunk/LedgerSMB/Template.pm
===================================================================
--- trunk/LedgerSMB/Template.pm	2007-09-15 16:04:59 UTC (rev 1611)
+++ trunk/LedgerSMB/Template.pm	2007-09-15 22:22:41 UTC (rev 1612)
@@ -11,7 +11,7 @@
 
 =over
 
-=item new(user => \%myconfig, template => $string, format => $string, [locale => $locale] [language => $string], [include_path => $path], [no_auto_output => $bool], [method => $string], [no_escape => $bool], [debug => $bool] );
+=item new(user => \%myconfig, template => $string, format => $string, [locale => $locale] [language => $string], [include_path => $path], [no_auto_output => $bool], [method => $string], [no_escape => $bool], [debug => $bool], [output_file => $string] );
 
 This command instantiates a new template:
 
@@ -68,11 +68,19 @@
 
 The output method to use, defaults to HTTP.  Media is a synonym for method
 
+=item output_file (optional)
+
+The base name of the file for output.
+
 =back
 
 =item render($hashref)
 
-This command renders the template and writes the result to standard output.  
+This command renders the template.  If no_auto_output was not specified during
+instantiation, this also writes the result to standard output and exits.
+Otherwise it returns the name of the output file if a file was created.  When
+no output file is created, the output is held in $self->{output}.
+
 Currently email and server-side printing are not supported.
 
 =item output
@@ -112,16 +120,12 @@
 	$self->{language} = $args{language};
 	$self->{no_escape} = $args{no_escape};
 	$self->{debug} = $args{debug};
-	if ($args{outputfile}) {
-		$self->{outputfile} =
-			"${LedgerSMB::Sysconfig::tempdir}/$args{outputfile}";
-	} else {
-		$self->{outputfile} =
-			"${LedgerSMB::Sysconfig::tempdir}/$args{template}-output-$$";
-	}
+	$self->{outputfile} =
+		"${LedgerSMB::Sysconfig::tempdir}/$args{output_file}" if
+		$args{output_file};
 	$self->{include_path} = $args{path};
 	$self->{locale} = $args{locale};
-	$self->{noauto} = $args{noauto};
+	$self->{noauto} = $args{no_auto_output};
 	$self->{method} = $args{method};
 	$self->{method} ||= $args{media};
 
@@ -191,13 +195,18 @@
 		$self->_email_output;
 	} elsif ('print' eq lc $method) {
 		$self->_lpr_output;
+	} elsif (defined $self->{output}) {
+		$self->_http_output;
+		exit;
 	} else {
-		$self->_http_output;
+		$self->_http_output_file;
 	}
 }
 
 sub _http_output {
 	my $self = shift;
+	my $data = shift;
+	$data ||= $self->{output};
 	my $FH;
 
 	if ($self->{mimetype} =~ /^text/) {
@@ -205,6 +214,15 @@
 	} else {
 		print "Content-Type: $self->{mimetype}\n\n";
 	}
+	binmode STDOUT, ':bytes';
+	print $data;
+	binmode STDOUT, ':utf8';
+}
+
+sub _http_output_file {
+	my $self = shift;
+	my $FH;
+
 	open($FH, '<:bytes', $self->{rendered}) or
 		throw Error::Simple 'Unable to open rendered file';
 	my $data;
@@ -213,9 +231,9 @@
 		$data = <$FH>;
 	}
 	close($FH);
-	binmode STDOUT, ':bytes';
-	print $data;
-	binmode STDOUT, ':utf8';
+	
+	$self->_http_output($data);
+	
 	unlink($self->{rendered}) or
 		throw Error::Simple 'Unable to delete output file';
 	exit;

Modified: trunk/t/04-template-handling.t
===================================================================
--- trunk/t/04-template-handling.t	2007-09-15 16:04:59 UTC (rev 1611)
+++ trunk/t/04-template-handling.t	2007-09-15 22:22:41 UTC (rev 1612)
@@ -173,7 +173,7 @@
 	'Template, new: Object creation with valid language has good include_path');
 $template = undef;
 $template = new LedgerSMB::Template('user' => $myconfig, 'language' => 'de',
-	'path' => 't/data', 'outputfile' => 'test');
+	'path' => 't/data', 'output_file' => 'test');
 ok(defined $template,
 	'Template, new: Object creation with valid language and path');
 isa_ok($template, 'LedgerSMB::Template', 
@@ -257,20 +257,10 @@
 is($template->{include_path}, 't/data',
 	'Template, new (TXT): Object creation with format and template');
 is($template->render({'login' => 'foo&bar'}),
-	"t/var/04-template-output-$$.txt",
-	'Template, render: Simple text template, default filename');
-ok(-e "t/var/04-template-output-$$.txt",
-	'Template, render (TXT): File created');
-open($FH, '<', "t/var/04-template-output-$$.txt");
..hidden.. = <$FH>;
-close($FH);
-chomp(@r);
-is(join("\n", @r), "I am a template.\nLook at me foo&bar.", 
+	undef,
+	'Template, render: Simple text template, no filename');
+is($template->{output}, "I am a template.\nLook at me foo&bar.\n", 
 	'Template, render (TXT): Simple TXT template, correct output');
-is(unlink("t/var/04-template-output-$$.txt"), 1,
-	'Template, render (TXT): removing testfile');
-ok(!-e "t/var/04-template-output-$$.txt",
-	'Template, render (TXT): testfile removed');
 
 $template = undef;
 $template = new LedgerSMB::Template('user' => $myconfig, 'format' => 'HTML', 
@@ -282,24 +272,14 @@
 is($template->{include_path}, 't/data',
 	'Template, new (HTML): Object creation with format and template');
 is($template->render({'login' => 'foo&bar'}),
-	"t/var/04-template-output-$$.html",
-	'Template, render (HTML): Simple HTML template, default filename');
-ok(-e "t/var/04-template-output-$$.html",
-	'Template, render (HTML): File created');
-open($FH, '<', "t/var/04-template-output-$$.html");
..hidden.. = <$FH>;
-close($FH);
-chomp(@r);
-is(join("\n", @r), "I am a template.\nLook at me foo&amp;bar.", 
+	undef,
+	'Template, render (HTML): Simple HTML template, no file');
+is($template->{output}, "I am a template.\nLook at me foo&amp;bar.", 
 	'Template, render (HTML): Simple HTML template, correct output');
-is(unlink("t/var/04-template-output-$$.html"), 1,
-	'Template, render (HTML): removing testfile');
-ok(!-e "t/var/04-template-output-$$.html",
-	'Template, render (HTML): testfile removed');
 
 $template = undef;
 $template = new LedgerSMB::Template('user' => $myconfig, 'format' => 'HTML', 
-	'template' => '04-gettext', 'outputfile' => '04-gettext',
+	'template' => '04-gettext', 'output_file' => '04-gettext',
 	'no_auto_output' => 1);
 ok(defined $template, 
 	'Template, new (HTML): Object creation with outputfile');
@@ -310,17 +290,18 @@
 is($template->render({'month' => 'June', 'login' => 'foo&bar', 
 	'fr' => $locale}), 't/var/04-gettext.html',
 	'Template, render (HTML): Gettext HTML template');
-ok(-e 't/var/04-gettext.html', 'Template, render (HTML): File created');
-open($FH, '<', 't/var/04-gettext.html');
+ok(-e "t/var/04-gettext.html",
+	'Template, render (HTML): File created');
+open($FH, '<', "t/var/04-gettext.html");
 @r = <$FH>;
 close($FH);
 chomp(@r);
 is(join("\n", @r), 
 	"I am a foo&amp;bar.\nLook at me Juin.\njuni\nAan foo&amp;bar", 
 	'Template, render (HTML): Gettext HTML template, correct output');
-is(unlink('t/var/04-gettext.html'), 1,
+is(unlink("t/var/04-gettext.html"), 1,
 	'Template, render (HTML): removing testfile');
-ok(!-e 't/var/04-gettext.html',
+ok(!-e "t/var/04-gettext.html",
 	'Template, render (HTML): testfile removed');
 
 ## XeTeX test, requires PDFLATEX to be xelatex and modified Template::Latex


This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.