File Coverage

blib/lib/Template/Caribou/Tags/HTML/Extended.pm
Criterion Covered Total %
statement 72 90 80.0
branch 4 8 50.0
condition 1 3 33.3
subroutine 21 26 80.7
pod 11 11 100.0
total 109 138 78.9


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