/usr/share/perl5/Template/Plugin/XML/Style.pm is in libtemplate-plugin-xml-perl 2.17-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 | #============================================================= -*-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 <abw@cpan.org>
#
# 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>";
$elem = $match->{ element } || $elem;
$pre = $match->{ pre_end } || '';
$post = $match->{ post_end } || '';
return "$pre</$elem>$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 %]
<table>
<tr>
<td>Foo</td> <td>Bar</td> <td>Baz</td>
</tr>
</table>
[% 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<border="0"> and
C<cellpadding="4"> can be added to E<lt>tableE<gt> elements.
[% USE xmlstyle
table = {
attributes = {
border = 0
cellpadding = 4
}
}
%]
[% FILTER xmlstyle %]
<table>
...
</table>
[% END %]
This produces the output:
<table border="0" cellpadding="4">
...
</table>
Parameters specified within the USE directive are applied
automatically each time the C<xmlstyle> 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"
}
}
%]
<table>
<tr>
...
</tr>
</table>
[% 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 %]
<div>foo</div>
<div align="right">bar</div>
[% END %]
The output produced is:
<div align="left">foo</div>
<div align="right">bar</div>
The filter can also be used to change the element from one type to
another.
[% FILTER xmlstyle
th = {
element = 'td'
attributes = { bgcolor='red' }
}
%]
<tr>
<th>Heading</th>
</tr>
<tr>
<td>Value</td>
</tr>
[% END %]
The output here is as follows. Notice how the end tag
C<E<lt>/thE<gt>> is changed to C<E<lt>/tdE<gt>> as well as the start
tag.
<tr>
<td bgcolor="red">Heading</td>
</tr>
<tr>
<td>Value</td>
</tr>
You can also define text to be added immediately before or after the
start or end tags. For example:
[% FILTER xmlstyle
table = {
pre_start = '<div align="center">'
post_end = '</div>'
}
th = {
element = 'td'
attributes = { bgcolor='red' }
post_start = '<b>'
pre_end = '</b>'
}
%]
<table>
<tr>
<th>Heading</th>
</tr>
<tr>
<td>Value</td>
</tr>
</table>
[% END %]
The output produced is:
<div align="center">
<table>
<tr>
<td bgcolor="red"><b>Heading</b></td>
</tr>
<tr>
<td>Value</td>
</tr>
</table>
</div>
=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<Template::Plugin|Template::Plugin>
=cut
# Local Variables:
# mode: perl
# perl-indent-level: 4
# indent-tabs-mode: nil
# End:
#
# vim: expandtab shiftwidth=4:
|