File Coverage

blib/lib/Template/Caribou/Tags/HTML/Extended.pm
Criterion Covered Total %
statement 77 98 78.5
branch 19 50 38.0
condition 2 5 40.0
subroutine 21 26 80.7
pod 11 11 100.0
total 130 190 68.4


line stmt bran cond sub pod time code
1             package Template::Caribou::Tags::HTML::Extended;
2             our $AUTHORITY = 'cpan:YANICK';
3             # ABSTRACT: custom HTML tags optimized for DWIMery
4             $Template::Caribou::Tags::HTML::Extended::VERSION = '1.2.1';
5              
6 1     1   15784 use strict;
  1         2  
  1         25  
7 1     1   3 use warnings;
  1         2  
  1         26  
8              
9 1     1   4 use Carp;
  1         1  
  1         68  
10              
11 1     1   340 use Template::Caribou::Tags qw/ render_tag attr /;
  1         3  
  1         9  
12 1     1   243 use Class::Load qw/ load_class /;
  1         1  
  1         38  
13              
14 1     1   3 use experimental 'signatures';
  1         2  
  1         5  
15              
16 1     1   102 use parent 'Exporter::Tiny';
  1         2  
  1         3  
17              
18             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 50   1 1 4466 sub doctype($type="html 5") {
  1 50       4  
  1         2  
27 1 50       8 if ( $type =~ /^html\s?5/ ) {
28 1         33 return Template::Caribou::Tags::print_raw( "<!DOCTYPE html>\n" );
29             }
30              
31 0         0 croak "type '$type' not supported";
32             }
33              
34              
35 1 50   1 1 4 sub favicon($) {
  1 50       3  
  1         1  
36 1         1 my $url = shift;
37              
38             render_tag( 'link', sub {
39 1     1   5 attr rel => 'shortcut icon',
40             href => $url
41 1         9 } );
42             }
43              
44              
45 1 50 50 1 1 9 sub submit($value=undef, %attr) {
  1         2  
  1         5  
  1         1  
46              
47             render_tag( 'input', '', sub {
48 1     1   2 $_{type} = 'submit';
49 1 50       3 $_{value} = $value if $value;
50 1         4 $_{$_} = $attr{$_} for keys %attr;
51 1         5 });
52             }
53              
54              
55 0 0   0 1 0 sub less($text) {
  0 0       0  
  0         0  
  0         0  
56 0         0 my $css = join '', load_class('CSS::LESSp')->parse($text);
57              
58 0         0 css($css);
59             }
60              
61              
62              
63 0 0   0 1 0 sub javascript($script) {
  0 0       0  
  0         0  
  0         0  
64             render_tag( 'script', sub {
65 0     0   0 attr type => 'text/javascript';
66 0         0 print ::RAW $script;
67 0         0 });
68             }
69              
70              
71 0 0   0 1 0 sub javascript_include($) {
  0 0       0  
  0         0  
72 0         0 my $url = shift;
73              
74             render_tag( 'script', sub {
75 0     0   0 attr type => 'text/javascript',
76             src => $url;
77 0         0 print ::RAW ' '; # to prevent collapsing the tag
78 0         0 });
79             }
80              
81              
82 2 50   2 1 13 sub css_include( $url, %args ) {
  2 50       6  
  2         3  
  2         5  
  2         2  
83             render_tag( 'link', sub {
84 2     2   8 attr rel => 'stylesheet',
85             href => $url,
86             %args
87             ;
88 2         11 });
89             }
90              
91              
92 1 50   1 1 6 sub css($) {
  1 50       4  
  1         1  
93 1         2 my $css = shift;
94             render_tag( 'style', sub {
95 1     1   3 attr type => 'text/css';
96 1         2 $css;
97 1         4 });
98             };
99              
100              
101 3 50   3 1 16 sub anchor($href,$inner) {
  3 50       5  
  3         4  
  3         4  
  3         1  
102             render_tag( 'a', $inner, sub {
103 3   33 3   36 $_{href} ||= $href;
104 3         13 });
105             }
106              
107              
108 1 50   1 1 8 sub image($src,%attr) {
  1 50       3  
  1         2  
  1         3  
  1         1  
109              
110 1 50       2 croak "src required" unless $src;
111              
112 1         2 $attr{src} = $src;
113              
114             render_tag( 'img', '', sub {
115 1     1   5 $_{$_} = $attr{$_} for keys %attr;
116 1         5 } );
117             }
118              
119              
120 1 50   1 1 6 sub markdown($){
  1 50       3  
  1         1  
121 1         553 require Text::MultiMarkdown;
122              
123 1 50       31542 return unless length $_[0];
124              
125 1         4 my $value = Text::MultiMarkdown::markdown(shift);
126              
127 1         1943 print ::RAW $value;
128             }
129              
130             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.2.1
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