From: Rob Kinyon Date: Wed, 26 Jan 2005 16:59:52 +0000 (+0000) Subject: Added CELL type attr; Removed PM_FILTER X-Git-Tag: v0.20~1 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=8c63e224aff1269ad7266a470885b5bbbbb9473e;p=p5sagit%2FExcel-Template.git Added CELL type attr; Removed PM_FILTER --- diff --git a/Changes b/Changes index 49b75be..acd1678 100644 --- a/Changes +++ b/Changes @@ -1,5 +1,9 @@ Revision history for Perl distribution Excel::Template +0.20 Wed Jan 26 12:00:00 2004 + - Removed PM_FILTER by adding an optional USE_UNICODE runtime parameter. + - Added a "type" attribute to CELL to allow printing by the write_*() family + 0.19 Wed Dec 08 12:00:00 2004 - Fixed META.yml - Added more values to the MANIFEST.SKIP diff --git a/MANIFEST b/MANIFEST index b6823d4..3b1983c 100644 --- a/MANIFEST +++ b/MANIFEST @@ -59,6 +59,8 @@ t/013_range.t t/013.xml t/014_heightwidth.t t/014.xml +t/015_cell_type.t +t/015.xml t/mock.pm t/Spreadsheet/WriteExcel.pm t/Spreadsheet/WriteExcel/Worksheet.pm diff --git a/META.yml b/META.yml index 42e2f61..0877242 100644 --- a/META.yml +++ b/META.yml @@ -1,7 +1,7 @@ # http://module-build.sourceforge.net/META-spec.html #XXXXXXX This is a prototype!!! It will change in the future!!! XXXXX# name: Excel-Template -version: 0.19 +version: 0.20 version_from: lib/Excel/Template.pm installdirs: site requires: diff --git a/Makefile.PL b/Makefile.PL index 225719d..fa70b36 100644 --- a/Makefile.PL +++ b/Makefile.PL @@ -14,29 +14,20 @@ my $prereqs = { # The assumption is Perl 5.8.0 and greater doesn't need Unicode::String. -my $use_unicode = 0; if ($] < 5.008) { - print "Do you want Unicode support (y/N)? "; - my $answer = ; - - if ($answer =~ /^[Yy]/) + eval { require Unicode::String; }; + if ($@) { - $prereqs->{'Unicode::String'} = '0.01'; - $use_unicode = 1; + print "Note: If you want to work with Unicode, you will need to install"; + print "the optional module Unicode::String and set USE_UNICODE to true."; } } -my $pm_filter = $use_unicode - ? q{perl -pi.old -e "s!UNI_YES ! !g;s!UNI_NO !\\#!g"} - : q{perl -pi.old -e "s!UNI_NO ! !g;s!UNI_YES !\\#!g"} -; - WriteMakefile( NAME => 'Excel::Template', VERSION_FROM => 'lib/Excel/Template.pm', # finds $VERSION AUTHOR => 'Rob Kinyon (rob.kinyon@gmail.com)', ABSTRACT => 'Excel::Template', PREREQ_PM => $prereqs, - PM_FILTER => $pm_filter, ); diff --git a/lib/Excel/Template.pm b/lib/Excel/Template.pm index 9c358a2..8eff016 100644 --- a/lib/Excel/Template.pm +++ b/lib/Excel/Template.pm @@ -6,7 +6,7 @@ BEGIN { use Excel::Template::Base; use vars qw ($VERSION @ISA); - $VERSION = '0.19'; + $VERSION = '0.20'; @ISA = qw( Excel::Template::Base ); } @@ -49,6 +49,9 @@ sub new join("\n\t", @renderer_classes) . "\n"; + $self->{USE_UNICODE} = ~~0 + if $] >= 5.008; + return $self; } @@ -66,7 +69,7 @@ sub param $params{uc $_} = delete $params{$_} for keys %params; @{$self->{PARAM_MAP}}{keys %params} = @params{keys %params}; - return !!1; + return ~~1; } sub write_file @@ -81,7 +84,7 @@ sub write_file $xls->close; - return !!1; + return ~~1; } sub output @@ -183,6 +186,7 @@ sub _prepare_output XLS => $xls, PARAM_MAP => [ $self->{PARAM_MAP} ], + UNICODE => $self->{UNICODE}, ); $_->render($context) for @{$self->{WORKBOOKS}}; @@ -268,7 +272,14 @@ described below.) new() accepts an optional BIG_FILE parameter. This will attempt to change the renderer from L to L. You -must have L installed on your system. +must already have L installed on your system. + +new() also accepts an optional USE_UNICODE parameter. This will use +L to represent strings instead of Perl's internal string +handling. You must already have L installed on your system. + +The USE_UNICODE parameter will be ignored if you are using Perl 5.8 or higher as +Perl's internal string handling is unicode-aware. NOTE: L and mod_perl clash for some reason. This is outside of my control. diff --git a/lib/Excel/Template/Context.pm b/lib/Excel/Template/Context.pm index 7a66225..c8fe40a 100644 --- a/lib/Excel/Template/Context.pm +++ b/lib/Excel/Template/Context.pm @@ -15,7 +15,7 @@ use Excel::Template::Format; # represent an XML object. Rather, every container will use this object to # maintain the context for its children. -my %isAbsolute = map { $_ => !!1 } qw( +my %isAbsolute = map { $_ => ~~1 } qw( ROW COL ); @@ -37,6 +37,8 @@ sub new return $self; } +sub use_unicode { $_[0]->{UNICODE} && 1 } + sub _find_param_in_map { my $self = shift; @@ -50,7 +52,7 @@ sub _find_param_in_map next unless exists $map->{$param}; $depth--, next if $depth; - $found = !!1; + $found = ~~1; $val = $map->{$param}; last; } @@ -144,7 +146,7 @@ sub enter_scope $self->{$key} = $self->resolve($obj, $key); } - return !!1; + return ~~1; } sub exit_scope @@ -160,7 +162,7 @@ sub exit_scope pop @{$self->{STACK}}; - return !!1; + return ~~1; } sub get @@ -255,7 +257,7 @@ sub add_reference push @{$self->{REFERENCES}{$ref}}, [ $row, $col ]; - return !!1; + return ~~1; } sub get_all_references diff --git a/lib/Excel/Template/Element/Cell.pm b/lib/Excel/Template/Element/Cell.pm index f14428a..1f5e8d4 100755 --- a/lib/Excel/Template/Element/Cell.pm +++ b/lib/Excel/Template/Element/Cell.pm @@ -37,18 +37,46 @@ sub get_text } else { -UNI_YES $txt = Unicode::String::utf8(''); -UNI_NO $txt = ''; + $txt = $context->use_unicode + ? Unicode::String::utf8('') + : ''; } return $txt; } +my %legal_types = ( + 'blank' => 'write_blank', + 'formula' => 'write_formula', + 'number' => 'write_number', + 'string' => 'write_string', + 'url' => 'write_url', +); + sub render { my $self = shift; my ($context, $method) = @_; + unless ( $method ) + { + my $type = $context->get( $self, 'TYPE' ); + if ( defined $type ) + { + my $type = lc $type; + + if ( exists $legal_types{ $type } ) + { + $method = $legal_types{ $type }; + } + else + { + warn "'$type' is not a legal cell type.\n" + if $^W; + } + } + } + $method ||= 'write'; my ($row, $col) = map { $context->get($self, $_) } qw(ROW COL); @@ -131,9 +159,32 @@ formula. See BACKREF and RANGE. Sets the width of the column the cell is in. The last setting for a given column will win out. -=back 4 +=item * TYPE + +This allows you to specify what write_*() method will be used. The default is to +call write() and let S::WE make the right call. However, you may wish to +override it. Excel::Template will not do any form of validation on what you +provide. You are assumed to know what you're doing. + +The legal types are: + +=over 4 + +=item * blank + +=item * formula + +=item * number + +=item * string + +=item * url + +=back + +q.v. L for more info. -There will be more parameters added, as features are added. +=back =head1 CHILDREN diff --git a/lib/Excel/Template/TextObject.pm b/lib/Excel/Template/TextObject.pm index 8fa6b47..6c7a99e 100755 --- a/lib/Excel/Template/TextObject.pm +++ b/lib/Excel/Template/TextObject.pm @@ -7,8 +7,6 @@ BEGIN { @ISA = qw(Excel::Template::Base); use Excel::Template::Base; - -UNI_YES use Unicode::String; } # This is a helper object. It is not instantiated by the user, @@ -31,8 +29,18 @@ sub resolve my $self = shift; my ($context) = @_; -UNI_YES my $t = Unicode::String::utf8(''); -UNI_NO my $t = ''; + my $use_unicode = $context->use_unicode; + + my $t; + if ($use_unicode) + { + require Unicode::String; + $t = Unicode::String::utf8(''); + } + else + { + $t = ''; + } for my $tok (@{$self->{STACK}}) { @@ -40,8 +48,9 @@ UNI_NO my $t = ''; $val = $val->resolve($context) if Excel::Template::Factory::is_embedded( $val ); -UNI_YES $t .= Unicode::String::utf8("$val"); -UNI_NO $t .= $val; + $t .= $use_unicode + ? Unicode::String::utf8("$val") + : $val; } return $t; diff --git a/t/015.xml b/t/015.xml new file mode 100644 index 0000000..8934a6e --- /dev/null +++ b/t/015.xml @@ -0,0 +1,9 @@ + + + String + Number + Blank + URL + Formula + + diff --git a/t/015_cell_type.t b/t/015_cell_type.t new file mode 100644 index 0000000..1355d89 --- /dev/null +++ b/t/015_cell_type.t @@ -0,0 +1,31 @@ +use strict; + +use Test::More tests => 4; + +use lib 't'; +use mock; +mock->reset; + +my $CLASS = 'Excel::Template'; +use_ok( $CLASS ); + +my $object = $CLASS->new( + filename => 't/015.xml', +); +isa_ok( $object, $CLASS ); + +ok( $object->write_file( 'filename' ), 'Something returned' ); + +my @calls = mock->get_calls; +is( join( $/, @calls, '' ), <<__END_EXPECTED__, 'Calls match up' ); +Spreadsheet::WriteExcel::new( 'filename' ) +Spreadsheet::WriteExcel::add_format( '' ) +Spreadsheet::WriteExcel::add_worksheet( 'cell_type' ) +Spreadsheet::WriteExcel::Worksheet::new( '' ) +Spreadsheet::WriteExcel::Worksheet::write_string( '0', '0', 'String', '1' ) +Spreadsheet::WriteExcel::Worksheet::write_number( '0', '1', 'Number', '1' ) +Spreadsheet::WriteExcel::Worksheet::write_blank( '0', '2', 'Blank', '1' ) +Spreadsheet::WriteExcel::Worksheet::write_url( '0', '3', 'URL', '1' ) +Spreadsheet::WriteExcel::Worksheet::write_formula( '0', '4', 'Formula', '1' ) +Spreadsheet::WriteExcel::close( '' ) +__END_EXPECTED__ diff --git a/t/Spreadsheet/WriteExcel/Worksheet.pm b/t/Spreadsheet/WriteExcel/Worksheet.pm index a269214..aa02220 100644 --- a/t/Spreadsheet/WriteExcel/Worksheet.pm +++ b/t/Spreadsheet/WriteExcel/Worksheet.pm @@ -16,6 +16,42 @@ sub new { return $self; } +sub write_string { + my $self = shift; + + { + local $" = "', '"; + push @mock::calls, __PACKAGE__ . "::write_string( '@_' )"; + } +} + +sub write_number { + my $self = shift; + + { + local $" = "', '"; + push @mock::calls, __PACKAGE__ . "::write_number( '@_' )"; + } +} + +sub write_blank { + my $self = shift; + + { + local $" = "', '"; + push @mock::calls, __PACKAGE__ . "::write_blank( '@_' )"; + } +} + +sub write_url { + my $self = shift; + + { + local $" = "', '"; + push @mock::calls, __PACKAGE__ . "::write_url( '@_' )"; + } +} + sub write_formula { my $self = shift;