Lite/Utility.pm

filter: Statementstmt 84.9% 79 / 93 Branchbran 60.7% 17 / 28 Conditioncond 50.0% 11 / 22 MC/DCmcdc 21.4% 3 / 14 Subroutinesub 100% 8 / 8 Podpod 100% 1 / 1 totaltotal 71.6% 119 / 166 CC 31 SCAR 35.9
CC
31
Cov
75%
_convert_html_element_to_tei_element
49.6
tei_convert_html_fragment
5.1
BEGIN
1.0
CRAP 36.1
↑ Summary Lite/Document.pm »
1 package TEI::Lite::Utility;
2 ######################################################################
3 ## ##
4 ## Package: Utility.pm ##
5 ## Author: D. Hageman <dhageman@dracken.com> ##
6 ## ##
7 ## Description: ##
8 ## ##
9 ## Perl object designed to assist the user in the creation and ##
10 ## manipulation of TEILite documents. ##
11 ## ##
12 ######################################################################
13  
14 ##==================================================================##
15 ## Libraries and Variables ##
16 ##==================================================================##
17  
18 6 require 5.006;
19 6 require Exporter;
20  
21 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 6 ▶use XML::LibXML;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 6 ▶use TEI::Lite::Element;
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 6 our @ISA = qw( Exporter );
28  
29 6 our @EXPORT = qw( tei_convert_html_fragment );
30  
31 6 our $VERSION = "0.60";
32  
33 6 our %HTML2TEI = (
34 'a' => [ 'link' ],
35 'abbr' => [ 'tei_abbr', {} ],
36 'acronym' => [ 'tei_abbr', {} ],
37 'address' => [ 'tei_address', {} ],
38 'applet' => [ undef ],
39 'area' => [ undef ],
40 'b' => [ 'tei_hi', { rend => 'bold' } ],
41 'base' => [ undef ],
42 'basefont' => [ undef ],
43 'bdo' => [ undef ],
44 'big' => [ 'tei_hi', { rend => 'bold' } ],
45 'blockquote' => [ 'tei_div', {} ],
46 'br' => [ 'tei_lb', {} ],
47 'center' => [ 'tei_hi', { rend => 'center' } ],
48 'cite' => [ 'tei_cit', {} ],
49 'code' => [ 'tei_code', {} ],
50 'col' => [ undef ],
51 'colgroup' => [ undef ],
52 'comment' => [ 'comment' ],
53 'dd' => [ undef ],
54 'del' => [ undef ],
55 'dfn' => [ undef ],
56 'div' => [ undef ],
57 'dl' => [ undef ],
58 'dt' => [ undef ],
59 'em' => [ 'tei_emph', {} ],
60 'fieldset' => [ undef ],
61 'font' => [ undef ],
62 'h1' => [ 'tei_head', {} ],
63 'h2' => [ 'tei_head', {} ],
64 'h3' => [ 'tei_head', {} ],
65 'h4' => [ 'tei_head', {} ],
66 'h5' => [ 'tei_head', {} ],
67 'h6' => [ 'tei_head', {} ],
68 'hr' => [ 'tei_pb', { rend => 'hr' } ],
69 'i' => [ 'tei_hi', { rend => 'italic' } ],
70 'img' => [ 'figure' ],
71 'ins' => [ undef ],
72 'isindex' => [ undef ],
73 'kbd' => [ undef ],
74 'legend' => [ undef ],
75 'li' => [ 'tei_item', {} ],
76 'link' => [ undef ],
77 'ol' => [ 'tei_list', { type => 'ordered' } ],
78 'p' => [ 'tei_p', {} ],
79 'pre' => [ undef ],
80 'q' => [ 'tei_hi', { rend => 'quoted' } ],
81 's' => [ undef ],
82 'samp' => [ 'tei_hi', { rend => 'italic' } ],
83 'small' => [ 'tei_hi', { rend => 'normal' } ],
84 'span' => [ undef ],
85 'strike' => [ 'tei_hi', { rend => 'strike-through' } ],
86 'strong' => [ 'tei_hi', { rend => 'bold' } ],
87 'style' => [ undef ],
88 'sub' => [ undef ],
89 'table' => [ 'tei_table', {} ],
90 'tbody' => [ undef ],
91 'td' => [ 'tei_cell', {} ],
92 'tfoot' => [ undef ],
93 'th' => [ undef ],
94 'thead' => [ undef ],
95 'tr' => [ 'tei_row', {} ],
96 'tt' => [ 'tei_h', { rend => 'monotype' } ],
97 'u' => [ 'tei_hi', { rend => 'underline' } ],
98 'ul' => [ 'tei_list', { type => 'bulleted' } ],
99 'var' => [ 'tei_hi', { rend => 'italic' } ]
100 );
101  
102 ##==================================================================##
103 ## Function(s) ##
104 ##==================================================================##
105  
106 ##----------------------------------------------##
107 ## tei_convert_html_fragment ##
108 ##----------------------------------------------##
109 sub tei_convert_html_fragment ($$@)
110 1 ▶{
Subroutine tei_convert_html_fragment: called CC=5 SCAR=16.3
Pod documented
111 1 my( $user_conversions, $format, @html ) = @_;
112
113 ## Define a variable to hold our HTML DOM tree.
114 1 my $html = join( '', @html );
115
116 ## Create a new document to hold our data.
117 1 my $dom = XML::LibXML::Document->new( '1.0' );
118
119 ## Default the format to be '0' if it isn't defined.
120 1 ▶ $format = 0 if !defined( $format );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $format 0 1 1
121
122 ## Create a new XML::LibXML parser to play with.
123 1 my $parser = XML::LibXML->new();
124  
125 eval
126 1 {
127 ## Attempt to parse the html data into a workable DOM tree.
128 1 $html = $parser->parse_html_string( $html );
129 };
130  
131 1 ▶ if( $@ )
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 1 1
132 {
133 0 return( undef );
134 }
135 else
136 {
137 ## Create a document fragment to insert our nodes into.
138 1 my $dom_fragment = $dom->createDocumentFragment;
139
140 1 foreach( $html->documentElement->findnodes( "//body/*" ) )
141 {
142 3 my( @elements ) =
143 _convert_html_element_to_tei_element( $user_conversions, $_ );
144  
145 3 foreach my $element ( @elements )
146 {
147 3 $dom_fragment->appendChild( $element );
148 }
149 }
150  
151 1 return( $dom_fragment->toString( $format ) );
152 }
153 }
154  
155 ##==================================================================##
156 ## Private Function(s) ##
157 ##==================================================================##
158  
159 ##----------------------------------------------##
160 ## _convert_html_element_to_tei_element ##
161 ##----------------------------------------------##
162 ## Private helper function for the TEI to ##
163 ## HTML conversion function. ##
164 ##----------------------------------------------##
165 sub _convert_html_element_to_tei_element
166 13 ▶{
Subroutine _convert_html_element_to_tei_element: called CC=27 SCAR=39.0
167 13 my( $user_conversions, $node ) = @_;
168  
169 ## Define an element to hold our scratch data and other elements.
170 13 my @result;
171 13 my $function,
172 my %attributes;
173
174 ## Simplest case is if the data is text - we can just return that to
175 ## be appended.
176 13 ▶ if( ref( $node ) eq ( "XML::LibXML::Text" ) )
Branch: 1 branch
BranchTrueFalseTotal
if (ref $node eq "XML::LibXML::Text") 7 6 13
177 {
178 7 return( $node );
179 }
180  
181 ## Determine which html node we are really dealing with ...
182 6 my $name = lc( $node->nodeName );
183  
184 ## Grab the conversion routine for this element from the converstion
185 ## hash we have already defined.
186 6 ▶ if( ( defined( $user_conversions ) ) &&
Branch: 1 branch
BranchTrueFalseTotal
if (defined $user_conversions and ref $user_conversions eq 'HASH') { } 6 0 6
Condition33%1/3
defined $user_conversions and ref $user_conversions eq "HASH"
!ll&&!rl&&r
006
MC/DC0%0/2
defined $user_conversions and ref $user_conversions eq "HASH"
defined $user_conversions ref $user_conversions eq "HASH"
Truth table33%1/3
A and B
A= defined $user_conversionsB= ref $user_conversions eq "HASH"
ABresult
0X0
100
111
187 ( ref( $user_conversions ) eq "HASH" ) )
188 {
189 6 $function = @{ $user_conversions->{ $name } }[0];
190  
191 6 ▶ if( !defined( $function ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $function) 6 0 6
192 {
193 6 $function = @{ $HTML2TEI{ $name } }[0];
194 }
195 }
196 else
197 {
198 0 $function = @{ $HTML2TEI{ $name } }[0];
199 }
200  
201 ## Check to see if the conversion function is defined our not.
202 6 ▶ if( ( !defined( $function ) ) && ( $node->hasChildNodes() ) )
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $function and $node->hasChildNodes) 0 6 6
Condition33%1/3
not defined $function and $node->hasChildNodes
!ll&&!rl&&r
600
MC/DC0%0/2
not defined $function and $node->hasChildNodes
not defined $function $node->hasChildNodes
Truth table33%1/3
not(A) and B
A= not defined $functionB= $node->hasChildNodes
ABresult
0X0
100
111
203 {
204 0 ▶ if( $node->hasChildNodes() )
Branch: 1 branch
BranchTrueFalseTotal
if ($node->hasChildNodes) { } 0 0 0
205 {
206 0 my( @children ) = $node->childNodes();
207
208 0 foreach( @children )
209 {
210 0 push( @result,
211 _convert_html_element_to_tei_element(
212 $user_conversions, $_ ) );
213 }
214 }
215 else
216 {
217 0 return( XML::LibXML::Text->new( " " ) );
218 }
219 }
220  
221 ## This is our true main case ... almost all the converstion elements
222 ## get done on this code branch.
223 6 ▶ if( ( defined( $function ) ) && ( $function =~ /tei/ ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $function and $function =~ /tei/) 4 2 6
Condition66%2/3
defined $function and $function =~ /tei/
!ll&&!rl&&r
024
MC/DC50%1/2
defined $function and $function =~ /tei/
defined $function $function =~ /tei/
Truth table66%2/3
A and B
A= defined $functionB= $function =~ /tei/
ABresult
0X0
100
111
224 {
225 4 ▶ if( defined( @{ $user_conversions->{ $name } }[1] ) )
Branch: 2 branches
BranchTrueFalseTotal
if (defined @{$$user_conversions{$name};}[1]) { } 0 4 4
elsif (defined @{$TEI::Lite::Utility::HTML2TEI{$name};}[1]) { } 4 0 4
226 {
227 0 %attributes = %{ @{ $user_conversions->{ $name } }[1] };
228 }
229 4 elsif( defined( @{ $HTML2TEI{ $name } }[1] ) )
230 {
231 ## Grab the attributes out of our converstion hash.
232 4 %attributes = %{ @{ $HTML2TEI{ $name } }[1] };
233 }
234  
235 6 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
236 4 $result[0] = &$function( \%attributes);
237 6 ▶ use strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
238  
239 ## Loop through each of the child nodes ...
240 4 foreach( $node->childNodes() )
241 {
242 9 my( @children ) =
243 _convert_html_element_to_tei_element( $user_conversions, $_ );
244
245 ## Loop through each of those child nodes ...
246 9 foreach my $child ( @children )
247 {
248 9 $result[0]->appendChild( $child );
249 }
250 }
251 }
252  
253 ## We have a special case for comment nodes.
254 6 ▶ if( ( defined( $function ) ) && ( $function eq "comment" ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $function and $function eq "comment") 0 6 6
Condition33%1/3
defined $function and $function eq "comment"
!ll&&!rl&&r
060
MC/DC0%0/2
defined $function and $function eq "comment"
defined $function $function eq "comment"
Truth table33%1/3
A and B
A= defined $functionB= $function eq "comment"
ABresult
0X0
100
111
255 {
256 0 $result[0] = XML::LibXML::Comment->new();
257  
258 0 foreach( $node->childNodes() )
259 {
260 0 $result[0]->appendChild( $_ );
261 }
262 }
263  
264 ## We have a special case for linking nodes.
265 6 ▶ if( ( defined( $function ) ) && ( $function eq "link" ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $function and $function eq "link") 1 5 6
Condition66%2/3
defined $function and $function eq "link"
!ll&&!rl&&r
051
MC/DC50%1/2
defined $function and $function eq "link"
defined $function $function eq "link"
Truth table66%2/3
A and B
A= defined $functionB= $function eq "link"
ABresult
0X0
100
111
266 {
267 1 my $href = $node->getAttribute( "href" );
268  
269 1 $result[0] = tei_xref( { url => $href } );
270  
271 1 foreach( $node->childNodes() )
272 {
273 1 my( @children ) =
274 _convert_html_element_to_tei_element( $user_conversions, $_ );
275
276 ## Loop through each of those child nodes ...
277 1 foreach my $child ( @children )
278 {
279 1 $result[0]->appendChild( $child );
280 }
281 }
282 }
283  
284 ## We have a special case for images as well ...
285 6 ▶ if( ( defined( $function ) ) && ( $function eq "figure" ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $function and $function eq "figure") 1 5 6
Condition66%2/3
defined $function and $function eq "figure"
!ll&&!rl&&r
051
MC/DC50%1/2
defined $function and $function eq "figure"
defined $function $function eq "figure"
Truth table66%2/3
A and B
A= defined $functionB= $function eq "figure"
ABresult
0X0
100
111
286 {
287 1 ▶ my $src = $node->getAttribute( "src" ) || "";
Condition50%1/2
$node->getAttribute('src') || ''
l!l
10
MC/DC0%0/1
$node->getAttribute('src') || ''
$node->getAttribute('src')
Truth table50%1/2
A
A= $node->getAttribute('src')
Aresult
11
00
288 1 ▶ my $alt = $node->getAttribute( "alt" ) || "";
Condition50%1/2
$node->getAttribute('alt') || ''
l!l
10
MC/DC0%0/1
$node->getAttribute('alt') || ''
$node->getAttribute('alt')
Truth table50%1/2
A
A= $node->getAttribute('alt')
Aresult
11
00
289  
290 1 $result[0] = tei_figure( { url => $src } );
291  
292 1 ▶ if( $alt ne "" )
Branch: 1 branch
BranchTrueFalseTotal
if ($alt ne "") 1 0 1
293 {
294 1 $result[0]->appendChild( tei_figDesc( {}, $alt ) );
295 }
296 }
297
298 6 return( @result );
299 }
300  
301 ##==================================================================##
302 ## End of Code ##
303 ##==================================================================##
304 6 1;
305  
306 ##==================================================================##
307 ## Plain Old Documentation (POD) ##
308 ##==================================================================##
309  
310 __END__
↑ Summary Lite/Document.pm »