Skip to content

Commit

Permalink
small thingies
Browse files Browse the repository at this point in the history
  • Loading branch information
yanick committed Dec 20, 2010
1 parent d4b1453 commit 02df3ba
Show file tree
Hide file tree
Showing 6 changed files with 127 additions and 3 deletions.
2 changes: 2 additions & 0 deletions 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
Expand Down
11 changes: 11 additions & 0 deletions MANIFEST
Expand Up @@ -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
Expand All @@ -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
1 change: 1 addition & 0 deletions MANIFEST.SKIP
@@ -1,3 +1,4 @@
TODO
project.vim
dist.ini
^XML-XSS.*
Expand Down
44 changes: 44 additions & 0 deletions 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 <stylesheet> document.xml
=cut
5 changes: 2 additions & 3 deletions dist.ini
@@ -1,5 +1,5 @@
name = XML-XSS
version = 0.1.3
version = 0.2.0
author = Yanick Champoux <yanick@cpan.org>
license = Perl_5
copyright_holder = Yanick Champoux
Expand All @@ -17,8 +17,7 @@ remote=github

[PodWeaver]

;[ExecDir]
;dir=scripts
[ExecDir]

[License]

Expand Down
67 changes: 67 additions & 0 deletions 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/'/&apos;/g;
$output .= $_->nodeName . ' => ' . "'$value'" . ', ';
}

$output .= '};';

return $output;
}

1;

0 comments on commit 02df3ba

Please sign in to comment.