#============================================================= -*-Perl-*- # # Template::Plugin::XML::Style # # DESCRIPTION # Template Toolkit plugin which performs some basic munging of XML # to perform simple stylesheet like transformations. # # AUTHOR # Andy Wardley # # COPYRIGHT # Copyright (C) 2001-2006 Andy Wardley. All Rights Reserved. # # This module is free software; you can redistribute it and/or # modify it under the same terms as Perl itself. # #============================================================================ package Template::Plugin::XML::Style; use strict; use warnings; use base 'Template::Plugin::Filter'; our $VERSION = 2.36; our $DYNAMIC = 1; our $FILTER_NAME = 'xmlstyle'; #------------------------------------------------------------------------ # new($context, \%config) #------------------------------------------------------------------------ sub init { my $self = shift; my $name = $self->{ _ARGS }->[0] || $FILTER_NAME; $self->install_filter($name); return $self; } sub filter { my ($self, $text, $args, $config) = @_; # munge start tags $text =~ s/ < ([\w\.\:]+) ( \s+ [^>]+ )? > / $self->start_tag($1, $2, $config) /gsex; # munge end tags $text =~ s/ < \/ ([\w\.\:]+) > / $self->end_tag($1, $config) /gsex; return $text; } sub start_tag { my ($self, $elem, $textattr, $config) = @_; $textattr ||= ''; my ($pre, $post); # look for an element match in the stylesheet my $match = $config->{ $elem } || $self->{ _CONFIG }->{ $elem } || return "<$elem$textattr>"; # merge element attributes into copy of stylesheet attributes my $attr = { %{ $match->{ attributes } || { } } }; while ($textattr =~ / \s* ([\w\.\:]+) = " ([^"]+) " /gsx ) { $attr->{ $1 } = $2; } $textattr = join(' ', map { "$_=\"$attr->{$_}\"" } keys %$attr); $textattr = " $textattr" if $textattr; $elem = $match->{ element } || $elem; $pre = $match->{ pre_start } || ''; $post = $match->{ post_start } || ''; return "$pre<$elem$textattr>$post"; } sub end_tag { my ($self, $elem, $config) = @_; my ($pre, $post); # look for an element match in the stylesheet my $match = $config->{ $elem } || $self->{ _CONFIG }->{ $elem } || return ""; $elem = $match->{ element } || $elem; $pre = $match->{ pre_end } || ''; $post = $match->{ post_end } || ''; return "$pre$post"; } 1; __END__ =head1 NAME Template::Plugin::XML::Style - Simple XML stylesheet transfomations =head1 SYNOPSIS [% USE xmlstyle table = { attributes = { border = 0 cellpadding = 4 cellspacing = 1 } } %] [% FILTER xmlstyle %]
Foo Bar Baz
[% END %] =head1 DESCRIPTION This plugin defines a filter for performing simple stylesheet based transformations of XML text. Named parameters are used to define those XML elements which require transformation. These may be specified with the USE directive when the plugin is loaded and/or with the FILTER directive when the plugin is used. This example shows how the default attributes C and C can be added to EtableE elements. [% USE xmlstyle table = { attributes = { border = 0 cellpadding = 4 } } %] [% FILTER xmlstyle %] ...
[% END %] This produces the output: ...
Parameters specified within the USE directive are applied automatically each time the C FILTER is used. Additional parameters passed to the FILTER directive apply for only that block. [% USE xmlstyle table = { attributes = { border = 0 cellpadding = 4 } } %] [% FILTER xmlstyle tr = { attributes = { valign="top" } } %] ...
[% END %] Of course, you may prefer to define your stylesheet structures once and simply reference them by name. Passing a hash reference of named parameters is just the same as specifying the named parameters as far as the Template Toolkit is concerned. [% style_one = { table = { ... } tr = { ... } } style_two = { table = { ... } td = { ... } } style_three = { th = { ... } tv = { ... } } %] [% USE xmlstyle style_one %] [% FILTER xmlstyle style_two %] # style_one and style_two applied here [% END %] [% FILTER xmlstyle style_three %] # style_one and style_three applied here [% END %] Any attributes defined within the source tags will override those specified in the style sheet. [% USE xmlstyle div = { attributes = { align = 'left' } } %] [% FILTER xmlstyle %]
foo
bar
[% END %] The output produced is:
foo
bar
The filter can also be used to change the element from one type to another. [% FILTER xmlstyle th = { element = 'td' attributes = { bgcolor='red' } } %] Heading Value [% END %] The output here is as follows. Notice how the end tag C/thE> is changed to C/tdE> as well as the start tag. Heading Value You can also define text to be added immediately before or after the start or end tags. For example: [% FILTER xmlstyle table = { pre_start = '
' post_end = '
' } th = { element = 'td' attributes = { bgcolor='red' } post_start = '' pre_end = '' } %]
Heading
Value
[% END %] The output produced is:
Heading
Value
=head1 AUTHOR Andy Wardley =head1 COPYRIGHT Copyright (C) 2001-2006 Andy Wardley. All Rights Reserved. This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself. =head1 SEE ALSO L =cut # Local Variables: # mode: perl # perl-indent-level: 4 # indent-tabs-mode: nil # End: # # vim: expandtab shiftwidth=4: