From 02df3ba5ebab77e9ecd5c7eb3fd96f6b1009cfe7 Mon Sep 17 00:00:00 2001 From: Yanick Champoux Date: Mon, 20 Dec 2010 17:53:17 -0500 Subject: [PATCH] small thingies --- Changes | 2 + MANIFEST | 11 +++++ MANIFEST.SKIP | 1 + bin/xss | 44 ++++++++++++++++++++ dist.ini | 5 +-- lib/XML/XSS/Stylesheet/HTML2TD.pm | 67 +++++++++++++++++++++++++++++++ 6 files changed, 127 insertions(+), 3 deletions(-) create mode 100755 bin/xss create mode 100644 lib/XML/XSS/Stylesheet/HTML2TD.pm diff --git a/Changes b/Changes index 58f0d65..20bb5fa 100644 --- a/Changes +++ b/Changes @@ -1,6 +1,8 @@ Revision history for XML::XSS {{$NEXT}} + * add $master to stylesheet classes. + * add 'style' keyword. 0.1.3 2010-07-29 20:43:30 America/Montreal * add a first pass of template/overloading documentation diff --git a/MANIFEST b/MANIFEST index febfeeb..c3c32b1 100644 --- a/MANIFEST +++ b/MANIFEST @@ -5,6 +5,7 @@ MANIFEST META.json META.yml SIGNATURE +bin/xss lib/XML/XSS.pm lib/XML/XSS/Comment.pm lib/XML/XSS/Document.pm @@ -13,19 +14,29 @@ lib/XML/XSS/ProcessingInstruction.pm lib/XML/XSS/Role/Renderer.pm lib/XML/XSS/Role/StyleAttribute.pm lib/XML/XSS/StyleAttribute.pm +lib/XML/XSS/Stylesheet/HTML2TD.pm lib/XML/XSS/Template.pm lib/XML/XSS/Text.pm t/000-report-versions.t t/basic.t t/document.t t/element.t +t/lib/A.pm +t/lib/B.pm +t/lib/C.pm t/lib/My/Test/Class.pm t/lib/My/Test/Class/Load.pm +t/lib/MyStylesheet.pm t/lib/XML/XSS/CommentTest.pm t/lib/XML/XSS/DocumentTest.pm t/lib/XML/XSS/ElementTest.pm t/lib/XML/XSS/OverloadTest.pm t/lib/XML/XSS/TemplateTest.pm t/lib/XML/XSSTest.pm +t/master.t +t/moose-dsl.t +t/moose_style.t +t/set.t +t/style_attributes.t t/test-class.t t/text.t diff --git a/MANIFEST.SKIP b/MANIFEST.SKIP index f4ea81d..42a1700 100644 --- a/MANIFEST.SKIP +++ b/MANIFEST.SKIP @@ -1,3 +1,4 @@ +TODO project.vim dist.ini ^XML-XSS.* diff --git a/bin/xss b/bin/xss new file mode 100755 index 0000000..ff31932 --- /dev/null +++ b/bin/xss @@ -0,0 +1,44 @@ +#!/usr/bin/env perl + +package XML::XSS::xss; +#ABSTRACT: command-line XML::XSS processor + +use Getopt::Long; + +GetOptions( + 'script=s' => \my $script, + 'm=s' => \my $module, + 'stylesheet=s' => \my $stylesheet, +); + +my $document = join '', <>; + +my $xss; + +if( $stylesheet ) { + my $class = "XML::XSS::Stylesheet::$stylesheet"; + eval "use $class"; + die $@ if $@; + + $xss = $class->new; +} +elsif ( $script ) { + $xss = do $script; +} +elsif( $module ) { + eval "use $module"; + $xss = $module->new; +} + +print $xss->render( $document ); + +__END__ + +=pod + +=head1 SYNOPSIS + + xss --stylesheet document.xml + + +=cut diff --git a/dist.ini b/dist.ini index fe692f0..6d1527a 100644 --- a/dist.ini +++ b/dist.ini @@ -1,5 +1,5 @@ name = XML-XSS -version = 0.1.3 +version = 0.2.0 author = Yanick Champoux license = Perl_5 copyright_holder = Yanick Champoux @@ -17,8 +17,7 @@ remote=github [PodWeaver] -;[ExecDir] -;dir=scripts +[ExecDir] [License] diff --git a/lib/XML/XSS/Stylesheet/HTML2TD.pm b/lib/XML/XSS/Stylesheet/HTML2TD.pm new file mode 100644 index 0000000..fa34d2f --- /dev/null +++ b/lib/XML/XSS/Stylesheet/HTML2TD.pm @@ -0,0 +1,67 @@ +package XML::XSS::Stylesheet::HTML2TD; + +use Moose; +use XML::XSS; +use Perl::Tidy; + +extends 'XML::XSS'; + +style '*' => ( + pre => \&pre_element, + post => '};', +); + +style '#text' => ( + process => sub { $_[1]->data =~ /\S/ }, + pre => "outs '", + post => "';", + filter => sub { s/'/\\'/g; s/^\s+|\s+$//gm; $_ }, +); + +style '#document' => ( + content => sub { + my ( $self, $node, $args ) = @_; + my $raw = $self->stylesheet->render( $node->childNodes ); + + my $output; + my $err; + eval { + Perl::Tidy::perltidy( + source => \$raw, + destination => \$output, + errorfile => \$err, + ) + }; + + # send the raw output if Tidy failed + return $err ? $raw : $output; + }, +); + +sub pre_element { + my ( $self, $node, $args ) = @_; + + my $name = $node->nodeName; + + return "$name {" . pre_attrs( $node ); +} + +sub pre_attrs { + my $node = shift; + + my @attr = $node->attributes or return ''; + + my $output = 'attr { '; + + for ( @attr ) { + my $value = $_->value; + $value =~ s/'/'/g; + $output .= $_->nodeName . ' => ' . "'$value'" . ', '; + } + + $output .= '};'; + + return $output; +} + +1;