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