summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortetragon <tetragon@4979c152-3d1c-0410-bac9-87ea11338e46>2007-10-02 02:54:28 +0000
committertetragon <tetragon@4979c152-3d1c-0410-bac9-87ea11338e46>2007-10-02 02:54:28 +0000
commit17ce0a83d97bede1a166b9841be7b1348477189a (patch)
tree87dcf66115b0c1f026178fa95329dfbcb514a12f
parent3e4fbb3cc4fa116159b9d10735eda8a78e4b0736 (diff)
Adding format_options and output_options template constructor arguments
Merging PDF and Postscript generation to the same file Adding DVI support git-svn-id: https://ledger-smb.svn.sourceforge.net/svnroot/ledger-smb/trunk@1684 4979c152-3d1c-0410-bac9-87ea11338e46
-rwxr-xr-xLedgerSMB/Template.pm27
-rwxr-xr-xLedgerSMB/Template/LaTeX.pm (renamed from LedgerSMB/Template/PDF.pm)36
-rw-r--r--LedgerSMB/Template/ODS.pm22
-rwxr-xr-xLedgerSMB/Template/PS.pm114
-rw-r--r--t/01-load.t4
-rw-r--r--t/04-template-handling.t43
6 files changed, 110 insertions, 136 deletions
diff --git a/LedgerSMB/Template.pm b/LedgerSMB/Template.pm
index ad48ad52..672078c0 100755
--- a/LedgerSMB/Template.pm
+++ b/LedgerSMB/Template.pm
@@ -26,6 +26,16 @@ in string form or the name of the file that is the template to be processed.
The format to be used. Currently HTML, PS, PDF, TXT and CSV are supported.
+=item format_options (optional)
+
+A hash of format-specific options. See the appropriate LSMB::T::foo for
+details.
+
+=item output_options (optional)
+
+A hash of output-specific options. See the appropriate output method for
+details.
+
=item locale (optional)
The locale object to use for regular gettext lookups. Having this option adds
@@ -122,7 +132,6 @@ sub new {
$self->{myconfig} = $args{user};
$self->{template} = $args{template};
$self->{format} = $args{format};
- $self->{format} = 'PS' if lc $self->{format} eq 'postscript';
$self->{language} = $args{language};
$self->{no_escape} = $args{no_escape};
$self->{debug} = $args{debug};
@@ -134,7 +143,21 @@ sub new {
$self->{noauto} = $args{no_auto_output};
$self->{method} = $args{method};
$self->{method} ||= $args{media};
-
+ $self->{format_args} = $args{format_options};
+ $self->{output_args} = $args{output_options};
+
+ # SC: Muxing pre-format_args LaTeX format specifications. Now with
+ # DVI support.
+ if (lc $self->{format} eq 'dvi') {
+ $self->{format} = 'LaTeX';
+ $self->{format_args}{filetype} = 'dvi';
+ } elsif (lc $self->{format} eq 'pdf') {
+ $self->{format} = 'LaTeX';
+ $self->{format_args}{filetype} = 'pdf';
+ } elsif (lc $self->{format} eq 'ps' or lc $self->{format} eq 'postscript') {
+ $self->{format} = 'LaTeX';
+ $self->{format_args}{filetype} = 'ps';
+ }
bless $self, $class;
if ($self->{format} !~ /^\p{IsAlnum}+$/) {
diff --git a/LedgerSMB/Template/PDF.pm b/LedgerSMB/Template/LaTeX.pm
index ef7ca519..77e2a2d4 100755
--- a/LedgerSMB/Template/PDF.pm
+++ b/LedgerSMB/Template/LaTeX.pm
@@ -1,7 +1,16 @@
=head1 NAME
-LedgerSMB::Template::PDF Template support module for LedgerSMB
+LedgerSMB::Template::LaTeX Template support module for LedgerSMB
+
+=head1 SYNOPSIS
+
+Muxed LaTeX rendering support. Handles PDF, Postscript, and DVI output.
+
+=head1 DETAILS
+
+The final output format is determined by the format_option of filetype. The
+valid filetype specifiers are 'pdf', 'ps', and 'dvi'.
=head1 METHODS
@@ -17,7 +26,7 @@ Currently does nothing.
=item process ($parent, $cleanvars)
-Processes the template for PDF.
+Processes the template for the appropriate output format.
=item postprocess ($parent)
@@ -36,7 +45,7 @@ including contact information of contributors, maintainers, and copyright
holders, see the CONTRIBUTORS file.
=cut
-package LedgerSMB::Template::PDF;
+package LedgerSMB::Template::LaTeX;
use Error qw(:try);
use Template::Latex;
@@ -85,8 +94,15 @@ sub process {
} else {
$source = get_template($parent->{template});
}
+ $Template::Latex::DEBUG = 1 if $parent->{debug};
+ my $format = 'ps';
+ if ($parent->{format_args}{filetype} eq 'dvi') {
+ $format = 'dvi';
+ } elsif ($parent->{format_args}{filetype} eq 'pdf') {
+ $format = 'pdf';
+ }
$template = Template::Latex->new({
- LATEX_FORMAT => 'pdf',
+ LATEX_FORMAT => $format,
INCLUDE_PATH => $parent->{include_path},
START_TAG => quotemeta('<?lsmb'),
END_TAG => quotemeta('?>'),
@@ -99,16 +115,20 @@ sub process {
$source,
{%$cleanvars, %$LedgerSMB::Template::TTI18N::ttfuncs,
'escape' => \&preprocess},
- "$parent->{outputfile}.pdf", binmode => 1)) {
+ "$parent->{outputfile}.$format", binmode => 1)) {
throw Error::Simple $template->error();
}
- $parent->{mimetype} = 'application/pdf';
+ if ($format eq 'dvi') {
+ $parent->{mimetype} = 'application/x-dvi';
+ } else {
+ $parent->{mimetype} = 'application/$format';
+ }
+ $parent->{rendered} = "$parent->{outputfile}.$format";
}
sub postprocess {
my $parent = shift;
- $parent->{rendered} = "$parent->{outputfile}.pdf";
- return "$parent->{outputfile}.pdf";
+ return $parent->{rendered};
}
1;
diff --git a/LedgerSMB/Template/ODS.pm b/LedgerSMB/Template/ODS.pm
index 414e732a..a1402042 100644
--- a/LedgerSMB/Template/ODS.pm
+++ b/LedgerSMB/Template/ODS.pm
@@ -119,6 +119,26 @@ sub _cell_handler {
$currcol++;
}
+sub _formula_handler {
+ my $cell = $ods->getCell(-1, $rowcount, $currcol);
+
+ if (@style_stack and $celltype{$style_stack[0][0]}) {
+ $ods->cellValueType($cell, $celltype{$style_stack[0][0]}[0]);
+ } elsif ($_->{att}->{type}) {
+ my $type = $_->{att}->{type};
+ if ($type =~ /^(string|blank|url)$/i) {
+ $ods->cellValueType($cell, 'string');
+ } elsif ($type =~ /^(number|formula)$/i) {
+ $ods->cellValueType($cell, 'float');
+ }
+ }
+ $ods->cellFormula($cell, "oooc:=$_->{att}->{text}");
+ if (@style_stack) {
+ $ods->cellStyle($cell, $style_stack[0][0]);
+ }
+ $currcol++;
+}
+
sub _border_set {
my ($format, $properties, $border) = @_;
my $edge = $border;
@@ -759,7 +779,7 @@ sub _ods_process {
worksheet => \&_worksheet_handler,
row => \&_row_handler,
cell => \&_cell_handler,
- formula => \&_cell_handler,
+ formula => \&_formula_handler,
format => \&_format_handler,
},
twig_handlers => {
diff --git a/LedgerSMB/Template/PS.pm b/LedgerSMB/Template/PS.pm
deleted file mode 100755
index fc732590..00000000
--- a/LedgerSMB/Template/PS.pm
+++ /dev/null
@@ -1,114 +0,0 @@
-
-=head1 NAME
-
-LedgerSMB::Template::PS Template support module for LedgerSMB
-
-=head1 METHODS
-
-=over
-
-=item get_template ($name)
-
-Returns the appropriate template filename for this format.
-
-=item preprocess ($vars)
-
-Currently does nothing.
-
-=item process ($parent, $cleanvars)
-
-Processes the template for Postscript.
-
-=item postprocess ($parent)
-
-Currently does nothing.
-
-=back
-
-=head1 Copyright (C) 2007, The LedgerSMB core team.
-
-This work contains copyrighted information from a number of sources all used
-with permission.
-
-It is released under the GNU General Public License Version 2 or, at your
-option, any later version. See COPYRIGHT file for details. For a full list
-including contact information of contributors, maintainers, and copyright
-holders, see the CONTRIBUTORS file.
-=cut
-
-package LedgerSMB::Template::PS;
-
-use Error qw(:try);
-use Template::Latex;
-use LedgerSMB::Template::TTI18N;
-
-sub get_template {
- my $name = shift;
- return "${name}.tex";
-}
-
-sub preprocess {
- my $rawvars = shift;
- my $vars;
- my $type = ref $rawvars;
-
- return $rawvars if $type =~ /^LedgerSMB::Locale/;
- if ($type eq 'ARRAY') {
- for (@{$rawvars}) {
- push @{$vars}, preprocess($_);
- }
- } elsif (!$type) {
- #XXX Fix escaping
- $rawvars =~ s/([&\$\\_<>~^#\%\{\}])/\\$1/g;
- $rawvars =~ s/"(.*)"/``$1''/gs;
- return $rawvars;
- } else {
- for ( keys %{$rawvars} ) {
- $vars->{$_} = preprocess($rawvars->{$_});
- }
- }
- return $vars;
-}
-
-sub process {
- my $parent = shift;
- my $cleanvars = shift;
- my $template;
- my $source;
- $parent->{outputfile} ||=
- "${LedgerSMB::Sysconfig::tempdir}/$parent->{template}-output-$$";
-
- if (ref $parent->{template} eq 'SCALAR') {
- $source = $parent->{template};
- } elsif (ref $parent->{template} eq 'ARRAY') {
- $source = join "\n", @{$parent->{template}};
- } else {
- $source = get_template($parent->{template});
- }
- $template = Template::Latex->new({
- LATEX_FORMAT => 'ps',
- INCLUDE_PATH => $parent->{include_path},
- START_TAG => quotemeta('<?lsmb'),
- END_TAG => quotemeta('?>'),
- DELIMITER => ';',
- DEBUG => ($parent->{debug})? 'dirs': undef,
- DEBUG_FORMAT => '',
- }) || throw Error::Simple Template::Latex->error();
-
- if (not $template->process(
- $source,
- {%$cleanvars, %$LedgerSMB::Template::TTI18N::ttfuncs,
- 'escape' => \&preprocess},
- "$parent->{outputfile}.ps", binmode => 1)) {
- throw Error::Simple $template->error();
- }
- $parent->{mimetype} = 'application/postscript';
-}
-
-sub postprocess {
- my $parent = shift;
- $parent->{rendered} = "$parent->{outputfile}.ps";
- return "$parent->{outputfile}.ps";
-}
-
-1;
diff --git a/t/01-load.t b/t/01-load.t
index 8b84cd88..6d654615 100644
--- a/t/01-load.t
+++ b/t/01-load.t
@@ -32,10 +32,10 @@ use_ok('LedgerSMB::Session');
use_ok('LedgerSMB::Sysconfig');
use_ok('LedgerSMB::Tax');
use_ok('LedgerSMB::Template');
+use_ok('LedgerSMB::Template::Elements');
use_ok('LedgerSMB::Template::CSV');
use_ok('LedgerSMB::Template::HTML');
-use_ok('LedgerSMB::Template::PDF');
-use_ok('LedgerSMB::Template::PS');
+use_ok('LedgerSMB::Template::LaTeX');
use_ok('LedgerSMB::Template::TXT');
use_ok('LedgerSMB::User');
diff --git a/t/04-template-handling.t b/t/04-template-handling.t
index e97980e2..27e63bf3 100644
--- a/t/04-template-handling.t
+++ b/t/04-template-handling.t
@@ -17,9 +17,10 @@ use LedgerSMB::Form;
use LedgerSMB::Sysconfig;
use LedgerSMB::Locale;
use LedgerSMB::Template;
+use LedgerSMB::Template::Elements;
+use LedgerSMB::Template::CSV;
use LedgerSMB::Template::HTML;
-use LedgerSMB::Template::PDF;
-use LedgerSMB::Template::PS;
+use LedgerSMB::Template::LaTeX;
use LedgerSMB::Template::TXT;
$LedgerSMB::Sysconfig::tempdir = 't/var';
@@ -153,19 +154,19 @@ is(LedgerSMB::Template::HTML::postprocess({outputfile => '04-template'}),
# Template->new
$myconfig = {'templates' => 't/data'};
-throws_ok{new LedgerSMB::Template('user' => $myconfig, 'language' => 'x/0')}
+throws_ok{new LedgerSMB::Template('user' => $myconfig, 'language' => 'x/0', 'format' => 'HTML')}
qr/Invalid language/, 'Template, new: Invalid language caught 1';
-throws_ok{new LedgerSMB::Template('user' => $myconfig, 'language' => '1\\2')}
+throws_ok{new LedgerSMB::Template('user' => $myconfig, 'language' => '1\\2', 'format' => 'HTML')}
qr/Invalid language/, 'Template, new: Invalid language caught 2';
-throws_ok{new LedgerSMB::Template('user' => $myconfig, 'language' => '1:2')}
+throws_ok{new LedgerSMB::Template('user' => $myconfig, 'language' => '1:2', 'format' => 'HTML')}
qr/Invalid language/, 'Template, new: Invalid language caught 3';
-throws_ok{new LedgerSMB::Template('user' => $myconfig, 'language' => '..')}
+throws_ok{new LedgerSMB::Template('user' => $myconfig, 'language' => '..', 'format' => 'HTML')}
qr/Invalid language/, 'Template, new: Invalid language caught 4';
-throws_ok{new LedgerSMB::Template('user' => $myconfig, 'language' => '.svn')}
+throws_ok{new LedgerSMB::Template('user' => $myconfig, 'language' => '.svn', 'format' => 'HTML')}
qr/Invalid language/,
'Template, new: Invalid language caught 5';
$template = undef;
-$template = new LedgerSMB::Template('user' => $myconfig, 'language' => 'de');
+$template = new LedgerSMB::Template('user' => $myconfig, 'language' => 'de', 'format' => 'HTML');
ok(defined $template, 'Template, new: Object creation with valid language');
isa_ok($template, 'LedgerSMB::Template',
'Template, new: Object creation with valid language');
@@ -173,7 +174,7 @@ is($template->{include_path}, 't/data/de;t/data',
'Template, new: Object creation with valid language has good include_path');
$template = undef;
$template = new LedgerSMB::Template('user' => $myconfig, 'language' => 'de',
- 'path' => 't/data', 'output_file' => 'test');
+ 'path' => 't/data', 'output_file' => 'test', 'format' => 'HTML');
ok(defined $template,
'Template, new: Object creation with valid language and path');
isa_ok($template, 'LedgerSMB::Template',
@@ -279,6 +280,21 @@ is($template->{output}, "I am a template.\nLook at me foo&amp;bar.",
$template = undef;
$template = new LedgerSMB::Template('user' => $myconfig, 'format' => 'HTML',
+ 'template' => \'Look at me <?lsmb login ?>.', 'no_auto_output' => 1);
+ok(defined $template,
+ 'Template, new (HTML): Object creation with string template');
+isa_ok($template, 'LedgerSMB::Template',
+ 'Template, new (HTML): Object creation with string template');
+is($template->{include_path}, 't/data',
+ 'Template, new (HTML): Object creation with string template');
+is($template->render({'login' => 'foo&bar'}),
+ undef,
+ 'Template, render (HTML): Simple HTML string template, no file');
+is($template->{output}, "Look at me foo&amp;bar.",
+ 'Template, render (HTML): Simple HTML string template, correct output');
+
+$template = undef;
+$template = new LedgerSMB::Template('user' => $myconfig, 'format' => 'HTML',
'template' => '04-gettext', 'output_file' => '04-gettext',
'no_auto_output' => 1);
ok(defined $template,
@@ -327,3 +343,12 @@ SKIP: {
ok(!-e "t/var/04-gettext-output-$$.pdf",
'Template, render (PDF): testfile removed');
}
+
+###################################
+## LedgerSMB::Template::Elements ##
+###################################
+
+$template = undef;
+$form = undef;
+
+$form = new Form;