Wright/XHTML.pm untested

filter: Statementstmt 0.0% 0 / 96 Branchbran 0.0% 0 / 34 Conditioncond 0.0% 0 / 10 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 0.0% 0 / 15 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 167 CC 18 SCAR 47.4
CC
18
Cov
0%
_add_prepared_row
240.0
_prepare
6.0
addsheet
6.0
CRAP 114.3
1 package Spreadsheet::Wright::XHTML;
2  
3 use 5.010;
4 use strict;
5 use warnings;
6 no warnings qw( uninitialized numeric );
7  
8 BEGIN {
9 $Spreadsheet::Wright::XHTML::VERSION = '0.107';
10 $Spreadsheet::Wright::XHTML::AUTHORITY = 'cpan:TOBYINK';
11 }
12  
13 use Carp;
14 use XML::LibXML;
15  
16 use parent qw(Spreadsheet::Wright);
17 use constant XHTML_NS => 'http://www.w3.org/1999/xhtml';
18  
19 sub new
20 {
21 my ($class, %args) = @_;
22
23 $args{'title'} //= 'Data';
24
25 my $self = bless { 'options' => \%args }, $class;
26
27 $self->{'_FILENAME'} = $args{'file'} // $args{'filename'}
28 or croak "Need filename.";
29  
30 return $self;
31 }
32  
33 sub _prepare
34 {
35 my $self = shift;
36
37 return $self if $self->{'document'};
38
39 $self->{'document'} = XML::LibXML->createDocument;
40 $self->{'document'}->setDocumentElement(
41 $self->{'document'}->createElementNS(XHTML_NS,'html')
42 );
43 $self->{'document'}->documentElement
44 ->addNewChild(XHTML_NS, 'head')
45 ->addNewChild(XHTML_NS, 'title')
46 ->appendText($self->{'options'}->{'title'});
47 $self->{'body'} = $self->{'document'}->documentElement
48 ->addNewChild(XHTML_NS, 'body');
49 $self->{'body'}->addNewChild(XHTML_NS, 'h1')
50 ->appendText($self->{'options'}->{'title'});
51 $self->addsheet($self->{'options'}->{'sheet'});
52
53 return $self;
54 }
55  
56 sub addsheet
57 {
58 my ($self, $caption) = @_;
59  
60 $self->{'worksheet'} = $self->{'body'}->addNewChild(XHTML_NS, 'table');
61  
62 if (defined $caption)
63 {
64 $self->{'worksheet'}->addNewChild(XHTML_NS, 'caption')->appendText($caption);
65 }
66  
67 $self->{'tbody'} = $self->{'worksheet'}->addNewChild(XHTML_NS, 'tbody');
68  
69 return $self;
70 }
71  
72 sub _add_prepared_row
73 {
74 my $self = shift;
75  
76 my $tr = $self->{'tbody'}->addNewChild(XHTML_NS, 'tr');
77
78 foreach my $cell (@_)
79 {
80 my $td;
81 if ($cell->{'header'})
82 {
83 $td = $tr->addNewChild(XHTML_NS, 'th');
84 }
85 else
86 {
87 $td = $tr->addNewChild(XHTML_NS, 'td');
88 }
89
90 my $content = $cell->{'content'};
91 $content = sprintf($cell->{'sprintf'}, $content)
92 if defined $cell->{'sprintf'};
93
94 $td->appendText($content);
95
96 if (defined $cell->{'style'} && defined $self->{'options'}->{'styles'}->{ $cell->{'style'} })
97 {
98 while (my ($k, $v) = each %{ $self->{'options'}->{'styles'}->{ $cell->{'style'} } })
99 {
100 $cell->{$k} = $v
101 unless defined $cell->{$k};
102 }
103 }
104
105 my %styles;
106 if ($cell->{'font_weight'} eq 'bold')
107 {
108 $styles{'font-weight'} = 'bold';
109 }
110 if ($cell->{'font_style'} eq 'italic')
111 {
112 $styles{'font-style'} = 'italics';
113 }
114 if ($cell->{'font_decoration'} =~ m'underline')
115 {
116 $styles{'text-decoration'} = 'underline';
117 }
118 if ($cell->{'font_decoration'} =~ m'strikeout')
119 {
120 $styles{'text-decoration'} = 'line-through';
121 }
122 if (defined $cell->{'font_color'})
123 {
124 $styles{'color'} = $cell->{'font_color'};
125 }
126 if (defined $cell->{'font_face'})
127 {
128 $styles{'font-family'}=sprintf('"%s"', $cell->{'font_face'});
129 }
130 if (defined $cell->{'font_size'})
131 {
132 $styles{'font-size'}=$cell->{'font_size'}.'pt';
133 }
134 if (defined $cell->{'align'})
135 {
136 $styles{'text-align'}=$cell->{'align'};
137 }
138 if (defined $cell->{'valign'})
139 {
140 $styles{'valign'}=$cell->{'valign'};
141 }
142  
143 my $style;
144 while (my ($k, $v) = each %styles)
145 {
146 $style .= sprintf('%s: %s; ', $k, $v);
147 }
148 $style =~ s/; $//;
149
150 $td->setAttribute('style', $style) if length $style;
151 }
152
153 return $self;
154 }
155  
156 sub close
157 {
158 my $self=shift;
159 return if $self->{'_CLOSED'};
160 $self->{'_FH'}->print( $self->_make_output );
161 $self->{'_FH'}->close;
162 $self->{'_CLOSED'}=1;
163 return $self;
164 }
165  
166 sub _make_output
167 {
168 my $self = shift;
169 return $self->{'document'}->toString;
170 }
171  
172 1;