| 1 | # | ||||||||||||||||||||
| 2 | # This file is part of HTML-Builder | ||||||||||||||||||||
| 3 | # | ||||||||||||||||||||
| 4 | # This software is Copyright (c) 2012 by Chris Weyl. | ||||||||||||||||||||
| 5 | # | ||||||||||||||||||||
| 6 | # This is free software, licensed under: | ||||||||||||||||||||
| 7 | # | ||||||||||||||||||||
| 8 | # The GNU Lesser General Public License, Version 2.1, February 1999 | ||||||||||||||||||||
| 9 | # | ||||||||||||||||||||
| 10 | package HTML::Builder; | ||||||||||||||||||||
| 11 | { | ||||||||||||||||||||
| 12 | 6 | $HTML::Builder::VERSION = '0.008'; | |||||||||||||||||||
| 13 | } | ||||||||||||||||||||
| 14 | |||||||||||||||||||||
| 15 | # ABSTRACT: A declarative approach to HTML generation | ||||||||||||||||||||
| 16 | |||||||||||||||||||||
| 17 | 6 | ▶ | use v5.10; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 18 | |||||||||||||||||||||
| 19 | 6 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 20 | 6 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 21 | |||||||||||||||||||||
| 22 | 6 | ▶ | use Capture::Tiny 0.15 'capture_stdout'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 23 | 6 | ▶ | use HTML::Tiny; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 24 | 6 | ▶ | use Sub::Install; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 25 | 6 | ▶ | use List::MoreUtils 'uniq'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 26 | |||||||||||||||||||||
| 27 | # debugging... | ||||||||||||||||||||
| 28 | #use Smart::Comments; | ||||||||||||||||||||
| 29 | |||||||||||||||||||||
| 30 | 6 | our $IN_TAG; | |||||||||||||||||||
| 31 | |||||||||||||||||||||
| 32 | |||||||||||||||||||||
| 33 | # HTML5 tags from: | ||||||||||||||||||||
| 34 | # http://en.wikipedia.org/wiki/HTML5#Differences_from_HTML.C2.A04.01_and_XHTML.C2.A01.x | ||||||||||||||||||||
| 35 | # 18 Feb 2012 | ||||||||||||||||||||
| 36 | # | ||||||||||||||||||||
| 37 | # Other HTML tags from: | ||||||||||||||||||||
| 38 | # http://www.w3schools.com/tags/default.asp | ||||||||||||||||||||
| 39 | # 19 Feb 2012 | ||||||||||||||||||||
| 40 | |||||||||||||||||||||
| 41 | |||||||||||||||||||||
| 42 | # !--...-- Defines | ||||||||||||||||||||
| 43 | |||||||||||||||||||||
| 44 | 13 | ▶ | sub html5_tags { qw{ | ||||||||||||||||||
|
Subroutine
html5_tags: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 45 | |||||||||||||||||||||
| 46 | article aside audio bdo canvas command datalist details embed figcaption | ||||||||||||||||||||
| 47 | figure footer header hgroup keygen mark meter nav output progress rp rt | ||||||||||||||||||||
| 48 | ruby section source summary time video wbr | ||||||||||||||||||||
| 49 | |||||||||||||||||||||
| 50 | } } | ||||||||||||||||||||
| 51 | |||||||||||||||||||||
| 52 | |||||||||||||||||||||
| 53 | 0 | ▶ | sub html5_minimal_tags { q{ article aside footer header nav } } | ||||||||||||||||||
|
Subroutine
html5_minimal_tags: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 54 | |||||||||||||||||||||
| 55 | # excl: s | ||||||||||||||||||||
| 56 | 15 | ▶ | sub deprecated_tags { qw{ applet basefont center dir font menu strike u xmp } } | ||||||||||||||||||
|
Subroutine
deprecated_tags: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 57 | 0 | ▶ | sub depreciated_tags { deprecated_tags() } | ||||||||||||||||||
|
Subroutine
depreciated_tags: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 58 | |||||||||||||||||||||
| 59 | |||||||||||||||||||||
| 60 | 19 | ▶ | sub conflicting_tags { { | ||||||||||||||||||
|
Subroutine
conflicting_tags: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 61 | 19 | html_sub => 'sub', | |||||||||||||||||||
| 62 | html_map => 'map', | ||||||||||||||||||||
| 63 | html_q => 'q', | ||||||||||||||||||||
| 64 | html_tr => 'tr', | ||||||||||||||||||||
| 65 | } } | ||||||||||||||||||||
| 66 | |||||||||||||||||||||
| 67 | 13 | ▶ | sub html_tags { | ||||||||||||||||||
|
Subroutine
html_tags: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 68 | |||||||||||||||||||||
| 69 | # excl: sub map q tr | ||||||||||||||||||||
| 70 | |||||||||||||||||||||
| 71 | 13 | return qw{ | |||||||||||||||||||
| 72 | |||||||||||||||||||||
| 73 | a abbr acronym address area b | ||||||||||||||||||||
| 74 | base bdo big blockquote body br | ||||||||||||||||||||
| 75 | button caption cite code col colgroup | ||||||||||||||||||||
| 76 | dd del dfn div dl dt | ||||||||||||||||||||
| 77 | em fieldset form frame frameset h1 | ||||||||||||||||||||
| 78 | head hr html i iframe img | ||||||||||||||||||||
| 79 | input ins kbd label legend li | ||||||||||||||||||||
| 80 | link meta noframes noscript object | ||||||||||||||||||||
| 81 | ol optgroup option p param pre | ||||||||||||||||||||
| 82 | samp script select small span | ||||||||||||||||||||
| 83 | strong style sup table | ||||||||||||||||||||
| 84 | tbody td textarea tfoot th thead | ||||||||||||||||||||
| 85 | title tt ul var | ||||||||||||||||||||
| 86 | |||||||||||||||||||||
| 87 | }; | ||||||||||||||||||||
| 88 | } | ||||||||||||||||||||
| 89 | |||||||||||||||||||||
| 90 | |||||||||||||||||||||
| 91 | 0 | ▶ | sub form_tags { qw{ | ||||||||||||||||||
|
Subroutine
form_tags: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 92 | |||||||||||||||||||||
| 93 | form fieldset button input label optgroup option select textarea | ||||||||||||||||||||
| 94 | |||||||||||||||||||||
| 95 | }} | ||||||||||||||||||||
| 96 | |||||||||||||||||||||
| 97 | |||||||||||||||||||||
| 98 | 0 | ▶ | sub table_tags { qw{ table tr td th thead tbody tfoot } } | ||||||||||||||||||
|
Subroutine
table_tags: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 99 | |||||||||||||||||||||
| 100 | |||||||||||||||||||||
| 101 | 2 | ▶ | sub minimal_tags { | ||||||||||||||||||
|
Subroutine
minimal_tags: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 102 | 2 | return ('h1'..'h5', qw{ | |||||||||||||||||||
| 103 | div span p img script br ul ol li style a | ||||||||||||||||||||
| 104 | }); | ||||||||||||||||||||
| 105 | } | ||||||||||||||||||||
| 106 | |||||||||||||||||||||
| 107 | |||||||||||||||||||||
| 108 | 13 | ▶ | sub our_tags { | ||||||||||||||||||
|
Subroutine
our_tags: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 109 | my @tags = ( | ||||||||||||||||||||
| 110 | html5_tags(), | ||||||||||||||||||||
| 111 | html_tags(), | ||||||||||||||||||||
| 112 | deprecated_tags(), | ||||||||||||||||||||
| 113 | 13 | (keys %{ conflicting_tags() }), | |||||||||||||||||||
| 114 | ); | ||||||||||||||||||||
| 115 | 13 | return uniq sort @tags; | |||||||||||||||||||
| 116 | } | ||||||||||||||||||||
| 117 | |||||||||||||||||||||
| 118 | |||||||||||||||||||||
| 119 | 0 | ▶ | sub _attr { die } | ||||||||||||||||||
|
Subroutine
_attr: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 120 | |||||||||||||||||||||
| 121 | 3 | ▶ | sub attr(&) { | ||||||||||||||||||
|
Subroutine
attr: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 122 | 3 | my $code = shift; | |||||||||||||||||||
| 123 | |||||||||||||||||||||
| 124 | ### in attr... | ||||||||||||||||||||
| 125 | 3 | _attr($code->()); | |||||||||||||||||||
| 126 | } | ||||||||||||||||||||
| 127 | |||||||||||||||||||||
| 128 | 534 | ▶ | sub _is_autoload_gen { | ||||||||||||||||||
|
Subroutine
_is_autoload_gen: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 129 | 534 | my ($attr_href) = @_; | |||||||||||||||||||
| 130 | |||||||||||||||||||||
| 131 | 261 | ▶ | return sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.0
| |||||||||||||||||||||
| 132 | 261 | shift; | |||||||||||||||||||
| 133 | |||||||||||||||||||||
| 134 | 261 | my $field = our $AUTOLOAD; | |||||||||||||||||||
| 135 | 261 | $field =~ s/.*:://; | |||||||||||||||||||
| 136 | |||||||||||||||||||||
| 137 | # XXX | ||||||||||||||||||||
| 138 | 261 | $field =~ s/__/:/g; # xml__lang is 'foo' ====> xml:lang="foo" | |||||||||||||||||||
| 139 | 261 | $field =~ s/_/-/g; # http_equiv is 'bar' ====> http-equiv="bar" | |||||||||||||||||||
| 140 | |||||||||||||||||||||
| 141 | # Squash empty values, but not '0' values | ||||||||||||||||||||
| 142 | 261 | ▶ | my $val = join ' ', grep { defined $_ && $_ ne '' } @_; | ||||||||||||||||||
|
Condition33%1/3
defined $_ && $_ ne ''
MC/DC0%0/2
defined $_ && $_ ne ''
defined $_
$_ ne ''
Truth table33%1/3
A && B
A= defined $_B= $_ ne ''
| |||||||||||||||||||||
| 143 | |||||||||||||||||||||
| 144 | #push @$attr_aref, $field => $val; | ||||||||||||||||||||
| 145 | 261 | $attr_href->{$field} = $val; | |||||||||||||||||||
| 146 | |||||||||||||||||||||
| 147 | 261 | return; | |||||||||||||||||||
| 148 | 534 | }; | |||||||||||||||||||
| 149 | } | ||||||||||||||||||||
| 150 | |||||||||||||||||||||
| 151 | 534 | ▶ | sub _attr_gen { | ||||||||||||||||||
|
Subroutine
_attr_gen: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 152 | 534 | my ($attr_href) = @_; | |||||||||||||||||||
| 153 | |||||||||||||||||||||
| 154 | 3 | ▶ | return sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 155 | |||||||||||||||||||||
| 156 | 3 | my %attrs = @_; | |||||||||||||||||||
| 157 | ### %attrs | ||||||||||||||||||||
| 158 | 3 | @{$attr_href}{keys %attrs} = (values %attrs); | |||||||||||||||||||
| 159 | |||||||||||||||||||||
| 160 | 3 | return; | |||||||||||||||||||
| 161 | 534 | }; | |||||||||||||||||||
| 162 | } | ||||||||||||||||||||
| 163 | |||||||||||||||||||||
| 164 | 534 | ▶ | sub tag($&) { | ||||||||||||||||||
|
Subroutine
tag: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 165 | 534 | my ($tag, $inner_coderef) = @_; | |||||||||||||||||||
| 166 | |||||||||||||||||||||
| 167 | 534 | state $h = HTML::Tiny->new; | |||||||||||||||||||
| 168 | |||||||||||||||||||||
| 169 | ### @_ | ||||||||||||||||||||
| 170 | 534 | my %attrs = (); | |||||||||||||||||||
| 171 | |||||||||||||||||||||
| 172 | # This is almost completely stolen from Template::Declare::Tags, and | ||||||||||||||||||||
| 173 | # completely terrifying in that it confirms my dark suspicions on how | ||||||||||||||||||||
| 174 | # it was achieved over there. | ||||||||||||||||||||
| 175 | 6 | ▶ | no warnings 'once', 'redefine'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 176 | 534 | local *gets::AUTOLOAD = _is_autoload_gen(\%attrs); | |||||||||||||||||||
| 177 | 534 | local *_attr = _attr_gen(\%attrs); | |||||||||||||||||||
| 178 | 534 | my $inner = q{}; | |||||||||||||||||||
| 179 | 534 | ▶ | my $stdout = capture_stdout { local $IN_TAG = 1; $inner .= $inner_coderef->() || q{} }; | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
Condition100%2/2
&$inner_coderef() || ''
MC/DC100%1/1
&$inner_coderef() || ''
&$inner_coderef()
Truth table100%2/2
A
A= &$inner_coderef()
| |||||||||||||||||||||
| 180 | |||||||||||||||||||||
| 181 | 534 | my $return = $h->tag($tag, \%attrs, "$stdout$inner"); | |||||||||||||||||||
| 182 | |||||||||||||||||||||
| 183 | ### $return | ||||||||||||||||||||
| 184 | 534 | ▶ | if ($IN_TAG) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 185 | 4 | print $return; | |||||||||||||||||||
| 186 | 4 | return q{}; | |||||||||||||||||||
| 187 | } | ||||||||||||||||||||
| 188 | else { | ||||||||||||||||||||
| 189 | 530 | return $return; | |||||||||||||||||||
| 190 | } | ||||||||||||||||||||
| 191 | } | ||||||||||||||||||||
| 192 | |||||||||||||||||||||
| 193 | 6 | ▶ | use Sub::Exporter -setup => { | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.9
| |||||||||||||||||||||
| 194 | |||||||||||||||||||||
| 195 | exports => [ our_tags, 'attr', 'tag' ], | ||||||||||||||||||||
| 196 | groups => { | ||||||||||||||||||||
| 197 | |||||||||||||||||||||
| 198 | default => ':minimal', | ||||||||||||||||||||
| 199 | |||||||||||||||||||||
| 200 | 1 | ▶ | minimal => sub { _generate_group([ minimal_tags ], @_) }, | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 201 | 0 | ▶ | html5 => sub { _generate_group([ html5_tags ], @_) }, | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 202 | 0 | ▶ | html5_minimal => sub { _generate_group([ html5_minimal_tags ], @_) }, | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 203 | 1 | ▶ | deprecated => sub { _generate_group([ deprecated_tags ], @_) }, | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 204 | 1 | ▶ | depreciated => sub { _generate_group([ deprecated_tags ], @_) }, | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 205 | 0 | ▶ | table => sub { _generate_group([ table_tags ], @_) }, | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 206 | 0 | ▶ | form => sub { _generate_group([ form_tags ], @_) }, | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 207 | 0 | ▶ | header => sub { _generate_group([qw{ header hgroup } ], @_) }, | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 208 | }, | ||||||||||||||||||||
| 209 | 6 | }; | |||||||||||||||||||
| 210 | |||||||||||||||||||||
| 211 | 3 | ▶ | sub _generate_group { | ||||||||||||||||||
|
Subroutine
_generate_group: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 212 | 3 | my ($tags, $class, $group, $arg) = @_; | |||||||||||||||||||
| 213 | |||||||||||||||||||||
| 214 | return { | ||||||||||||||||||||
| 215 | attr => \&attr, | ||||||||||||||||||||
| 216 | 3 | ▶ | map { my $tag = $_; $tag => sub(&) { unshift @_, $tag; goto \&tag } } | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 217 | @$tags | ||||||||||||||||||||
| 218 | }; | ||||||||||||||||||||
| 219 | } | ||||||||||||||||||||
| 220 | |||||||||||||||||||||
| 221 | { | ||||||||||||||||||||
| 222 | 6 | ▶ | my $_install = sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.0
| |||||||||||||||||||||
| 223 | 702 | my ($subname, $tag) = @_; | |||||||||||||||||||
| 224 | 702 | ▶ | $tag ||= $subname; | ||||||||||||||||||
|
Condition66%2/3
$tag ||= $subname
MC/DC0%0/2
$tag ||= $subname
$tag
$subname
Truth table66%2/3
A ||= B
A= $tagB= $subname
| |||||||||||||||||||||
| 225 | Sub::Install::install_sub({ | ||||||||||||||||||||
| 226 | 462 | ▶ | code => sub(&) { unshift @_, $tag; goto \&tag }, | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 227 | 702 | as => $tag, | |||||||||||||||||||
| 228 | }); | ||||||||||||||||||||
| 229 | 6 | }; | |||||||||||||||||||
| 230 | |||||||||||||||||||||
| 231 | 6 | my $conflict = conflicting_tags; | |||||||||||||||||||
| 232 | 6 | $_install->($_) for our_tags; | |||||||||||||||||||
| 233 | 6 | $_install->(@$_) for map { [ $_ => $conflict->{$_} ] } keys %$conflict; | |||||||||||||||||||
| 234 | } | ||||||||||||||||||||
| 235 | |||||||||||||||||||||
| 236 | 6 | !!42; | |||||||||||||||||||
| 237 | |||||||||||||||||||||
| 238 | __END__ | ||||||||||||||||||||