line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Rest::HtmlVis; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
12583
|
use 5.008_005; |
|
1
|
|
|
|
|
2
|
|
4
|
1
|
|
|
1
|
|
2
|
use strict; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
17
|
|
5
|
1
|
|
|
1
|
|
3
|
use warnings FATAL => 'all'; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
532
|
|
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
our $VERSION = '0.13'; # Set automatically by milla |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
my $based = { |
10
|
|
|
|
|
|
|
'default.base' => 'Rest::HtmlVis::Base', |
11
|
|
|
|
|
|
|
'default.content' => 'Rest::HtmlVis::Content', |
12
|
|
|
|
|
|
|
'default.footer' => 'Rest::HtmlVis::Footer', |
13
|
|
|
|
|
|
|
}; |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
sub new { |
16
|
0
|
|
|
0
|
1
|
|
my ($class, $params) = @_; |
17
|
|
|
|
|
|
|
|
18
|
0
|
|
|
|
|
|
my $htmlVis; |
19
|
|
|
|
|
|
|
|
20
|
0
|
|
|
|
|
|
my $self = bless {}, $class; |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
### Set uri path |
23
|
0
|
|
|
|
|
|
$self->{baseurl} = "/static"; |
24
|
|
|
|
|
|
|
# Don't delete key, because of original hash |
25
|
0
|
0
|
|
|
|
|
$self->{baseurl} = $params->{'default.baseurl'} if exists $params->{'default.baseurl'}; |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
### Add htmlvis |
28
|
0
|
|
|
|
|
|
foreach my $key (sort keys %$params){ |
29
|
0
|
0
|
|
|
|
|
next if $key eq 'default.baseurl'; |
30
|
0
|
|
|
|
|
|
$self->loadVisObject($key, $params->{$key}); |
31
|
|
|
|
|
|
|
} |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
### Set params |
34
|
0
|
|
|
|
|
|
foreach my $key (sort keys %$based) { |
35
|
0
|
0
|
|
|
|
|
$self->loadVisObject($key, $based->{$key}) unless exists $params->{$key}; |
36
|
|
|
|
|
|
|
} |
37
|
|
|
|
|
|
|
|
38
|
0
|
|
|
|
|
|
return $self; |
39
|
|
|
|
|
|
|
} |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
sub baseurl { |
42
|
0
|
|
|
0
|
0
|
|
my ($self) = shift; |
43
|
0
|
|
|
|
|
|
return $self->{baseurl}; |
44
|
|
|
|
|
|
|
} |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
sub loadVisObject { |
47
|
0
|
|
|
0
|
0
|
|
my ($self, $key, $class) = @_; |
48
|
|
|
|
|
|
|
|
49
|
0
|
|
|
|
|
|
my ($rtrn, $err) = _try_load($class); |
50
|
0
|
0
|
|
|
|
|
if ($rtrn){ |
51
|
0
|
|
|
|
|
|
my $vis = $class->new($self->baseurl); |
52
|
0
|
|
|
|
|
|
my $order = $vis->getOrder; |
53
|
0
|
0
|
|
|
|
|
push(@{$self->{htmlVis}{$order}}, { |
|
0
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
key => $key, |
55
|
|
|
|
|
|
|
object => $vis |
56
|
|
|
|
|
|
|
}) if $vis->isa('Rest::HtmlVis::Key'); |
57
|
|
|
|
|
|
|
}else{ |
58
|
0
|
|
|
|
|
|
print STDERR "ERROR: to load vis class: ".$err."\n"; |
59
|
|
|
|
|
|
|
} |
60
|
|
|
|
|
|
|
} |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
sub html { |
63
|
0
|
|
|
0
|
1
|
|
my ($self, $struct, $env, $header) = @_; |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
### manage keys |
66
|
0
|
|
|
|
|
|
my $head_parts = ''; |
67
|
0
|
|
|
|
|
|
my $onload_parts = ''; |
68
|
0
|
|
|
|
|
|
my $html_parts = ''; |
69
|
0
|
|
|
|
|
|
my $footer_parts = ''; |
70
|
|
|
|
|
|
|
|
71
|
0
|
|
|
|
|
|
my $rowBlocks = 0; # count number of blocks in row |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
### Add blocks |
74
|
0
|
|
|
|
|
|
foreach my $order (sort keys %{$self->{htmlVis}}) { |
|
0
|
|
|
|
|
|
|
75
|
0
|
|
|
|
|
|
foreach my $obj (@{$self->{htmlVis}{$order}}) { |
|
0
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
|
77
|
0
|
|
|
|
|
|
my $vis = $obj->{object}; |
78
|
0
|
0
|
|
|
|
|
next unless $vis->setStruct($obj->{key}, $struct, $env); |
79
|
0
|
|
|
|
|
|
$vis->setHeader($header); |
80
|
|
|
|
|
|
|
|
81
|
0
|
|
|
|
|
|
my $head = $vis->head($self->{local}); |
82
|
0
|
0
|
|
|
|
|
$head_parts .= $head if $head; |
83
|
|
|
|
|
|
|
|
84
|
0
|
|
|
|
|
|
my $onload = $vis->onload(); |
85
|
0
|
0
|
|
|
|
|
$onload_parts .= $onload if $onload; |
86
|
|
|
|
|
|
|
|
87
|
0
|
|
|
|
|
|
my $html = $vis->html(); |
88
|
0
|
0
|
|
|
|
|
if ($html){ |
89
|
0
|
|
|
|
|
|
$rowBlocks += $vis->blocks(); |
90
|
0
|
0
|
0
|
|
|
|
my $newRow = ($vis->newRow() or $rowBlocks > 12) ? 1 : 0; |
91
|
|
|
|
|
|
|
|
92
|
0
|
0
|
|
|
|
|
$html_parts .= ' ' if $newRow; |
93
|
0
|
|
|
|
|
|
$html_parts .= $html; |
94
|
0
|
0
|
|
|
|
|
$html_parts .= '' if $newRow; |
95
|
0
|
0
|
|
|
|
|
$rowBlocks = 0 if $newRow; |
96
|
|
|
|
|
|
|
} |
97
|
0
|
|
|
|
|
|
my $footer = $vis->footer($self->{local}); |
98
|
0
|
0
|
|
|
|
|
$head_parts .= $footer if $footer; |
99
|
|
|
|
|
|
|
} |
100
|
|
|
|
|
|
|
} |
101
|
|
|
|
|
|
|
|
102
|
0
|
|
|
|
|
|
return "\n\n\n$head_parts\n\n\n$html_parts\n\n"; |
103
|
|
|
|
|
|
|
} |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
### Try load library |
106
|
|
|
|
|
|
|
sub _try_load { |
107
|
0
|
|
|
0
|
|
|
my $mod = shift; |
108
|
|
|
|
|
|
|
|
109
|
0
|
0
|
|
|
|
|
return +(0, "Not defined module.") unless $mod; |
110
|
0
|
0
|
|
|
|
|
return 1 if ($mod->can("html")); # because of local class in psgi |
111
|
0
|
0
|
|
|
|
|
eval("use $mod; 1") ? return 1 : return (0, $@); |
112
|
|
|
|
|
|
|
} |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
1; |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
=encoding utf-8 |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
=head1 NAME |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
Rest::HtmlVis - Rest API visualizer in HTML |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
=head1 SYNOPSIS |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
Transform perl hash to html. |
125
|
|
|
|
|
|
|
Each key in perl hash is transormed to the piece of html, js and css which are include in main html. |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
Example: |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
use Rest::HtmlVis; |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
my $htmlvis = Rest::HtmlVis->new({ |
132
|
|
|
|
|
|
|
events => Rest::HtmlVis::Events |
133
|
|
|
|
|
|
|
}); |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
$htmlvis->html({ |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
events => [ |
138
|
|
|
|
|
|
|
], |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
links => { |
141
|
|
|
|
|
|
|
rel => 'root', |
142
|
|
|
|
|
|
|
href => /, |
143
|
|
|
|
|
|
|
name => Root resource |
144
|
|
|
|
|
|
|
} |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
form => { |
147
|
|
|
|
|
|
|
GET => { |
148
|
|
|
|
|
|
|
from => { |
149
|
|
|
|
|
|
|
type => 'time', |
150
|
|
|
|
|
|
|
default => time(), |
151
|
|
|
|
|
|
|
} |
152
|
|
|
|
|
|
|
}, |
153
|
|
|
|
|
|
|
POST => { |
154
|
|
|
|
|
|
|
DATA => { |
155
|
|
|
|
|
|
|
type => "text" |
156
|
|
|
|
|
|
|
temperature => 25 |
157
|
|
|
|
|
|
|
}, |
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
} |
160
|
|
|
|
|
|
|
} |
161
|
|
|
|
|
|
|
}); |
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
HtmlVis has default blocks that are show everytime: |
165
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
=over 4 |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
=item * default.baseurl |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
Set default prefix for links in html. Default is '/static' |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
=item * default.base |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
See L. |
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
=item * default.content |
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
See L. |
179
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
=back |
181
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
These blocks can be rewrite when the base or content key is set in constructor params. |
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
=head1 SUBROUTINES/METHODS |
185
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
=head2 new( params ) |
187
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
Create new htmlvis object. You have to specify params for keys that should be transformed. |
189
|
|
|
|
|
|
|
|
190
|
|
|
|
|
|
|
=head3 params |
191
|
|
|
|
|
|
|
|
192
|
|
|
|
|
|
|
Define keys in input hash and library that manage this key. |
193
|
|
|
|
|
|
|
|
194
|
|
|
|
|
|
|
Example: |
195
|
|
|
|
|
|
|
|
196
|
|
|
|
|
|
|
{ events => Rest::HtmlVis::Events } |
197
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
Third-party js library are primary mapped to /static URL. |
199
|
|
|
|
|
|
|
You have to manage this url by your http server and map this url to share directory. |
200
|
|
|
|
|
|
|
For example in Plack: |
201
|
|
|
|
|
|
|
|
202
|
|
|
|
|
|
|
use File::ShareDir; |
203
|
|
|
|
|
|
|
my $share = File::ShareDir::dist_dir('Rest-HtmlVis') || "../Rest-HtmlVis/share/"; |
204
|
|
|
|
|
|
|
mount "/static" => Plack::App::File->new(root => $share); |
205
|
|
|
|
|
|
|
|
206
|
|
|
|
|
|
|
=cut |
207
|
|
|
|
|
|
|
|
208
|
|
|
|
|
|
|
=head2 html( hash_struct ) |
209
|
|
|
|
|
|
|
|
210
|
|
|
|
|
|
|
Convert input hash struct to html. Return html string. |
211
|
|
|
|
|
|
|
|
212
|
|
|
|
|
|
|
=cut |
213
|
|
|
|
|
|
|
|
214
|
|
|
|
|
|
|
=head1 TUTORIAL |
215
|
|
|
|
|
|
|
|
216
|
|
|
|
|
|
|
L |
217
|
|
|
|
|
|
|
|
218
|
|
|
|
|
|
|
=head1 AUTHOR |
219
|
|
|
|
|
|
|
|
220
|
|
|
|
|
|
|
Václav Dovrtěl Evaclav.dovrtel@gmail.comE |
221
|
|
|
|
|
|
|
|
222
|
|
|
|
|
|
|
=head1 BUGS |
223
|
|
|
|
|
|
|
|
224
|
|
|
|
|
|
|
Please report any bugs or feature requests to github repository. |
225
|
|
|
|
|
|
|
|
226
|
|
|
|
|
|
|
=head1 ACKNOWLEDGEMENTS |
227
|
|
|
|
|
|
|
|
228
|
|
|
|
|
|
|
Inspired by L |
229
|
|
|
|
|
|
|
|
230
|
|
|
|
|
|
|
=head1 COPYRIGHT |
231
|
|
|
|
|
|
|
|
232
|
|
|
|
|
|
|
Copyright 2015- Václav Dovrtěl |
233
|
|
|
|
|
|
|
|
234
|
|
|
|
|
|
|
=head1 LICENSE |
235
|
|
|
|
|
|
|
|
236
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or modify |
237
|
|
|
|
|
|
|
it under the same terms as Perl itself. |
238
|
|
|
|
|
|
|
|
239
|
|
|
|
|
|
|
=cut |