WikiConverter/Normalizer.pm

filter: Statementstmt 91.0% 92 / 101 Branchbran 64.7% 22 / 34 Conditioncond 45.4% 5 / 11 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 90.0% 18 / 20 Podpod 100% 3 / 3 totaltotal 80.0% 140 / 175 CC 31 SCAR 25.5
CC
31
Cov
82%
_find_handler
31.3
_normalize_css
7.1
_normalize_attrs
4.0
CRAP 12.8
« WikiConverter.pm ↑ Summary WikiConverter/WebApp.pm »
1 package HTML::WikiConverter::Normalizer;
2  
3 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 4 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 4 ▶use CSS;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use HTML::Element;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 =head1 NAME
12  
13 HTML::WikiConverter::Normalizer - Convert CSS styles to (roughly) corresponding HTML
14  
15 =head1 SYNOPSIS
16  
17 use HTML::TreeBuilder;
18 use HTML::WikiConverter::Normalizer;
19  
20 my $tree = new HTML::TreeBuilder();
21 $tree->parse( '<p><font style="font-style:italic; font-weight:bold">text</font></p>' );
22  
23 my $norm = new HTML::WikiConverter::Normalizer();
24 $norm->normalize($tree);
25  
26 # Roughly gives "<p><font><b><i>text</i></b></font></p>"
27 print $tree->as_HTML();
28  
29 =head1 DESCRIPTION
30  
31 L<HTML::WikiConverter> dialects convert HTML into wiki markup. Most
32 (if not all) know nothing about CSS, nor do they take it into
33 consideration when performing html-to-wiki conversion. But there is no
34 good reason for, say, C<E<lt>font
35 style="font-weight:bold"E<gt>textE<lt>/fontE<gt>> not to be converted
36 into C<'''text'''> in the MediaWiki dialect. The same is true of other
37 dialects, all of which should be able to use CSS information to
38 produce wiki markup.
39  
40 The issue becomes especially problematic when considering that several
41 WYSIWYG HTML editors (e.g. Mozilla's) produce this sort of CSS-heavy
42 HTML. Prior to C<HTML::WikiConverter::Normalizer>, this HTML would
43 have been essentially converted to text, the CSS information having
44 been ignored by C<HTML::WikiConverter>.
45  
46 C<HTML::WikiConverter::Normalizer> avoids this with a few simple
47 transformations that convert CSS styles into HTML tags.
48  
49 =head1 METHODS
50  
51 =head2 new
52  
53 my $norm = new HTML::WikiConverter::Normalizer();
54  
55 Constructs a new normalizer
56  
57 =cut
58  
59 29 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
60 29 my( $pkg, %attrs ) = @_;
61 29 my $self = bless \%attrs, $pkg;
62 29 $self->{_css} = new CSS( { parser => 'CSS::Parse::Lite' } );
63 29 $self->{_handlers} = $self->handlers;
64 29 return $self;
65 }
66  
67 =head2 normalize
68  
69 $norm->normalize($elem);
70  
71 Normalizes C<$elem> and all its descendents, where C<$elem> is an
72 L<HTML::Element> object.
73  
74 =cut
75  
76 29 ▶sub normalize {
Subroutine normalize: called CC=1 SCAR=0.0
Pod documented
77 29 my( $self, $root ) = @_;
78 29 $self->_normalize($root);
79 29 $self->_postprocess($root);
80 }
81  
82 =head1 SUBCLASSING
83  
84 The following methods may be useful to subclasses.
85  
86 =cut
87  
88 18 ▶sub _css { shift->{_css} }
Subroutine _css: called CC=1 SCAR=0.0
89 46 ▶sub _handlers { shift->{_handlers} }
Subroutine _handlers: called CC=1 SCAR=0.0
90  
91 =head2 handlers
92  
93 my $handlers = $self->handlers;
94  
95 Class method returning reference to an array of handlers used to
96 convert CSS to HTML. Each handler is a hashref that specifies the CSS
97 properties and values to match, and the HTML tags and attributes the
98 matched properties will be converted to.
99  
100 The C<type>, C<name>, C<value>, and C<tag> keys may be used to match
101 an element's property or attribute. C<type> may be either C<"css"> if
102 matching a CSS property (in which case C<name> must contain the name
103 of the property, and C<value> must contain the property value to
104 match) or C<"attr"> if matching an HTML tag attribute (in which case
105 C<name> must contain the name of the attribute, and C<value> must
106 contain the attribute value to match).
107  
108 C<value> may be a string (for an exact match), regex (which will be
109 used to match against the element's property or attribute value),
110 coderef (which will be passed the property or attribute value and is
111 expected to return true on match, false otherwise), or C<"*"> (which
112 matches any property or attribute value). A tag or list of tags can
113 also be matched with the C<tag> key, which takes either a string or an
114 arrayref.
115  
116 To specify what actions the handler will take, the C<new_tag>,
117 C<new_attr>, and C<normalizer> keys are used. C<new_tag> is required
118 and indicates the name of the tag that will be created. C<attribute>
119 is optional and indicates the name of the attribute in the new tag
120 that will take the value of the original CSS property. If a coderef is
121 given as the C<normalizer>, it will be passed the value of the
122 property/attribute and should return one suitable to be assigned to
123 the new tag attribute.
124  
125 =cut
126  
127 29 ▶sub handlers { [
Subroutine handlers: called CC=1 SCAR=0.0
Pod documented
128 29 { type => 'css', name => 'font-family', value => '*', new_tag => 'font', new_attr => 'face' },
129 { type => 'css', name => 'font-size', value => '*', new_tag => 'font', new_attr => 'size', normalizer => \&_normalize_fontsize },
130 { type => 'css', name => 'color', value => '*', new_tag => 'font', new_attr => 'color', normalizer => \&_normalize_color },
131 { type => 'css', name => 'font-weight', value => 'bold', new_tag => 'b' },
132 { type => 'css', name => 'text-decoration', value => 'underline', new_tag => 'u' },
133 { type => 'css', name => 'font-style', value => 'italic', new_tag => 'i' },
134 { type => 'attr', name => 'align', value => 'center', tag => [ qw/ div p / ], new_tag => 'center' },
135  
136 # { type => 'attr', tag => 'font', attr => 'size', value => '*', new_tag => 'span', style => 'font-size' },
137 ] }
138  
139 0 ▶sub _new_handlers {
Subroutine _new_handlers: not called CC=1 SCAR=6.9
140 0 span_to_font => {
141 xpath => '//[@style[contains(., "font-size")]]',
142 },
143 }
144  
145 1 ▶sub _normalize_color {
Subroutine _normalize_color: called CC=1 SCAR=0.0
146 1 my( $self, $color ) = @_;
147 1 return $color;
148 }
149  
150 0 ▶sub _normalize_fontsize {
Subroutine _normalize_fontsize: not called CC=1 SCAR=6.9
151 0 my( $self, $size ) = @_;
152 0 return $size;
153 }
154  
155 29 ▶sub _normalize {
Subroutine _normalize: called CC=1 SCAR=0.0
156 29 my( $self, $node ) = @_;
157 29 $self->_normalize_css( $node );
158 29 $self->_normalize_attrs( $node );
159 }
160  
161 167 ▶sub _normalize_css {
Subroutine _normalize_css: called CC=7 SCAR=19.6
162 167 my( $self, $node ) = @_;
163  
164 167 $node->objectify_text;
165  
166 # Recurse
167 167 $self->_normalize_css($_) for $node->content_list;
168  
169 167 ▶ my $style_text = $node->attr('style') or return;
Branch: 1 branch
BranchTrueFalseTotal
my $style_text = $node->attr("style") or return 6 161 167
170 6 my $full_css = "this { $style_text }";
171  
172 6 $self->_css->read_string( $full_css );
173 6 my $style = $self->_css->get_style_by_selector('this');
174  
175 6 ▶ my @original_props = @{ $style->{properties} || [] };
Condition50%1/2
$style->{'properties'} || []
l!l
60
MC/DC0%0/1
$style->{'properties'} || []
$style->{'properties'}
Truth table50%1/2
A
A= $style->{'properties'}
Aresult
11
00
176 6 my @new_props = ( );
177  
178 6 foreach my $prop ( @original_props ) {
179 7 my $handler = $self->_find_handler( type => 'css', name => $prop->{property}, value => $prop->{simple_value}, tag => $node->tag );
180 7 ▶ if( $handler ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($handler) { } 7 0 7
181 7 $self->_handle( $handler, $node, $prop->{simple_value} );
182 } else {
183 0 push @new_props, $prop;
184 }
185 }
186  
187 6 $style->{properties} = \@new_props;
188 6 chomp( my $style_string = $style->to_string );
189 6 $style_string =~ s/^this \{\s*(.*?)\s*\}$/$1/;
190 6 $style_string =~ s/\s+$//;
191 6 ▶ $style_string ||= undef;
Condition50%1/2
$style_string ||= undef
l!l
06
MC/DC0%0/1
$style_string ||= undef
$style_string
Truth table50%1/2
A
A= $style_string
Aresult
11
00
192  
193 6 $node->attr( style => $style_string );
194 6 $self->_css->purge();
195 }
196  
197 174 ▶sub _normalize_attrs {
Subroutine _normalize_attrs: called CC=4 SCAR=13.9
198 174 my( $self, $node ) = @_;
199  
200 174 $node->objectify_text();
201  
202 # Recurse
203 174 $self->_normalize_attrs($_) for $node->content_list;
204
205 174 foreach my $attr ( $node->all_external_attr_names ) {
206 39 my $attr_value = $node->attr($attr);
207 39 my $handler = $self->_find_handler( type => 'attr', name => $attr, value => $attr_value, tag => $node->tag );
208 39 ▶ if( $handler ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($handler) 1 38 39
209 1 $self->_handle( $handler, $node, $attr_value );
210 1 $node->attr( $attr => undef );
211 }
212 }
213 }
214  
215 29 ▶sub _postprocess { }
Subroutine _postprocess: called CC=1 SCAR=0.0
216  
217 8 ▶sub _handle {
Subroutine _handle: called CC=4 SCAR=13.9
218 8 my( $self, $handler, $node, $value ) = @_;
219 8 ▶ $value = $handler->{normalizer} ? $handler->{normalizer}->( $self, $value ) : $value;
Branch: 1 branch
BranchTrueFalseTotal
$handler->{'normalizer'} ? : 1 7 8
220  
221 8 my %elem_attrs = ( );
222 8 ▶ $elem_attrs{$handler->{new_attr}} = $value if $handler->{new_attr};
Branch: 1 branch
BranchTrueFalseTotal
if $handler->{"new_attr"} 2 6 8
223  
224 8 my $new_elem = new HTML::Element( $handler->{new_tag}, %elem_attrs );
225  
226 8 foreach my $c ( $node->content_list ) {
227 8 $c->detach;
228 8 $new_elem->push_content($c);
229 }
230  
231 8 $node->push_content($new_elem);
232 }
233  
234 46 ▶sub _find_handler {
Subroutine _find_handler: called CC=17 SCAR=34.4
235 46 my( $self, %args ) = @_;
236  
237 46 my @arg_keys = qw/ type name value /;
238 46 for my $arg ( @arg_keys ) {
239 138 ▶ if( not exists $args{$arg} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $args{$arg}) 0 138 138
240 0 my( $t, $n, $v ) = @args{ @arg_keys };
241 0 ▶ $_ ||= '' for ( $t, $n, $v );
Condition0%0/2
$_ ||= ""
l!l
00
MC/DC0%0/1
$_ ||= ""
$_
Truth table0%0/2
A
A= $_
Aresult
11
00
242  
243 0 croak sprintf "missing required '$arg' key (type: %s, name: %s, value: %s)", $t, $n, $v;
244 }
245 }
246  
247 46 my $type = $args{type};
248 46 my $name = $args{name};
249 46 my $value = $args{value};
250 46 ▶ my $tag = $args{tag} || '';
Condition50%1/2
$args{'tag'} || ''
l!l
460
MC/DC0%0/1
$args{'tag'} || ''
$args{'tag'}
Truth table50%1/2
A
A= $args{'tag'}
Aresult
11
00
251  
252 46 foreach my $handler ( @{ $self->_handlers } ) {
253 303 ▶ next unless $handler->{type} eq $type;
Branch: 1 branch
BranchTrueFalseTotal
unless $handler->{"type"} eq $type 234 69 303
254 69 ▶ next unless $handler->{name} eq $name;
Branch: 1 branch
BranchTrueFalseTotal
unless $handler->{"name"} eq $name 61 8 69
255 8 ▶ next if $handler->{tag} and ! $self->_match_handler_tag( $handler->{tag}, $tag );
Branch: 1 branch
BranchTrueFalseTotal
if $handler->{"tag"} and not $self->_match_handler_tag($handler->{"tag"}, $tag) 0 8 8
Condition66%2/3
$handler->{"tag"} and not $self->_match_handler_tag($handler->{"tag"}, $tag)
!ll&&!rl&&r
710
MC/DC0%0/2
$handler->{"tag"} and not $self->_match_handler_tag($handler->{"tag"}, $tag)
$handler->{"tag"} not $self->_match_handler_tag($handler->{"tag"}, $tag)
Truth table66%2/3
A and not(B)
A= $handler->{"tag"}B= not $self->_match_handler_tag($handler->{"tag"}, $tag)
ABresult
0X0
100
111
256  
257 8 ▶ if( ref $handler->{value} eq 'Regexp' ) {
Branch: 3 branches
BranchTrueFalseTotal
if (ref $handler->{'value'} eq 'Regexp') { } 0 8 8
elsif (ref $handler->{'value'} eq 'CODE') { } 0 8 8
elsif ($handler->{'value'} eq '*') { } 2 6 8
258 return $handler if $value =~ $handler->{value}
259 0 ▶ } elsif( ref $handler->{value} eq 'CODE' ) {
Branch: 1 branch
BranchTrueFalseTotal
if $value =~ /$handler->{"value"}/ 0 0 0
260 0 ▶ return $handler if $handler->{value}->( $value );
Branch: 1 branch
BranchTrueFalseTotal
if $handler->{"value"}->($value) 0 0 0
261 } elsif( $handler->{value} eq '*') {
262 2 return $handler;
263 } else {
264 6 ▶ return $handler if $handler->{value} eq $value;
Branch: 1 branch
BranchTrueFalseTotal
if $handler->{"value"} eq $value 6 0 6
265 }
266 }
267  
268 38 return;
269 }
270  
271 1 ▶sub _match_handler_tag {
Subroutine _match_handler_tag: called CC=3 SCAR=11.4
272 1 my( $self, $handler_tag, $tag ) = @_;
273 1 ▶ my %handler_tags = map { $_ => 1 } ( ref $handler_tag eq 'ARRAY' ? @$handler_tag : $handler_tag );
Branch: 1 branch
BranchTrueFalseTotal
ref $handler_tag eq 'ARRAY' ? : 1 0 1
274 1 ▶ return $handler_tags{$tag} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$handler_tags{$tag} ? : 1 0 1
275 }
276  
277 =head1 SEE ALSO
278  
279 L<CSS>
280  
281 =head1 AUTHOR
282  
283 David J. Iberri, C<< <diberri@cpan.org> >>
284  
285 =head1 BUGS
286  
287 Please report any bugs or feature requests to C<bug-html-wikiconverter
288 at rt.cpan.org>, or through the web interface at
289 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=HTML-WikiConverter>.
290 I will be notified, and then you'll automatically be notified of
291 progress on your bug as I make changes.
292  
293 =head1 COPYRIGHT & LICENSE
294  
295 Copyright 2006 David J. Iberri, all rights reserved.
296  
297 This program is free software; you can redistribute it and/or modify it
298 under the same terms as Perl itself.
299  
300 =cut
301  
302 4 1;
« WikiConverter.pm ↑ Summary WikiConverter/WebApp.pm »