home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2004 December / PCpro_2004_12.ISO / files / webserver / tsw / TSW_3.4.0.exe / Apache2 / perl / Style.pm < prev    next >
Encoding:
Perl POD Document  |  2004-01-30  |  8.0 KB  |  358 lines

  1. #============================================================= -*-Perl-*-
  2. #
  3. # Template::Plugin::XML::Style
  4. #
  5. # DESCRIPTION
  6. #   Template Toolkit plugin which performs some basic munging of XML 
  7. #   to perform simple stylesheet like transformations.
  8. #
  9. # AUTHOR
  10. #   Andy Wardley   <abw@kfs.org>
  11. #
  12. # COPYRIGHT
  13. #   Copyright (C) 2001 Andy Wardley.  All Rights Reserved.
  14. #
  15. #   This module is free software; you can redistribute it and/or
  16. #   modify it under the same terms as Perl itself.
  17. #
  18. # REVISION
  19. #   $Id: Style.pm,v 2.34 2004/01/13 16:21:50 abw Exp $
  20. #
  21. #============================================================================
  22.  
  23. package Template::Plugin::XML::Style;
  24.  
  25. require 5.004;
  26.  
  27. use strict;
  28. use Template::Plugin::Filter;
  29.  
  30. use base qw( Template::Plugin::Filter );
  31. use vars qw( $VERSION $DYNAMIC $FILTER_NAME );
  32.  
  33. $VERSION = sprintf("%d.%02d", q$Revision: 2.34 $ =~ /(\d+)\.(\d+)/);
  34. $DYNAMIC = 1;
  35. $FILTER_NAME = 'xmlstyle';
  36.  
  37.  
  38. #------------------------------------------------------------------------
  39. # new($context, \%config)
  40. #------------------------------------------------------------------------
  41.  
  42. sub init {
  43.     my $self = shift;
  44.     my $name = $self->{ _ARGS }->[0] || $FILTER_NAME;
  45.     $self->install_filter($name);
  46.     return $self;
  47. }
  48.  
  49.  
  50. sub filter {
  51.     my ($self, $text, $args, $config) = @_;
  52.  
  53.     # munge start tags
  54.     $text =~ s/ < ([\w\.\:]+) ( \s+ [^>]+ )? > 
  55.           / $self->start_tag($1, $2, $config)
  56.           /gsex;
  57.  
  58.     # munge end tags
  59.     $text =~ s/ < \/ ([\w\.\:]+) > 
  60.           / $self->end_tag($1, $config)
  61.           /gsex;
  62.  
  63.     return $text;
  64.  
  65. }
  66.  
  67.  
  68. sub start_tag {
  69.     my ($self, $elem, $textattr, $config) = @_;
  70.     $textattr ||= '';
  71.     my ($pre, $post);
  72.  
  73.     # look for an element match in the stylesheet
  74.     my $match = $config->{ $elem } 
  75.     || $self->{ _CONFIG }->{ $elem }
  76.         || return "<$elem$textattr>";
  77.     
  78.     # merge element attributes into copy of stylesheet attributes
  79.     my $attr = { %{ $match->{ attributes } || { } } };
  80.     while ($textattr =~ / \s* ([\w\.\:]+) = " ([^"]+) " /gsx ) {
  81.     $attr->{ $1 } = $2;
  82.     }
  83.     $textattr = join(' ', map { "$_=\"$attr->{$_}\"" } keys %$attr);
  84.     $textattr = " $textattr" if $textattr;
  85.  
  86.     $elem = $match->{ element    } || $elem;
  87.     $pre  = $match->{ pre_start  } || '';
  88.     $post = $match->{ post_start } || '';
  89.  
  90.     return "$pre<$elem$textattr>$post";
  91. }
  92.  
  93.  
  94. sub end_tag {
  95.     my ($self, $elem, $config) = @_;
  96.     my ($pre, $post);
  97.  
  98.     # look for an element match in the stylesheet
  99.     my $match = $config->{ $elem } 
  100.     || $self->{ _CONFIG }->{ $elem }
  101.     || return "</$elem>";
  102.     
  103.     $elem = $match->{ element  } || $elem;
  104.     $pre  = $match->{ pre_end  } || '';
  105.     $post = $match->{ post_end } || '';
  106.     
  107.     return "$pre</$elem>$post";
  108. }
  109.  
  110.  
  111. 1;
  112.  
  113. __END__
  114.  
  115.  
  116. #------------------------------------------------------------------------
  117. # IMPORTANT NOTE
  118. #   This documentation is generated automatically from source
  119. #   templates.  Any changes you make here may be lost.
  120. #   The 'docsrc' documentation source bundle is available for download
  121. #   from http://www.template-toolkit.org/docs.html and contains all
  122. #   the source templates, XML files, scripts, etc., from which the
  123. #   documentation for the Template Toolkit is built.
  124. #------------------------------------------------------------------------
  125.  
  126. =head1 NAME
  127.  
  128. Template::Plugin::XML::Style - Simple XML stylesheet transfomations
  129.  
  130. =head1 SYNOPSIS
  131.  
  132.     [% USE xmlstyle 
  133.            table = { 
  134.                attributes = { 
  135.                    border      = 0
  136.                    cellpadding = 4
  137.                    cellspacing = 1
  138.                }
  139.            }
  140.     %]
  141.  
  142.     [% FILTER xmlstyle %]
  143.     <table>
  144.     <tr>
  145.       <td>Foo</td> <td>Bar</td> <td>Baz</td>
  146.     </tr>
  147.     </table>
  148.     [% END %]
  149.  
  150. =head1 DESCRIPTION
  151.  
  152. This plugin defines a filter for performing simple stylesheet based 
  153. transformations of XML text.  
  154.  
  155. Named parameters are used to define those XML elements which require
  156. transformation.  These may be specified with the USE directive when
  157. the plugin is loaded and/or with the FILTER directive when the plugin
  158. is used.
  159.  
  160. This example shows how the default attributes C<border="0"> and
  161. C<cellpadding="4"> can be added to E<lt>tableE<gt> elements.
  162.  
  163.     [% USE xmlstyle 
  164.            table = { 
  165.                attributes = { 
  166.                    border      = 0
  167.                    cellpadding = 4
  168.                }
  169.            }
  170.     %]
  171.  
  172.     [% FILTER xmlstyle %]
  173.     <table>
  174.        ...
  175.     </table>
  176.     [% END %]
  177.  
  178. This produces the output:
  179.  
  180.     <table border="0" cellpadding="4">
  181.        ...
  182.     </table>
  183.  
  184. Parameters specified within the USE directive are applied automatically each
  185. time the C<xmlstyle> FILTER is used.  Additional parameters passed to the 
  186. FILTER directive apply for only that block.
  187.  
  188.     [% USE xmlstyle 
  189.            table = { 
  190.                attributes = { 
  191.                    border      = 0
  192.                    cellpadding = 4
  193.                }
  194.            }
  195.     %]
  196.  
  197.     [% FILTER xmlstyle
  198.            tr = {
  199.                attributes = {
  200.                    valign="top"
  201.                }
  202.            }
  203.     %]
  204.     <table>
  205.        <tr>
  206.          ...
  207.        </tr>
  208.     </table>
  209.     [% END %]
  210.  
  211. Of course, you may prefer to define your stylesheet structures once and 
  212. simply reference them by name.  Passing a hash reference of named parameters
  213. is just the same as specifying the named parameters as far as the Template 
  214. Toolkit is concerned.
  215.  
  216.     [% style_one = {
  217.           table = { ... }
  218.           tr    = { ... }
  219.        }
  220.        style_two = {
  221.           table = { ... }
  222.           td    = { ... }
  223.        }
  224.        style_three = {
  225.           th = { ... }
  226.           tv = { ... }
  227.        }
  228.     %]
  229.  
  230.     [% USE xmlstyle style_one %]
  231.  
  232.     [% FILTER xmlstyle style_two %]
  233.        # style_one and style_two applied here 
  234.     [% END %]
  235.       
  236.     [% FILTER xmlstyle style_three %]
  237.        # style_one and style_three applied here 
  238.     [% END %]
  239.  
  240. Any attributes defined within the source tags will override those specified
  241. in the style sheet.
  242.  
  243.     [% USE xmlstyle 
  244.            div = { attributes = { align = 'left' } } 
  245.     %]
  246.  
  247.  
  248.     [% FILTER xmlstyle %]
  249.     <div>foo</div>
  250.     <div align="right">bar</div>
  251.     [% END %]
  252.  
  253. The output produced is:
  254.  
  255.     <div align="left">foo</div>
  256.     <div align="right">bar</div>
  257.  
  258. The filter can also be used to change the element from one type to another.
  259.  
  260.     [% FILTER xmlstyle 
  261.               th = { 
  262.                   element = 'td'
  263.                   attributes = { bgcolor='red' }
  264.               }
  265.     %]
  266.     <tr>
  267.       <th>Heading</th>
  268.     </tr>
  269.     <tr>
  270.       <td>Value</td>
  271.     </tr>
  272.     [% END %]
  273.  
  274. The output here is as follows.  Notice how the end tag C<E<lt>/thE<gt>> is
  275. changed to C<E<lt>/tdE<gt>> as well as the start tag.
  276.  
  277.     <tr>
  278.       <td bgcolor="red">Heading</td>
  279.     </tr>
  280.     <tr>
  281.       <td>Value</td>
  282.     </tr>
  283.  
  284. You can also define text to be added immediately before or after the 
  285. start or end tags.  For example:
  286.  
  287.     [% FILTER xmlstyle 
  288.               table = {
  289.                   pre_start = '<div align="center">'
  290.                   post_end  = '</div>'
  291.               }
  292.               th = { 
  293.                   element    = 'td'
  294.                   attributes = { bgcolor='red' }
  295.                   post_start = '<b>'
  296.                   pre_end    = '</b>'
  297.               }
  298.     %]
  299.     <table>
  300.     <tr>
  301.       <th>Heading</th>
  302.     </tr>
  303.     <tr>
  304.       <td>Value</td>
  305.     </tr>
  306.     </table>
  307.     [% END %]
  308.  
  309. The output produced is:
  310.  
  311.     <div align="center">
  312.     <table>
  313.     <tr>
  314.       <td bgcolor="red"><b>Heading</b></td>
  315.     </tr>
  316.     <tr>
  317.       <td>Value</td>
  318.     </tr>
  319.     </table>
  320.     </div>
  321.  
  322. =head1 AUTHOR
  323.  
  324. Andy Wardley E<lt>abw@andywardley.comE<gt>
  325.  
  326. L<http://www.andywardley.com/|http://www.andywardley.com/>
  327.  
  328.  
  329.  
  330.  
  331. =head1 VERSION
  332.  
  333. 2.34, distributed as part of the
  334. Template Toolkit version 2.13, released on 30 January 2004.
  335.  
  336. =head1 COPYRIGHT
  337.  
  338.   Copyright (C) 1996-2004 Andy Wardley.  All Rights Reserved.
  339.   Copyright (C) 1998-2002 Canon Research Centre Europe Ltd.
  340.  
  341. This module is free software; you can redistribute it and/or
  342. modify it under the same terms as Perl itself.
  343.  
  344. =head1 SEE ALSO
  345.  
  346. L<Template::Plugin|Template::Plugin>
  347.  
  348. =cut
  349.  
  350. # Local Variables:
  351. # mode: perl
  352. # perl-indent-level: 4
  353. # indent-tabs-mode: nil
  354. # End:
  355. #
  356. # vim: expandtab shiftwidth=4:
  357.