Tag.pm

filter: Statementstmt 86.8% 53 / 61 Branchbran 63.3% 19 / 30 Conditioncond 85.7% 6 / 7 MC/DCmcdc 50.0% 2 / 4 Subroutinesub 100% 9 / 9 Podpod 66.6% 2 / 3 totaltotal 79.8% 91 / 114 CC 22 SCAR 24.3
CC
22
Cov
80%
tag
23.7
ns
9.0
import
3.0
CRAP 11.4
↑ Summary Tag/html5.pm »
1 #! /usr/bin/perl
2 package XML::Tag;
3 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 our $VERSION = '0.4';
6  
7 # ABSTRACT: lib to build builders for xml content
8  
9 sub import {
10 4 ▶ shift;
Subroutine import: called CC=3 SCAR=11.1
11 4 my ( $caller ) = caller;
12 4 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
13 4 my @tags = do {
14 4 ▶ if (@_) {@_}
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 0 4 4
15 4 else { qw< tag ns as_xml > }
16 };
17 4 for (@tags) { *{"$caller\::$_"} = \&{$_} }
18 }
19  
20 sub tag {
21 7 ▶ my ( $tag, $code, $attrs ) = @_;
Subroutine tag: called CC=10 SCAR=31.6
Pod documented
22 7 ▶ my %attr = $attrs ? %$attrs : ();
Branch: 1 branch
BranchTrueFalseTotal
$attrs ? : 0 7 7
23 7 ▶ my @data = $code ? $code->() : ();
Branch: 1 branch
BranchTrueFalseTotal
$code ? : 7 0 7
24  
25 # TODO: what if blessed ?
26 7 ▶ while (my $ref = ref $data[0] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $ref = ref $data[0]) 0 7 7
27 0 ▶ $ref eq 'HASH' or die "$ref cant hold xml attributes";
Branch: 1 branch
BranchTrueFalseTotal
$ref eq "HASH" or die "$ref cant hold xml attributes" 0 0 0
28 0 my $news = shift @data;
29 0 ▶ while ( my ( $k, $v ) = each %$news ) { push @{ $attr{$k} }, $v; }
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$news) 0 0 0
30 }
31  
32 my @content =
33 ( '<'
34 , $tag
35 , ( keys %attr
36 ? ( map {
37 # yeah: i know that this code can lead to stuttering xml like
38 # class="foo foo foo bar"
39 # frankly ? i don't care :-)
40 7 ▶ ' '
Branch: 1 branch
BranchTrueFalseTotal
keys %attr ? : 0 7 7
41 , $_
42 , '='
43 0 ▶ , ( map {ref $_ ? qq{"@$_"} : qq("$_") } $attr{$_} )
Branch: 2 branches
BranchTrueFalseTotal
ref $_ ? : 0 0 0
@data ? : 3 4 7
44 } keys %attr )
45 : ()
46 )
47 , ( @data
48 ? ( '>', @data, '</', $tag , '>')
49 : '/>'
50 )
51 );
52  
53 7 ▶ if (wantarray) { @content }
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 4 3 7
54 3 else { join '', @content }
55  
56 }
57  
58 sub ns {
59 4 ▶ my ( $ns, $pkg ) = do {
Subroutine ns: called CC=9 SCAR=22.0
Pod documented
60 4 my $first = shift;
61 4 ▶ if ( ref $first ) { map {$_||=''} @$first }
Branch: 1 branch
BranchTrueFalseTotal
if (ref $first) { } 3 1 4
Condition100%2/2
$_ ||= ""
l!l
22
MC/DC100%1/1
$_ ||= ""
$_
Truth table100%2/2
A
A= $_
Aresult
11
00
62 1 else { $first, $first } # xml ns = perl package
63 };
64 4 ▶ $pkg ||= caller;
Condition66%2/3
$pkg ||= caller
l!l&&r!l&&!r
220
MC/DC0%0/2
$pkg ||= caller
$pkg caller
Truth table66%2/3
A ||= B
A= $pkgB= caller
ABresult
1X1
011
000
65 4 ▶ $ns and $ns.=':'; # add namespace separator
Branch: 1 branch
BranchTrueFalseTotal
$ns and $ns .= ':' 2 2 4
66  
67 4 ▶ $_//='' for $ns, $pkg;
Condition100%2/2
$_ //= ""
l!l
71
MC/DC100%1/1
$_ //= ""
$_
Truth table100%2/2
A
A= $_
Aresult
11
00
68  
69 4 for my $spec ( @_ ) {
70 10 my ( $sub, $tag ) = do {
71 10 ▶ if ( ref $spec ) { @$spec }
Branch: 1 branch
BranchTrueFalseTotal
if (ref $spec) { } 3 7 10
72 7 else { $spec, $spec }
73 };
74 4 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
75 10 ▶ *{"${pkg}::$sub"} = sub (&) { tag "$ns$tag", @_ }
Subroutine __ANON__: called CC=1 SCAR=0.0
76 10 }
77 }
78  
79 sub as_xml (_);
80 sub as_xml (_) {
81 2 ▶ my $entry = shift;
Subroutine as_xml: called CC=3 SCAR=11.0
Pod undocumented
82 2 my @render;
83 2 ▶ while ( my ($tag,$v) = each %$entry ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$entry) 3 2 5
84 3 ▶ push @render
Branch: 1 branch
BranchTrueFalseTotal
ref $v ? : 1 2 3
85 , "<$tag>"
86 , ( ref $v ? as_xml $v : $v )
87 , "</$tag>"
88 };
89 2 join '', @render;
90 }
91  
92 4 1;
93  
94 =head1 XML::Tag, a simple XML builder
95  
96 Builders are a set of helpers to generate the tag content. I see 3 major gains
97 using this strategy over templating systems:
98  
99 =over 2
100  
101 =item *
102  
103 keep the power of perl in your hands (don't abuse it and respect at least
104 an MVC separation)
105  
106 =item *
107  
108 don't be WYSIWYG. When i write code, i need indentations and line feeds
109 to make things readable. All those extra stuff must disapear in the final
110 result because they are useless and anoying when you manage to control spaces
111 with CSS.
112  
113 =item *
114  
115 stay confident about the quality of generated code: as long as they
116 compiles, the helpers render bug free xml (WARNING: the quality of all PCDATA,
117 attribute values and schemas is *your* job)
118  
119 =back
120  
121 L<see how builders works|http://docs.codehaus.org/display/GROOVY/How+Builders+Work>
122 or see it in action:
123  
124 To render this text on C<STDIN>.
125  
126 <!doctype html><html><head><title lang="fr">my personal homepage</title></head></html>
127  
128 you can use directly the C<tag()> function from XML::Tag
129  
130 use XML::Tag;
131 use Modern::Perl;
132  
133 print '<!doctype html>'
134 , tag html => sub {
135 tag head => sub {
136 tag title => sub { +{lang => 'fr'}, "my personal homepage" }
137 }
138 }
139  
140 you can use the C<ns()> function from XML::Tag to generate the helpers
141  
142 use XML::Tag;
143 use Modern::Perl;
144  
145 BEGIN {
146 ns '' # use the default namespace
147 , qw< html head title >
148 }
149  
150 print '<!doctype html>', html {
151 head {
152 title { +{lang => 'fr'}, "my personal homepage" }
153 }
154 }
155  
156 you can even use a ready to use set of helpers
157  
158 use XML::Tag::html5;
159 print '<!doctype html>', html {
160 head {
161 title { +{lang => 'fr'}, "my personal homepage" }
162 }
163 }
164  
165 =head2 XML::Tag functions
166  
167 =head3 tag
168  
169 =head3 ns
170  
171 =head3 tag $name, $content, $attrs
172  
173 the parameters of tag are
174  
175 =over 2
176  
177 =item *
178  
179 $name: the name of the tag
180  
181 =item *
182  
183 $content:
184  
185 a sub returning th
186  
187 * content sub
188 * a hashref with the list of default attributes for the tag
189  
190 =item *
191  
192 $name ???
193  
194 =back
195  
196 perl -MXML::Tag -E '
197 print "($_)" for tag title => sub { "content" }, +{qw(class test)};
198 '
199  
200 (<)(title)( )(class)(=)("test")(>)(content)(</)(title)(>)
201  
202  
203 tag title => sub { "content" }, +{qw(class test)}
204 tag title => sub { +{qw(class test)}, "content" }
205  
206 use XML::Tag;
207 print for tag title => sub { "content" }, +{qw(class test)};
208  
209  
210 use XML::Tag;
211 print for tag title => sub { "content" }, +{qw(class test)};
212  
213 use XML::Tag;
214 tag title => sub { "content" }, +{qw(class test)}
215 tag title => sub { +{qw(class test)}, "content" }
216  
217  
218 the content sub returns a list, the first elements of the lists are
219  
220 use Modern::Perl;
221 use XML::Tag;
222  
223 sub foo (&) { tag foo => @_, {qw< isa foo >} }
224  
225 print foo{
226 + {qw< class bar id bang >}
227 , {qw< style text-align:center >}
228 , "this is "
229 , "the content"
230 };
231  
232 =head2 how to build tag list
233  
234 extract_elements () {
235 xmlstarlet sel -T -t -m '//xs:element/@name' -v . -n "$@"
236 }
237  
238 schema=http://dublincore.org/schemas/xmls/simpledc20021212.xsd
239 curl -ls "$schema" | extract_elements
240  
↑ Summary Tag/html5.pm »