Caribou/Tags/HTML/Extended.pm

filter: Statementstmt 83.8% 52 / 62 Branchbran 50.0% 4 / 8 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 80.7% 21 / 26 Podpod 100% 11 / 11 totaltotal 79.4% 89 / 112 CC 10 SCAR 6.1
CC
10
Cov
78%
doctype
3.3
__ANON__
3.1
__ANON__
2.5
CRAP 1.8
« Caribou.pm ↑ Summary Caribou/Utils.pm »
1 package Template::Caribou::Tags::HTML::Extended;
2 2 our $AUTHORITY = 'cpan:YANICK';
3 # ABSTRACT: custom HTML tags optimized for DWIMery
4 2 $Template::Caribou::Tags::HTML::Extended::VERSION = '1.1.0';
5  
6 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 2 ▶use Template::Caribou::Tags qw/ render_tag attr /;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use Class::Load qw/ load_class /;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 2 ▶use experimental 'signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 2 ▶use parent 'Exporter::Tiny';
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 2 our @EXPORT = qw/
19 css anchor image markdown javascript javascript_include submit
20 less css_include doctype
21 favicon
22  
23 /;
24  
25  
26 1 ▶sub doctype($type="html 5") {
Subroutine doctype: called CC=3 SCAR=12.0
Pod documented
27 1 ▶ if ( $type =~ /^html\s?5/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type =~ /^html\s?5/) 1 0 1
28 1 return Template::Caribou::Tags::print_raw( "<!DOCTYPE html>\n" );
29 }
30  
31 0 croak "type '$type' not supported";
32 }
33  
34  
35 ▶sub favicon($) {
Subroutine favicon: called CC=1 SCAR=0.0
Pod documented
36 1 my $url = shift;
37  
38 render_tag( 'link', sub {
39 1 ▶ attr rel => 'shortcut icon',
Subroutine __ANON__: called CC=1 SCAR=0.0
40 href => $url
41 1 } );
42 }
43  
44  
45 1 ▶sub submit($value=undef, %attr) {
Subroutine submit: called CC=2 SCAR=7.0
Pod documented
46  
47 render_tag( 'input', '', sub {
48 1 ▶ $_{type} = 'submit';
Subroutine __ANON__: called CC=3 SCAR=11.2
49 1 ▶ $_{value} = $value if $value;
Branch: 1 branch
BranchTrueFalseTotal
if $value 1 0 1
50 1 $_{$_} = $attr{$_} for keys %attr;
51 1 });
52 }
53  
54  
55 ▶sub less($text) {
Subroutine less: not called CC=1 SCAR=6.9
Pod documented
56 0 my $css = join '', load_class('CSS::LESSp')->parse($text);
57  
58 0 css($css);
59 }
60  
61  
62  
63 ▶sub javascript($script) {
Subroutine javascript: not called CC=1 SCAR=6.9
Pod documented
64 render_tag( 'script', sub {
65 0 ▶ attr type => 'text/javascript';
Subroutine __ANON__: not called CC=1 SCAR=6.9
66 0 print ::RAW $script;
67 0 });
68 }
69  
70  
71 ▶sub javascript_include($) {
Subroutine javascript_include: not called CC=1 SCAR=6.9
Pod documented
72 0 my $url = shift;
73  
74 render_tag( 'script', sub {
75 0 ▶ attr type => 'text/javascript',
Subroutine __ANON__: not called CC=1 SCAR=6.9
76 src => $url;
77 0 print ::RAW ' '; # to prevent collapsing the tag
78 0 });
79 }
80  
81  
82 ▶sub css_include( $url, %args ) {
Subroutine css_include: called CC=1 SCAR=0.0
Pod documented
83 render_tag( 'link', sub {
84 2 ▶ attr rel => 'stylesheet',
Subroutine __ANON__: called CC=1 SCAR=0.0
85 href => $url,
86 %args
87 ;
88 2 });
89 }
90  
91  
92 ▶sub css($) {
Subroutine css: called CC=1 SCAR=0.0
Pod documented
93 1 my $css = shift;
94 render_tag( 'style', sub {
95 1 ▶ attr type => 'text/css';
Subroutine __ANON__: called CC=1 SCAR=0.0
96 1 $css;
97 1 });
98 };
99  
100  
101 ▶sub anchor($href,$inner) {
Subroutine anchor: called CC=1 SCAR=0.6
Pod documented
102 render_tag( 'a', $inner, sub {
103 2 ▶ $_{href} ||= $href;
Subroutine __ANON__: called CC=2 SCAR=9.2
Condition33%1/3
$_{"href"} ||= $href
l!l&&r!l&&!r
020
MC/DC0%0/2
$_{"href"} ||= $href
$_{"href"} $href
Truth table33%1/3
A ||= B
A= $_{"href"}B= $href
ABresult
1X1
011
000
104 2 });
105 }
106  
107  
108 ▶sub image($src,%attr) {
Subroutine image: called CC=2 SCAR=7.0
Pod documented
109  
110 1 ▶ croak "src required" unless $src;
Branch: 1 branch
BranchTrueFalseTotal
unless $src 0 1 1
111  
112 1 $attr{src} = $src;
113  
114 render_tag( 'img', '', sub {
115 1 ▶ $_{$_} = $attr{$_} for keys %attr;
Subroutine __ANON__: called CC=2 SCAR=6.9
116 1 } );
117 }
118  
119  
120 ▶sub markdown($){
Subroutine markdown: called CC=2 SCAR=7.0
Pod documented
121 1 require Text::MultiMarkdown;
122  
123 1 ▶ return unless length $_[0];
Branch: 1 branch
BranchTrueFalseTotal
unless length $_[0] 0 1 1
124  
125 1 my $value = Text::MultiMarkdown::markdown(shift);
126  
127 1 print ::RAW $value;
128 }
129  
130 2 1;
131  
132 __END__
133  
134 =pod
135  
136 =encoding UTF-8
137  
138 =head1 NAME
139  
140 Template::Caribou::Tags::HTML::Extended - custom HTML tags optimized for DWIMery
141  
142 =head1 VERSION
143  
144 version 1.1.0
145  
146 =head1 SYNOPSIS
147  
148 package MyTemplate;
149  
150 use Moose;
151  
152 use Template::Caribou::Tags::HTML;
153 use Template::Caribou::Tags::HTML::Extended;
154  
155 with 'Template::Caribou';
156  
157 template 'page' => sub {
158 html {
159 head {
160 css q{
161 color: magenta;
162 };
163 };
164 body {
165 markdown q{Ain't Markdown **grand**?};
166
167 anchor "http://foo.com" => sub {
168 image 'http://foo.com/bar.jpg', alt => 'Foo logo';
169 };
170 }
171  
172 }
173 };
174  
175 =head1 DESCRIPTION
176  
177 Whereas L<Template::Caribou::Tags::HTML> offers straight function equivalents to their
178 HTML tags, this module provides a more DWIM interface, and shortcut for often used patterns.
179  
180 =head2 doctype $type
181  
182 doctype 'html5';
183 # <!DOCTYPE html>
184  
185 Prints the doctype declaration for the given type.
186  
187 For the moment, only I<html 5> (or I<html5>) is supported as a type.
188  
189 =head2 favicon $url
190  
191 Generates a favicon link tag.
192  
193 favicon 'my_icon.png';
194 # <link rel="shortcut icon" href="my_icon.png" />
195  
196 =head2 submit $value, %attr
197  
198 submit 'foo';
199 # <input type="submit" value="foo" />
200  
201 Shortcut for
202  
203 input { attr type => submit, value => $value, %attr; }
204  
205 =head2 less $script
206  
207 Compiles the LESS script into CSS. Requires L<CSS::LESSp>.
208  
209 =head2 javascript $script
210  
211 javascript q{ console.log( "Hello there!" ) };
212 # <script type="text/javascript">console.log( "Hello there!" )</script>
213  
214 Shortcut for
215  
216 <script type="text/javascript>$script</script>
217  
218 =head2 javascript_include $url
219  
220 Shortcut for
221  
222 <script type="text/javascript" src="http://..."> </script>
223  
224 =head2 css_include $url, %args
225  
226 css_include 'public/bootstrap/css/bootstrap.min.css', media => 'screen';
227 # <link href="public/bootstrap/css/bootstrap.min.css" rel="stylesheet"
228 # media="screen" />
229  
230 =head2 css $text
231  
232 Wraps the I<$text> in a style element.
233  
234 <style type="text/css">$text</style>
235  
236 =head2 anchor $url, $inner
237  
238 Shortcut for <a>. I<$inner> can be either a string, or a subref.
239  
240 anchor 'http://foo.com' => 'linkie';
241  
242 is equivalent to
243  
244 a {
245 attr href => 'http://foo.com';
246 'linkie';
247 }
248  
249 =head2 image $src, %attr
250  
251 image 'kitten.jpg';
252 # <img src="kitten.jpg" />
253  
254 Shortcut for <img>.
255  
256 =head2 markdown $text
257  
258 Converts the markdown $text into its html equivalent.
259  
260 Uses L<Text::MultiMarkdown>.
261  
262 =head1 AUTHOR
263  
264 Yanick Champoux <yanick@cpan.org>
265  
266 =head1 COPYRIGHT AND LICENSE
267  
268 This software is copyright (c) 2017 by Yanick Champoux.
269  
270 This is free software; you can redistribute it and/or modify it under
271 the same terms as the Perl 5 programming language system itself.
272  
273 =cut
« Caribou.pm ↑ Summary Caribou/Utils.pm »