summaryrefslogtreecommitdiff
path: root/lib/Tree/Transform/XSLTish.pm
diff options
context:
space:
mode:
Diffstat (limited to 'lib/Tree/Transform/XSLTish.pm')
-rw-r--r--lib/Tree/Transform/XSLTish.pm77
1 files changed, 77 insertions, 0 deletions
diff --git a/lib/Tree/Transform/XSLTish.pm b/lib/Tree/Transform/XSLTish.pm
new file mode 100644
index 0000000..390616f
--- /dev/null
+++ b/lib/Tree/Transform/XSLTish.pm
@@ -0,0 +1,77 @@
+package Tree::Transform::XSLTish;
+use strict;
+use warnings;
+use Sub::Exporter;
+use Params::Validate ':all';
+use Tree::Transform::XSLTish::Utils;
+use Tree::Transform::XSLTish::Transformer;
+use Carp::Clan qw(^Tree::Transform::XSLTish);
+
+our $VERSION='0.1';
+
+Sub::Exporter::setup_exporter({
+ exports => [qw(tree_rule default_rules new_transformer)],
+ groups => {
+ default => [ 'tree_rule',
+ 'default_rules',
+ 'new_transformer' => {-as => 'new'} ],
+ }
+});
+
+sub default_rules {
+ my $store=Tree::Transform::XSLTish::Utils::_rules_store(scalar caller);
+
+ push @{$store->{by_match}},
+ {match=> '/',priority=>0,action=>sub { $_[0]->apply_rules } },
+ {match=> '*',priority=>0,action=>sub { $_[0]->apply_rules } },
+ ;
+ return;
+}
+
+sub tree_rule {
+ my (%args)=validate(@_, {
+ match => { type => SCALAR, optional => 1 },
+ action => { type => CODEREF },
+ name => { type => SCALAR, optional => 1},
+ priority => { type => SCALAR, default => 1 },
+ });
+
+ # TODO at least one of 'name' and 'match' must be specified
+ # TODO default priority mased on match
+
+ my $store=Tree::Transform::XSLTish::Utils::_rules_store(scalar caller);
+
+ if ($args{match}) {
+ push @{$store->{by_match}},\%args;
+ }
+ if ($args{name}) {
+ if (exists $store->{by_name}{$args{name}}) {
+ carp "Duplicate rule named $args{name}, ignoring";
+ return;
+ }
+ $store->{by_name}{$args{name}}=\%args;
+ }
+
+ return;
+}
+
+sub _transformer_class { 'Tree::Transform::XSLTish::Transformer' };
+
+sub new_transformer {
+ my ($rules_package)=@_;
+
+ return _transformer_class->new(rules_package=>$rules_package);
+}
+
+1;
+__END__
+
+=head1 NAME
+
+Tree::Transform::XSLTish - transform tree data, like XSLT but in Perl
+
+=head1 AUTHOR
+
+Gianni Ceccarelli <dakkar@thenautilus.net>
+
+=cut