File Coverage

blib/lib/WE_Frontend/Plugin/TextTable.pm
Criterion Covered Total %
statement 12 92 13.0
branch 0 36 0.0
condition 0 13 0.0
subroutine 4 8 50.0
pod 2 4 50.0
total 18 153 11.7


\n"; "; \n"; "; " } @cols; \n";
line stmt bran cond sub pod time code
1             # -*- perl -*-
2              
3             #
4             # $Id: TextTable.pm,v 1.10 2004/02/06 16:51:08 eserte Exp $
5             # Author: Slaven Rezic
6             #
7             # Copyright (C) 2003 Slaven Rezic.
8             # This is free software; you can redistribute it and/or modify it under the
9             # terms of the GNU General Public License, see the file COPYING.
10              
11             #
12             # Mail: slaven@rezic.de
13             # WWW: http://we-framework.sourceforge.net
14             #
15              
16             package WE_Frontend::Plugin::TextTable;
17 1     1   63587 use base qw(Template::Plugin);
  1         3  
  1         1161  
18              
19 1     1   1018 use HTML::Entities;
  1         3  
  1         67  
20              
21 1     1   5 use strict;
  1         2  
  1         23  
22 1     1   6 use vars qw($VERSION);
  1         2  
  1         987  
23             $VERSION = sprintf("%d.%02d", q$Revision: 1.10 $ =~ /(\d+)\.(\d+)/);
24              
25             sub fixeol ($);
26              
27             =head1 NAME
28              
29             WE_Frontend::Plugin::TextTable - format a textual table description to HTML
30              
31             =head1 SYNOPSIS
32              
33             my $t = Template->new({PLUGIN_BASE => "WE_Frontend::Plugin"});
34              
35             [% USE TextTable %]
36             [% TextTable.out(tabledef) %]
37              
38             =head1 DESCRIPTION
39              
40             Format a textual table description into HTML. See also the "table"
41             definitions in F in the web.editor
42             distribution.
43              
44             =head2 METHODS
45              
46             =over
47              
48             =cut
49              
50             sub new {
51 0     0 1   my($class, $context) = @_;
52 0           my $self = {Context => $context};
53 0           bless $self, $class;
54             }
55              
56             =item out($tabledef)
57              
58             Format and output the given table definition.
59              
60             =cut
61              
62             sub out {
63 0     0 1   my($self, $tabledef) = @_;
64 0           my $type = $tabledef->{type};
65 0 0         if ($type ne "table") {
66 0           warn "Element type is `$type', expected `table'";
67             }
68 0           my $title = fixeol $tabledef->{title};
69 0           my $css = fixeol $tabledef->{css};
70 0           my $sep = quotemeta fixeol $tabledef->{sep};
71 0           my $head = fixeol $tabledef->{tablehead};
72 0           my $head_align = fixeol $tabledef->{tablehead_align};
73 0           my $head_is_html = $tabledef->{tablehead_ishtml};
74 0           my $subhead = fixeol $tabledef->{tablesubhead};
75 0           my $subhead_align = fixeol $tabledef->{tablesubhead_align};
76 0           my $subhead_is_html = $tabledef->{tablesubhead_ishtml};
77 0           my $text_align = fixeol $tabledef->{tabletext_align};
78 0           my $text = fixeol $tabledef->{tabletext};
79 0           my @rows = split /\n/, $text;
80 0           my $out = "
81 0 0 0       if (defined $css && $css ne "") {
82 0           $out .= " class='$css'";
83             }
84 0           $out .= ">\n";
85 0 0 0       if (defined $title && $title ne "") {
86 0           $out .= "
" . HTML::Entities::encode($title) . "
87             }
88 0           for my $def ([$head, $head_is_html, $head_align],
89             [$subhead, $subhead_is_html, $subhead_align]
90             ) {
91 0           my($h, $is_html, $align) = @$def;
92 0 0         $align = "" if !defined $align;
93 0           my @style = map {
94 0           my $style = parse_align($_);
95 0 0         if ($style ne "") {
96 0           $style = " style='" . $style . "'";
97             }
98 0           $style;
99             } split $sep, $align;
100              
101 0 0 0       if (defined $h && $h ne "") {
102 0           $out .= "
103 0 0         if ($is_html) {
104 0           $out .= $h;
105             } else {
106 0           my @cols = split $sep, $h;
107 0           my $col_i = 0;
108 0 0         $out .= join "", map { my $s = defined $_ ? $_ : "";
  0            
109 0   0       my $style = $style[$col_i++] || "";
110 0           "" . HTML::Entities::encode($s) . "" } @cols;
111             }
112 0           $out .= "
113             }
114             }
115              
116 0 0         $text_align = "" if !defined $text_align;
117 0           my @td_style = map {
118 0           my $style = parse_align($_);
119 0 0         if ($style ne "") {
120 0           $style = " style='" . $style . "'";
121             }
122 0           $style;
123             } split $sep, $text_align;
124              
125 0           for my $row (@rows) {
126 0           $out .= "
127 0           my @cols = split $sep, $row;
128 0           my $col_i = 0;
129 0 0         $out .= join "", map { my $s = defined $_ ? $_ : "";
  0            
130 0   0       my $style = $td_style[$col_i++] || "";
131 0           "" . HTML::Entities::encode($s) . "
132 0           $out .= "
133             }
134 0           $out .= "
\n"; 135               136 0           return $out; 137             } 138               139             sub fixeol ($) { 140 0     0 0   my $s = shift; 141 0 0         return if !defined $s; 142 0           $s =~ s/\015//g; 143 0           $s; 144             } 145               146             sub parse_align { 147 0     0 0   local $_ = shift; 148 0           my $style = ""; 149 0 0         if (/[   0             150 0 0         if (/[>r]/) { $style .= ' text-align:right;' }   0             151 0 0         if (/[\|c]/) { $style .= ' text-align:center;' }   0             152 0 0         if (/[\^t]/) { $style .= ' vertical-align:top;' }   0             153 0 0         if (/[vb]/) { $style .= ' vertical-align:bottom;' }   0             154 0 0         if (/[-m]/) { $style .= ' vertical-align:middle;' }   0             155 0           $style; 156             } 157               158             1; 159               160             __END__