Permalink
Browse files

Let HTML templates handled by File::Slurp and the encoding determined…

… by the specification.
  • Loading branch information...
1 parent 57829cb commit cdecb8ab316fa8314aec029af7425bc34608813b @racke committed Jun 21, 2011
View
@@ -13,6 +13,7 @@ WriteMakefile(
PL_FILES => {},
PREREQ_PM => {
'Test::More' => 0,
+ 'File::Slurp' => 0,
'XML::Twig' => 0,
'HTML::TreeBuilder' => 0,
},
View
@@ -3,6 +3,8 @@ package Template::Flute::HTML;
use strict;
use warnings;
+use Encode;
+use File::Slurp ();
use XML::Twig;
use Template::Flute::Increment;
@@ -254,20 +256,26 @@ sub parse_file {
sub _parse_template {
my ($self, $template, $spec_object) = @_;
- my ($twig, $xml, $object, $list);
+ my ($twig, $xml, $object, $list, $html_content, $encoding);
$object = {specs => {}, lists => {}, forms => {}, params => {}};
$twig = new XML::Twig (twig_handlers => {_all_ => sub {$self->_parse_handler($_[1], $spec_object)}});
if (ref($template) eq 'SCALAR') {
$self->{file} = '';
- $xml = $twig->safe_parse_html($$template);
+ $html_content = $$template;
}
else {
$self->{file} = $template;
- $xml = $twig->safe_parsefile_html($template);
+ $encoding = $spec_object->encoding();
+ $html_content = File::Slurp::read_file($template, binmode => ":encoding($encoding)");
+ unless ($encoding eq 'utf8') {
+ $html_content = encode('utf8', $html_content);
+ }
}
+
+ $xml = $twig->safe_parse_html($html_content);
unless ($xml) {
die "Invalid HTML template: $template: $@\n";
@@ -37,7 +37,7 @@ sub new {
my (%params);
$class = shift;
- %params = @_;
+ %params = (encoding => 'utf8', @_);
$self = \%params;
@@ -52,6 +52,39 @@ sub new {
=head1 METHODS
+=head2 name NAME
+
+Set or get the name of the specification.
+
+=cut
+
+sub name {
+ my $self = shift;
+
+ if (scalar @_ > 0) {
+ $self->{name} = shift;
+ }
+
+ return $self->{name};
+}
+
+=head2 encoding ENCODING
+
+Set or get the encoding of the HTML template
+which is parsed according to this specification.
+
+=cut
+
+sub encoding {
+ my $self = shift;
+
+ if (scalar @_ > 0) {
+ $self->{encoding} = shift;
+ }
+
+ return $self->{encoding};
+}
+
=head2 container_add CONTAINER
Add container specified by hash reference CONTAINER.
@@ -97,10 +97,14 @@ parse_file methods.
sub create_specification {
my ($self, $config) = @_;
- my ($spec, $scoped, $key, $value, %list);
+ my ($spec, $scoped, $key, $value, %list, $encoding);
# specification object
$spec = new Template::Flute::Specification;
+
+ if ($encoding = $config->{specification}->{encoding}) {
+ $spec->encoding($encoding);
+ }
# lists
while (($key, $value) = each %{$config->{list}}) {
@@ -125,9 +125,15 @@ sub _initialize {
sub _spec_handler {
my ($self, $elt) = @_;
- my ($name);
+ my ($value);
- $name = $elt->att('name');
+ if ($value = $elt->att('name')) {
+ $self->{spec}->name($value);
+ }
+
+ if ($value = $elt->att('encoding')) {
+ $self->{spec}->encoding($value);
+ }
}
sub _container_handler {
View
@@ -12,11 +12,14 @@ if ($@) {
plan skip_all => "No Config::Scoped module.";
}
-plan tests => 2;
+plan tests => 3;
require Template::Flute::Specification::Scoped;
my $conf = <<EOF;
+specification {
+ encoding = iso8859-1
+}
list test {
class = cpan
}
@@ -35,5 +38,8 @@ eval {
diag("Failure parsing specification: $@") if $@;
isa_ok($ret, 'Template::Flute::Specification');
+# check for proper encoding
+ok($ret->encoding() eq 'iso8859-1', 'get encoding from specification');
+
# check for list
ok(exists($ret->{lists}->{test}->{input}));
View
@@ -0,0 +1,43 @@
+#! perl -T
+#
+# Test for HTML files with UTF-8 content
+
+use strict;
+use warnings;
+
+use Test::More tests => 3;
+
+use File::Basename;
+use Template::Flute;
+use Template::Flute::Specification::XML;
+use Template::Flute::HTML;
+
+# parse XML specification
+my ($spec, $ret);
+
+$spec = new Template::Flute::Specification::XML;
+
+$ret = $spec->parse_file(dirname(__FILE__) . '/files/utf8.xml');
+
+isa_ok($ret, 'Template::Flute::Specification');
+
+# check whether encoding was read correctly
+ok($ret->encoding() eq 'utf8');
+
+# parse HTML template
+my ($html_object);
+
+$html_object = new Template::Flute::HTML;
+
+eval {
+ $html_object->parse_file(dirname(__FILE__) . '/files/utf8.html', $ret);
+};
+
+if ($@) {
+ fail("Crashed while parsing HTML: $@");
+}
+else {
+ pass("Parsing HTML was successful.");
+}
+
+
View
@@ -0,0 +1,43 @@
+#! perl
+#
+# Test for HTML files with ISO-8859-1 content
+
+use strict;
+use warnings;
+
+use Test::More tests => 3;
+
+use File::Basename;
+use Template::Flute;
+use Template::Flute::Specification::XML;
+use Template::Flute::HTML;
+
+# parse XML specification
+my ($spec, $ret);
+
+$spec = new Template::Flute::Specification::XML;
+
+$ret = $spec->parse_file(dirname(__FILE__) . '/files/iso.xml');
+
+isa_ok($ret, 'Template::Flute::Specification');
+
+# check whether encoding was read correctly
+ok($ret->encoding() eq 'iso8859-1');
+
+# parse HTML template
+my ($html_object);
+
+$html_object = new Template::Flute::HTML;
+
+eval {
+ $html_object->parse_file(dirname(__FILE__) . '/files/iso.html', $ret);
+};
+
+if ($@) {
+ fail("Crashed while parsing HTML: $@");
+}
+else {
+ pass("Parsing HTML was successful.");
+}
+
+
View
@@ -0,0 +1 @@
+<span class="dish">Erdäpfelsalat</span>
View
@@ -0,0 +1,2 @@
+<specification encoding="iso8859-1">
+</specification>
View
@@ -0,0 +1 @@
+<span class="copyright">Copyright © 2011. All rights reserved.</span>
View
@@ -0,0 +1,2 @@
+<specification>
+</specification>

0 comments on commit cdecb8a

Please sign in to comment.