| 1 | package DSL::HTML::Rendering; | ||||||||||
| 2 | 8 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | 8 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | |||||||||||
| 5 | 8 | ▶ | use HTML::Element; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | |||||||||||
| 7 | 8 | ▶ | use Scalar::Util qw/blessed/; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | |||||||||||
| 9 | 8 | ▶ | use Carp qw/croak confess/; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 8 | our @CARP_NOT = qw/DSL::HTML DSL::HTML::Template/; | |||||||||
| 11 | |||||||||||
| 12 | 14 | ▶ | sub template { shift->{template} } | ||||||||
|
Subroutine
template: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 13 | 8 | ▶ | sub head { shift->{head} } | ||||||||
|
Subroutine
head: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 14 | 6 | ▶ | sub body { shift->{body} } | ||||||||
|
Subroutine
body: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 15 | 4 | ▶ | sub root { shift->{root} } | ||||||||
|
Subroutine
root: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 16 | 143 | ▶ | sub tag_stack { shift->{tag_stack} } | ||||||||
|
Subroutine
tag_stack: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 17 | 8 | ▶ | sub css_seen { shift->{css_seen} } | ||||||||
|
Subroutine
css_seen: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 18 | 10 | ▶ | sub js_seen { shift->{js_seen} } | ||||||||
|
Subroutine
js_seen: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 19 | 12 | ▶ | sub css_ref { shift->{css_ref} } | ||||||||
|
Subroutine
css_ref: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 20 | 14 | ▶ | sub js_ref { shift->{js_ref} } | ||||||||
|
Subroutine
js_ref: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 21 | |||||||||||
| 22 | 8 | my @STACK; | |||||||||
| 23 | 155 | ▶ | sub current { | ||||||||
|
Subroutine
current: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 24 | 155 | return $STACK[-1]; | |||||||||
| 25 | } | ||||||||||
| 26 | |||||||||||
| 27 | 5 | ▶ | sub new { | ||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 28 | 5 | my $class = shift; | |||||||||
| 29 | 5 | my ( $template ) = @_; | |||||||||
| 30 | |||||||||||
| 31 | 5 | my $html = HTML::Element->new('html'); | |||||||||
| 32 | 5 | my $body = HTML::Element->new('body'); | |||||||||
| 33 | 5 | my $head = HTML::Element->new('head'); | |||||||||
| 34 | |||||||||||
| 35 | 5 | return bless { | |||||||||
| 36 | template => $template, | ||||||||||
| 37 | tag_stack => [$html, $body], | ||||||||||
| 38 | head => $head, | ||||||||||
| 39 | body => $body, | ||||||||||
| 40 | root => $html, | ||||||||||
| 41 | css_ref => [], | ||||||||||
| 42 | js_ref => [], | ||||||||||
| 43 | css_seen => {}, | ||||||||||
| 44 | js_seen => {}, | ||||||||||
| 45 | }, $class; | ||||||||||
| 46 | } | ||||||||||
| 47 | |||||||||||
| 48 | 10 | ▶ | sub args { | ||||||||
|
Subroutine
args: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||
| 49 | 10 | my $self = shift; | |||||||||
| 50 | 10 | ▶ | $self->{args} = [@_] if @_; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 51 | 10 | ▶ | return unless $self->{args}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 52 | 9 | return @{$self->{args}}; | |||||||||
| 53 | } | ||||||||||
| 54 | |||||||||||
| 55 | 4 | ▶ | sub compile { | ||||||||
|
Subroutine
compile: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 56 | 4 | my $self = shift; | |||||||||
| 57 | 4 | my (@args) = @_; | |||||||||
| 58 | |||||||||||
| 59 | 4 | $self->build(@args); | |||||||||
| 60 | 4 | return $self->as_html; | |||||||||
| 61 | } | ||||||||||
| 62 | |||||||||||
| 63 | 6 | ▶ | sub include { | ||||||||
|
Subroutine
include: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 64 | 6 | my $self = shift; | |||||||||
| 65 | 6 | my ($tmp, @args) = @_; | |||||||||
| 66 | |||||||||||
| 67 | 6 | my $sub_render = bless { | |||||||||
| 68 | %$self, | ||||||||||
| 69 | template => $tmp, | ||||||||||
| 70 | }, blessed($self); | ||||||||||
| 71 | |||||||||||
| 72 | 6 | $sub_render->build(@args); | |||||||||
| 73 | |||||||||||
| 74 | 6 | return; | |||||||||
| 75 | } | ||||||||||
| 76 | |||||||||||
| 77 | 10 | ▶ | sub build { | ||||||||
|
Subroutine
build: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 78 | 10 | my $self = shift; | |||||||||
| 79 | 10 | my (@args) = @_; | |||||||||
| 80 | |||||||||||
| 81 | 10 | $self->args(@args); | |||||||||
| 82 | |||||||||||
| 83 | 10 | push @STACK => $self; | |||||||||
| 84 | 10 | my $success = eval { | |||||||||
| 85 | 10 | $self->template->block->($self->peek_tag, @args); | |||||||||
| 86 | 10 | 1; | |||||||||
| 87 | }; | ||||||||||
| 88 | 10 | my $error = $@; | |||||||||
| 89 | 10 | pop @STACK; | |||||||||
| 90 | 10 | ▶ | die $error unless $success; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 91 | } | ||||||||||
| 92 | |||||||||||
| 93 | 4 | ▶ | sub as_html { | ||||||||
|
Subroutine
as_html: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 94 | 4 | my $self = shift; | |||||||||
| 95 | |||||||||||
| 96 | 4 | my $head = $self->head; | |||||||||
| 97 | 4 | my $body = $self->body; | |||||||||
| 98 | 4 | my $html = $self->root; | |||||||||
| 99 | |||||||||||
| 100 | 4 | for my $css ($self->css_list) { | |||||||||
| 101 | 4 | my $tag = HTML::Element->new( | |||||||||
| 102 | link => ( | ||||||||||
| 103 | rel => 'stylesheet', | ||||||||||
| 104 | type => 'text/css', | ||||||||||
| 105 | href => $css, | ||||||||||
| 106 | ) | ||||||||||
| 107 | ); | ||||||||||
| 108 | 4 | $head->push_content($tag); | |||||||||
| 109 | } | ||||||||||
| 110 | |||||||||||
| 111 | $html->push_content( | ||||||||||
| 112 | $head, | ||||||||||
| 113 | $body, | ||||||||||
| 114 | 4 | map { HTML::Element->new( script => ( src => $_ )) } $self->js_list, | |||||||||
| 115 | ); | ||||||||||
| 116 | |||||||||||
| 117 | 4 | return $html->as_HTML(undef, $self->template->indent); | |||||||||
| 118 | } | ||||||||||
| 119 | |||||||||||
| 120 | 37 | ▶ | sub insert { | ||||||||
|
Subroutine
insert: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 121 | 37 | my $self = shift; | |||||||||
| 122 | 37 | $self->peek_tag->push_content(@_); | |||||||||
| 123 | } | ||||||||||
| 124 | |||||||||||
| 125 | 43 | ▶ | sub push_tag { | ||||||||
|
Subroutine
push_tag: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 126 | 43 | my $self = shift; | |||||||||
| 127 | 43 | my ($tag) = @_; | |||||||||
| 128 | 43 | push @{$self->tag_stack} => $tag; | |||||||||
| 129 | } | ||||||||||
| 130 | |||||||||||
| 131 | 43 | ▶ | sub pop_tag { | ||||||||
|
Subroutine
pop_tag: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||
| 132 | 43 | my $self = shift; | |||||||||
| 133 | 43 | my ($want) = @_; | |||||||||
| 134 | 43 | my $got = pop @{$self->tag_stack}; | |||||||||
| 135 | 43 | ▶ | confess "Corrupt stack detected! popped the wrong tag!" | ||||||||
|
Branch: 1 branch
| |||||||||||
| 136 | unless $got == $want; | ||||||||||
| 137 | } | ||||||||||
| 138 | |||||||||||
| 139 | 57 | ▶ | sub peek_tag { | ||||||||
|
Subroutine
peek_tag: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 140 | 57 | my $self = shift; | |||||||||
| 141 | 57 | return $self->tag_stack->[-1]; | |||||||||
| 142 | } | ||||||||||
| 143 | |||||||||||
| 144 | 4 | ▶ | sub js_list { | ||||||||
|
Subroutine
js_list: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 145 | 4 | my $self = shift; | |||||||||
| 146 | 4 | return @{ $self->js_ref }; | |||||||||
| 147 | } | ||||||||||
| 148 | |||||||||||
| 149 | 4 | ▶ | sub css_list { | ||||||||
|
Subroutine
css_list: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 150 | 4 | my $self = shift; | |||||||||
| 151 | 4 | return @{ $self->css_ref }; | |||||||||
| 152 | } | ||||||||||
| 153 | |||||||||||
| 154 | 8 | ▶ | sub add_css { | ||||||||
|
Subroutine
add_css: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||
| 155 | 8 | my $self = shift; | |||||||||
| 156 | 8 | my $seen = $self->css_seen; | |||||||||
| 157 | 8 | my $ref = $self->css_ref; | |||||||||
| 158 | 8 | for my $file ( @_ ) { | |||||||||
| 159 | 8 | ▶ | next if $seen->{$file}++; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 160 | 4 | push @$ref => $file; | |||||||||
| 161 | } | ||||||||||
| 162 | } | ||||||||||
| 163 | |||||||||||
| 164 | 10 | ▶ | sub add_js { | ||||||||
|
Subroutine
add_js: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||
| 165 | 10 | my $self = shift; | |||||||||
| 166 | 10 | my $seen = $self->js_seen; | |||||||||
| 167 | 10 | my $ref = $self->js_ref; | |||||||||
| 168 | 10 | for my $file ( @_ ) { | |||||||||
| 169 | 10 | ▶ | next if $seen->{$file}++; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 170 | 6 | push @$ref => $file; | |||||||||
| 171 | } | ||||||||||
| 172 | } | ||||||||||
| 173 | |||||||||||
| 174 | 8 | 1; | |||||||||
| 175 | |||||||||||
| 176 | __END__ | ||||||||||