Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added basic support for XML output via XML::LibXML.
- Loading branch information
Showing
3 changed files
with
94 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,75 @@ | ||
#TODO | ||
package Parse::Zezenen::Filter::LibXML; | ||
use 5.006; | ||
use strict; | ||
use warnings; | ||
use base "Parse::Zezenen::Filter::Base"; | ||
use XML::LibXML; | ||
|
||
=head1 NAME | ||
Parse::Zezenen::Filter::LibXML | ||
=cut | ||
|
||
sub filter_string | ||
{ | ||
my ($self, $target, $args) = @_; | ||
return XML::LibXML::Text->new($target); | ||
} | ||
sub filter_array | ||
{ | ||
my ($self, $target, $args) = @_; | ||
my @interpreted = map {$self->filter($_,$args)} @$target; | ||
} | ||
sub filter_element | ||
{ | ||
my ($self, $target, $args) = @_; | ||
my $e; | ||
$e = XML::LibXML::Element->new($target->{'#name'}); | ||
foreach my $key (keys %$target) | ||
{ | ||
if ($key eq '~') | ||
{ | ||
foreach my $nChild (@{$target->{$key}}) | ||
{ | ||
$nChild = $self->filter($nChild,$args); | ||
# TODO: Flatten lists where directives exist. | ||
$e->appendChild($nChild); | ||
} | ||
} | ||
elsif ($key !~ /^#/) | ||
{ | ||
$e->setAttribute($key, $target->{$key}); | ||
} | ||
} | ||
return $e; | ||
} | ||
sub filter_directive | ||
{ | ||
my ($self, $target, $args) = @_; | ||
return [map {$self->filter($_, $args)} @{$self->{'~'}}]; | ||
} | ||
sub filter #Takes a parse tree and processes it | ||
{ | ||
my ($self, $target, $args) = @_; | ||
return undef unless defined $target; | ||
if (ref ($target) eq ref ('')) | ||
{ | ||
return $self->filter_string($target, $args); | ||
} | ||
elsif (ref ($target) eq ref ([])) | ||
{ | ||
return $self->filter_array($target, $args); | ||
} | ||
elsif (ref ($target) eq ref ({})) | ||
{ | ||
if($target->{'#directive'}) | ||
{ | ||
return $self->filter_directive($target, $args); | ||
} | ||
return $self->filter_element($target, $args); | ||
} | ||
return undef; | ||
} | ||
1; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
#!perl -T | ||
use strict; | ||
use warnings; | ||
use Test::More; | ||
use Parse::Zezenen::RecDescent; | ||
use Parse::Zezenen::Filter::LibXML; | ||
use Data::Dumper; | ||
$Data::Dumper::Sortkeys = 1; | ||
my $parser = Parse::Zezenen::RecDescent->new; | ||
my $filter = Parse::Zezenen::Filter::LibXML->new(); | ||
is($filter->filter($parser->parse('block','div{test}'))->toString,'<div>test</div>', 'Simple block is ok'); | ||
is($filter->filter($parser->parse('block','div{ test }'))->toString,'<div> test </div>', 'Spaces preserved ok'); | ||
is($filter->filter($parser->parse('block','div{ b{test} }'))->toString,'<div> <b>test</b> </div>', 'Nesting works ok'); | ||
is($filter->filter($parser->parse('block','div{ b{test} more text }'))->toString,'<div> <b>test</b> more text </div>', 'Mixed content'); | ||
is($filter->filter($parser->parse('block','div[style="float:left;"]{test}'))->toString,'<div style="float:left;">test</div>', 'Attributes work'); | ||
|
||
|
||
|
||
done_testing; |