-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHTML.pm
411 lines (298 loc) · 9.37 KB
/
HTML.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
package Plack::App::Tags::HTML;
use base qw(Plack::Component::Tags::HTML);
use strict;
use warnings;
use English;
use Error::Pure qw(err);
use Plack::Util::Accessor qw(component constructor_args data data_css data_init data_prepare);
use Symbol::Get;
our $VERSION = 0.18;
sub _css {
my ($self, $env) = @_;
if ($self->{'_component'}->can('process_css')) {
my @data_css;
if (defined $self->data_css) {
push @data_css, @{$self->data_css};
}
$self->{'_component'}->process_css(@data_css);
}
return;
}
sub _loaded_component {
my ($self, $component) = @_;
my @names = eval {
Symbol::Get::get_names($component);
};
if ($EVAL_ERROR) {
return 0;
}
return 1;
}
sub _prepare_app {
my $self = shift;
$self->SUPER::_prepare_app();
my %p = (
'css' => $self->css,
'tags' => $self->tags,
);
my $component = $self->component;
if (! $self->_loaded_component($component)) {
eval "require $component;";
if ($EVAL_ERROR) {
err "Cannot load component '$component'.",
'Error', $EVAL_ERROR;
}
}
$self->{'_component'} = $component->new(
%p,
defined $self->constructor_args ? (
%{$self->constructor_args},
) : (),
);
if (! $self->{'_component'}->isa('Tags::HTML')) {
err "Component must be a instance of 'Tags::HTML' class.";
}
# Init prepared data.
if ($self->{'_component'}->can('prepare')) {
my @data = ();
if (defined $self->data_prepare) {
push @data, @{$self->data_prepare};
}
$self->{'_component'}->prepare(@data);
}
# Copy CSS links from component to main object.
if ($self->{'_component'}->can('css_src')) {
$self->css_src($self->{'_component'}->css_src);
}
# Copy Javascript links from component to main object.
if ($self->{'_component'}->can('script_js_src')) {
$self->script_js_src($self->{'_component'}->script_js_src);
}
return;
}
sub _process_actions {
my ($self, $env) = @_;
if ($self->{'_component'}->can('init')) {
if (defined $self->data_init) {
$self->{'_component'}->init(@{$self->data_init});
} else {
$self->{'_component'}->init;
}
}
# Copy Javascript code from component to main object.
if ($self->{'_component'}->can('script_js')) {
$self->script_js($self->{'_component'}->script_js);
}
# Init begin of page.
$self->SUPER::_process_actions($env);
return;
}
sub _tags_middle {
my ($self, $env) = @_;
my @data;
if (defined $self->data) {
push @data, @{$self->data};
}
$self->{'_component'}->process(@data);
return;
}
1;
__END__
=pod
=encoding utf8
=head1 NAME
Plack::App::Tags::HTML - Plack application for running L<Tags::HTML> objects.
=head1 SYNOPSIS
use Plack::App::Tags::HTML;
my $obj = Plack::App::Tags::HTML->new(%parameters);
my $app = $obj->to_app;
=head1 METHODS
Class inherites L<Plack::Component::Tags::HTML>.
=head2 C<new>
my $obj = Plack::App::Tags::HTML->new(%parameters);
Constructor.
Returns instance of object.
=over 8
=item * C<component>
L<Tags::HTML> component.
Option is required.
=item * C<constructor_args>
L<Tags::HTML> component constructor arguments.
Default value is undef.
=item * C<data>
Array data structure as input argument of L<Tags::HTML::process()|Tags::HTML/process>.
Default value is undef.
=item * C<data_css>
Reference to array with structure for input argument of L<Tags::HTML::process_css()|Tags::HTML/process_css>.
Default value is undef.
=item * C<data_init>
Reference to array with structure for input argument of L<Tags::HTML::init()|Tags::HTML/init>.
This structure is used in init phase of each web app call.
Default value is undef.
=item * C<data_prepare>
Reference to array with structure for input argument of L<Tags::HTML::prepare()|Tags::HTML/prepare>.
This structure is used in prepare phase of web app run.
Default value is undef.
=back
=head2 C<to_app>
my $app = $obj->to_app;
Get code of plack application.
Returns code of app.
=head1 ERRORS
prepare_app():
Cannot load component '%s'.
Error: %s
Component must be a instance of 'Tags::HTML' class.
=head1 EXAMPLE1
=for comment filename=web_app_with_stars.pl
use strict;
use warnings;
use CSS::Struct::Output::Indent;
use Plack::App::Tags::HTML;
use Plack::Runner;
use Tags::Output::Indent;
# Run application.
my $app = Plack::App::Tags::HTML->new(
'component' => 'Tags::HTML::Stars',
'css' => CSS::Struct::Output::Indent->new,,
'data' => [{
1 => 'full',
2 => 'half',
3 => 'nothing',
}],
'tags' => Tags::Output::Indent->new(
'preserved' => ['style'],
),
)->to_app;
Plack::Runner->new->run($app);
# Output:
# HTTP::Server::PSGI: Accepting connections at http://0:5000/
# > curl http://localhost:5000/
# <!DOCTYPE html>
# <html lang="en">
# <head>
# <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
# </meta>
# <meta name="viewport" content="width=device-width, initial-scale=1.0">
# </meta>
# <style type="text/css">
# * {
# box-sizing: border-box;
# margin: 0;
# padding: 0;
# }
# </style>
# </head>
# <body>
# <div>
# <img src=
# "data:image/svg+xml;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2U9IiM2MDVhMDAiIHN0cm9rZS13aWR0aD0iMTUiIHBvaW50cz0iMTUwLDI1IDE3OSwxMTEgMjY5LDExMSAxOTcsMTY1IDIyMywyNTEgMTUwLDIwMCA3NywyNTEgMTAzLDE2NSAzMSwxMTEgMTIxLDExMSIgY2xpcC1wYXRoPSJ1cmwoI2ZpbGxlZCkiIC8+Cjwvc3ZnPgo="
# >
# </img>
# <img src=
# "data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iMzAwcHgiIGhlaWdodD0iMjc1cHgiIHZpZXdCb3g9IjAgMCAzMDAgMjc1IiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZlcnNpb249IjEuMSI+CiAgPHBvbHlnb24gZmlsbD0ibm9uZSIgc3Ryb2tlPSIjODA4MDgwIiBzdHJva2Utd2lkdGg9IjE1IiBzdHJva2Utb3BhY2l0eT0iMC4zNzY0NzA2MCIgcG9pbnRzPSIxNTAsMjUgMTc5LDExMSAyNjksMTExIDE5NywxNjUgMjIzLDI1MSAxNTAsMjAwIDc3LDI1MSAxMDMsMTY1IDMxLDExMSAxMjEsMTExIiAvPgo8L3N2Zz4K"
# >
# </img>
# </div>
# </body>
# </html>
=begin html
<a href="https://raw.githubusercontent.com/michal-josef-spacek/Plack-App-Tags-HTML/master/images/ex1.png">
<img src="https://raw.githubusercontent.com/michal-josef-spacek/Plack-App-Tags-HTML/master/images/ex1.png" alt="Example #1 web application" width="300px" height="300px" />
</a>
=end html
=head1 EXAMPLE2
=for comment filename=web_app_with_div.pl
use strict;
use warnings;
package App;
use base qw(Tags::HTML);
sub _process {
my ($self, $value_hr) = @_;
$self->{'tags'}->put(
['b', 'div'],
['a', 'class', 'my-class'],
['d', join ',', @{$value_hr->{'foo'}}],
['e', 'div'],
);
return;
}
sub _process_css {
my $self = shift;
$self->{'css'}->put(
['s', '.my-class'],
['d', 'border', '1px solid black'],
['e'],
);
return;
}
package main;
use CSS::Struct::Output::Indent;
use Plack::App::Tags::HTML;
use Plack::Runner;
use Tags::Output::Indent;
# Run application.
my $app = Plack::App::Tags::HTML->new(
'component' => 'App',
'css' => CSS::Struct::Output::Indent->new,
'data' => [{
'foo' => [1, 2],
}],
'tags' => Tags::Output::Indent->new(
'preserved' => ['style'],
),
)->to_app;
Plack::Runner->new->run($app);
# Output:
# HTTP::Server::PSGI: Accepting connections at http://0:5000/
# > curl http://localhost:5000/
# <!DOCTYPE html>
# <html lang="en">
# <head>
# <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
# </meta>
# <meta name="viewport" content="width=device-width, initial-scale=1.0">
# </meta>
# <style type="text/css">
# * {
# box-sizing: border-box;
# margin: 0;
# padding: 0;
# }
# .my-class {
# border: 1px solid black;
# }
# </style>
# </head>
# <body>
# <div class="my-class">
# 1,2
# </div>
# </body>
# </html>
=head1 DEPENDENCIES
L<English>,
L<Error::Pure>,
L<Plack::Component::Tags::HTML>,
L<Plack::Util::Accessor>,
L<Symbol::Get>.
=head1 SEE ALSO
=over
=item L<Tags::HTML>
Tags helper abstract class.
=back
=head1 REPOSITORY
L<https://github.com/michal-josef-spacek/Plack-App-Tags-HTML>
=head1 AUTHOR
Michal Josef Špaček L<mailto:[email protected]>
L<http://skim.cz>
=head1 LICENSE AND COPYRIGHT
© 2021-2024 Michal Josef Špaček
BSD 2-Clause License
=head1 VERSION
0.18
=cut