line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Tags::HTML::Messages; |
2
|
|
|
|
|
|
|
|
3
|
5
|
|
|
5
|
|
398188
|
use base qw(Tags::HTML); |
|
5
|
|
|
|
|
52
|
|
|
5
|
|
|
|
|
2586
|
|
4
|
5
|
|
|
5
|
|
33023
|
use strict; |
|
5
|
|
|
|
|
12
|
|
|
5
|
|
|
|
|
93
|
|
5
|
5
|
|
|
5
|
|
27
|
use warnings; |
|
5
|
|
|
|
|
7
|
|
|
5
|
|
|
|
|
133
|
|
6
|
|
|
|
|
|
|
|
7
|
5
|
|
|
5
|
|
25
|
use Class::Utils qw(set_params split_params); |
|
5
|
|
|
|
|
1687
|
|
|
5
|
|
|
|
|
218
|
|
8
|
5
|
|
|
5
|
|
42
|
use Error::Pure qw(err); |
|
5
|
|
|
|
|
11
|
|
|
5
|
|
|
|
|
183
|
|
9
|
5
|
|
|
5
|
|
28
|
use Scalar::Util qw(blessed); |
|
5
|
|
|
|
|
8
|
|
|
5
|
|
|
|
|
2854
|
|
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
our $VERSION = 0.07; |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
# Constructor. |
14
|
|
|
|
|
|
|
sub new { |
15
|
21
|
|
|
21
|
1
|
25631
|
my ($class, @params) = @_; |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
# Create object. |
18
|
21
|
|
|
|
|
72
|
my ($object_params_ar, $other_params_ar) = split_params( |
19
|
|
|
|
|
|
|
['css_messages', 'flag_no_messages'], @params); |
20
|
21
|
|
|
|
|
530
|
my $self = $class->SUPER::new(@{$other_params_ar}); |
|
21
|
|
|
|
|
83
|
|
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
# CSS class. |
23
|
19
|
|
|
|
|
585
|
$self->{'css_messages'} = 'messages'; |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
# Flag for no messages. |
26
|
19
|
|
|
|
|
35
|
$self->{'flag_no_messages'} = 1; |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
# Process params. |
29
|
19
|
|
|
|
|
26
|
set_params($self, @{$object_params_ar}); |
|
19
|
|
|
|
|
55
|
|
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
# Object. |
32
|
19
|
|
|
|
|
218
|
return $self; |
33
|
|
|
|
|
|
|
} |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
sub _check_messages { |
36
|
10
|
|
|
10
|
|
15
|
my ($self, $message_ar) = @_; |
37
|
|
|
|
|
|
|
|
38
|
10
|
100
|
|
|
|
33
|
if (ref $message_ar ne 'ARRAY') { |
39
|
1
|
|
|
|
|
8
|
err "Bad list of messages."; |
40
|
|
|
|
|
|
|
} |
41
|
9
|
|
|
|
|
14
|
foreach my $message (@{$message_ar}) { |
|
9
|
|
|
|
|
19
|
|
42
|
8
|
100
|
100
|
|
|
60
|
if (! blessed($message) || ! $message->isa('Data::Message::Simple')) { |
43
|
|
|
|
|
|
|
|
44
|
2
|
|
|
|
|
56
|
err 'Bad message data object.'; |
45
|
|
|
|
|
|
|
} |
46
|
|
|
|
|
|
|
} |
47
|
|
|
|
|
|
|
|
48
|
7
|
|
|
|
|
21
|
return; |
49
|
|
|
|
|
|
|
} |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
# Process 'Tags'. |
52
|
|
|
|
|
|
|
sub _process { |
53
|
10
|
|
|
10
|
|
1073
|
my ($self, $message_ar) = @_; |
54
|
|
|
|
|
|
|
|
55
|
10
|
|
|
|
|
28
|
$self->_check_messages($message_ar); |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
# No messages. |
58
|
7
|
100
|
100
|
|
|
22
|
if (! $self->{'flag_no_messages'} && ! @{$message_ar}) { |
|
2
|
|
|
|
|
8
|
|
59
|
1
|
|
|
|
|
3
|
return; |
60
|
|
|
|
|
|
|
} |
61
|
|
|
|
|
|
|
|
62
|
6
|
|
|
|
|
9
|
my $num = 0; |
63
|
|
|
|
|
|
|
$self->{'tags'}->put( |
64
|
|
|
|
|
|
|
['b', 'div'], |
65
|
6
|
|
|
|
|
35
|
['a', 'class', $self->{'css_messages'}], |
66
|
|
|
|
|
|
|
); |
67
|
6
|
100
|
|
|
|
424
|
if (@{$message_ar}) { |
|
6
|
|
|
|
|
15
|
|
68
|
5
|
|
|
|
|
8
|
foreach my $message (@{$message_ar}) { |
|
5
|
|
|
|
|
10
|
|
69
|
6
|
100
|
|
|
|
14
|
if ($num) { |
70
|
1
|
|
|
|
|
28
|
$self->{'tags'}->put( |
71
|
|
|
|
|
|
|
['b', 'br'], |
72
|
|
|
|
|
|
|
['e', 'br'], |
73
|
|
|
|
|
|
|
); |
74
|
|
|
|
|
|
|
} |
75
|
6
|
100
|
|
|
|
85
|
$self->{'tags'}->put( |
76
|
|
|
|
|
|
|
['b', 'span'], |
77
|
|
|
|
|
|
|
['a', 'class', $message->type], |
78
|
|
|
|
|
|
|
defined $message->lang |
79
|
|
|
|
|
|
|
? (['a', 'lang', $message->lang]) |
80
|
|
|
|
|
|
|
: (), |
81
|
|
|
|
|
|
|
['d', $message->text], |
82
|
|
|
|
|
|
|
['e', 'span'], |
83
|
|
|
|
|
|
|
); |
84
|
6
|
|
|
|
|
830
|
$num++; |
85
|
|
|
|
|
|
|
} |
86
|
|
|
|
|
|
|
} else { |
87
|
1
|
|
|
|
|
14
|
$self->{'tags'}->put( |
88
|
|
|
|
|
|
|
['d', 'No messages'], |
89
|
|
|
|
|
|
|
); |
90
|
|
|
|
|
|
|
} |
91
|
6
|
|
|
|
|
45
|
$self->{'tags'}->put( |
92
|
|
|
|
|
|
|
['e', 'div'], |
93
|
|
|
|
|
|
|
); |
94
|
|
|
|
|
|
|
|
95
|
6
|
|
|
|
|
220
|
return; |
96
|
|
|
|
|
|
|
} |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
# Process 'CSS::Struct'. |
99
|
|
|
|
|
|
|
sub _process_css { |
100
|
4
|
|
|
4
|
|
51
|
my ($self, $message_types_hr) = @_; |
101
|
|
|
|
|
|
|
|
102
|
4
|
100
|
100
|
|
|
27
|
if (! defined $message_types_hr || ref $message_types_hr ne 'HASH') { |
103
|
2
|
|
|
|
|
7
|
return; |
104
|
|
|
|
|
|
|
} |
105
|
|
|
|
|
|
|
|
106
|
2
|
|
|
|
|
6
|
foreach my $message_type (keys %{$message_types_hr}) { |
|
2
|
|
|
|
|
9
|
|
107
|
|
|
|
|
|
|
$self->{'css'}->put( |
108
|
|
|
|
|
|
|
['s', '.'.$message_type], |
109
|
1
|
|
|
|
|
10
|
['d', 'color', $message_types_hr->{$message_type}], |
110
|
|
|
|
|
|
|
['e'], |
111
|
|
|
|
|
|
|
); |
112
|
|
|
|
|
|
|
} |
113
|
|
|
|
|
|
|
|
114
|
2
|
|
|
|
|
135
|
return; |
115
|
|
|
|
|
|
|
} |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
1; |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
__END__ |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
=pod |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
=encoding utf8 |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
=head1 NAME |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
Tags::HTML::Messages - Tags helper for HTML messages. |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
=head1 SYNOPSIS |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
use Tags::HTML::Messages; |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
my $obj = Tags::HTML::Messages->new(%params); |
134
|
|
|
|
|
|
|
$obj->process($message_ar); |
135
|
|
|
|
|
|
|
$obj->process_css($type, $color); |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
=head1 METHODS |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
=head2 C<new> |
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
my $obj = Tags::HTML::Messages->new(%params); |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
Constructor. |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
=over 8 |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
=item * C<css> |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
'CSS::Struct::Output' object for L<process_css> processing. |
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
Default value is undef. |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
=item * C<css_messages> |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
CSS class for main messages div block. |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
Default value is 'messages'. |
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
=item * C<flag_no_messages> |
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
Flag for no messages printing. |
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
Possible values: |
164
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
0 - Print nothing |
166
|
|
|
|
|
|
|
1 - Print message box with 'No messages.' text. |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
Default value is 1. |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
=item * C<tags> |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
'Tags::Output' object. |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
Default value is undef. |
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
=back |
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
=head2 C<process> |
179
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
$obj->process($message_ar); |
181
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
Process Tags structure for output. |
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
Reference to array with message objects C<$message_ar> must be a instance of |
185
|
|
|
|
|
|
|
L<Data::Message::Simple> object. |
186
|
|
|
|
|
|
|
|
187
|
|
|
|
|
|
|
Returns undef. |
188
|
|
|
|
|
|
|
|
189
|
|
|
|
|
|
|
=head2 C<process_css> |
190
|
|
|
|
|
|
|
|
191
|
|
|
|
|
|
|
$obj->process_css($message_types_hr); |
192
|
|
|
|
|
|
|
|
193
|
|
|
|
|
|
|
Process CSS::Struct structure for output. |
194
|
|
|
|
|
|
|
|
195
|
|
|
|
|
|
|
Variable C<$message_type_hr> is reference to hash with keys for message type and value for color in CSS style. |
196
|
|
|
|
|
|
|
Possible message types are info and error now. Types are defined in L<Data::Message::Simple>. |
197
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
Returns undef. |
199
|
|
|
|
|
|
|
|
200
|
|
|
|
|
|
|
=head1 ERRORS |
201
|
|
|
|
|
|
|
|
202
|
|
|
|
|
|
|
new(): |
203
|
|
|
|
|
|
|
From Class::Utils::set_params(): |
204
|
|
|
|
|
|
|
Unknown parameter '%s'. |
205
|
|
|
|
|
|
|
Parameter 'css' must be a 'CSS::Struct::Output::*' class. |
206
|
|
|
|
|
|
|
Parameter 'tags' must be a 'Tags::Output::*' class. |
207
|
|
|
|
|
|
|
|
208
|
|
|
|
|
|
|
process(): |
209
|
|
|
|
|
|
|
Bad list of messages. |
210
|
|
|
|
|
|
|
Bad message data object. |
211
|
|
|
|
|
|
|
|
212
|
|
|
|
|
|
|
=head1 EXAMPLE1 |
213
|
|
|
|
|
|
|
|
214
|
|
|
|
|
|
|
=for comment filename=html_page_with_messages.pl |
215
|
|
|
|
|
|
|
|
216
|
|
|
|
|
|
|
use strict; |
217
|
|
|
|
|
|
|
use warnings; |
218
|
|
|
|
|
|
|
|
219
|
|
|
|
|
|
|
use CSS::Struct::Output::Indent; |
220
|
|
|
|
|
|
|
use Data::Message::Simple; |
221
|
|
|
|
|
|
|
use Tags::HTML::Page::Begin; |
222
|
|
|
|
|
|
|
use Tags::HTML::Page::End; |
223
|
|
|
|
|
|
|
use Tags::HTML::Messages; |
224
|
|
|
|
|
|
|
use Tags::Output::Indent; |
225
|
|
|
|
|
|
|
|
226
|
|
|
|
|
|
|
# Object. |
227
|
|
|
|
|
|
|
my $tags = Tags::Output::Indent->new( |
228
|
|
|
|
|
|
|
'preserved' => ['style'], |
229
|
|
|
|
|
|
|
'xml' => 1, |
230
|
|
|
|
|
|
|
); |
231
|
|
|
|
|
|
|
my $css = CSS::Struct::Output::Indent->new; |
232
|
|
|
|
|
|
|
my $begin = Tags::HTML::Page::Begin->new( |
233
|
|
|
|
|
|
|
'css' => $css, |
234
|
|
|
|
|
|
|
'lang' => { |
235
|
|
|
|
|
|
|
'title' => 'Tags::HTML::Messages example', |
236
|
|
|
|
|
|
|
}, |
237
|
|
|
|
|
|
|
'generator' => 'Tags::HTML::Messages', |
238
|
|
|
|
|
|
|
'tags' => $tags, |
239
|
|
|
|
|
|
|
); |
240
|
|
|
|
|
|
|
my $end = Tags::HTML::Page::End->new( |
241
|
|
|
|
|
|
|
'tags' => $tags, |
242
|
|
|
|
|
|
|
); |
243
|
|
|
|
|
|
|
my $messages = Tags::HTML::Messages->new( |
244
|
|
|
|
|
|
|
'css' => $css, |
245
|
|
|
|
|
|
|
'tags' => $tags, |
246
|
|
|
|
|
|
|
); |
247
|
|
|
|
|
|
|
|
248
|
|
|
|
|
|
|
# Error structure. |
249
|
|
|
|
|
|
|
my $message_ar = [ |
250
|
|
|
|
|
|
|
Data::Message::Simple->new( |
251
|
|
|
|
|
|
|
'text' => 'Error #1', |
252
|
|
|
|
|
|
|
'type' => 'error', |
253
|
|
|
|
|
|
|
), |
254
|
|
|
|
|
|
|
Data::Message::Simple->new( |
255
|
|
|
|
|
|
|
'text' => 'Error #2', |
256
|
|
|
|
|
|
|
'type' => 'error', |
257
|
|
|
|
|
|
|
), |
258
|
|
|
|
|
|
|
Data::Message::Simple->new( |
259
|
|
|
|
|
|
|
'lang' => 'en', |
260
|
|
|
|
|
|
|
'text' => 'Ok #1', |
261
|
|
|
|
|
|
|
), |
262
|
|
|
|
|
|
|
Data::Message::Simple->new( |
263
|
|
|
|
|
|
|
'text' => 'Ok #2', |
264
|
|
|
|
|
|
|
), |
265
|
|
|
|
|
|
|
]; |
266
|
|
|
|
|
|
|
|
267
|
|
|
|
|
|
|
# Process page. |
268
|
|
|
|
|
|
|
$messages->process_css({ |
269
|
|
|
|
|
|
|
'error' => 'red', |
270
|
|
|
|
|
|
|
'info' => 'green', |
271
|
|
|
|
|
|
|
}); |
272
|
|
|
|
|
|
|
$begin->process; |
273
|
|
|
|
|
|
|
$messages->process($message_ar); |
274
|
|
|
|
|
|
|
$end->process; |
275
|
|
|
|
|
|
|
|
276
|
|
|
|
|
|
|
# Print out. |
277
|
|
|
|
|
|
|
print $tags->flush; |
278
|
|
|
|
|
|
|
|
279
|
|
|
|
|
|
|
# Output: |
280
|
|
|
|
|
|
|
# <!DOCTYPE html> |
281
|
|
|
|
|
|
|
# <html lang="en"> |
282
|
|
|
|
|
|
|
# <head> |
283
|
|
|
|
|
|
|
# <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> |
284
|
|
|
|
|
|
|
# <meta name="generator" content="Tags::HTML::Messages" /> |
285
|
|
|
|
|
|
|
# <meta name="viewport" content="width=device-width, initial-scale=1.0" /> |
286
|
|
|
|
|
|
|
# <title> |
287
|
|
|
|
|
|
|
# Tags::HTML::Messages example |
288
|
|
|
|
|
|
|
# </title> |
289
|
|
|
|
|
|
|
# <style type="text/css"> |
290
|
|
|
|
|
|
|
# .error { |
291
|
|
|
|
|
|
|
# color: red; |
292
|
|
|
|
|
|
|
# } |
293
|
|
|
|
|
|
|
# .info { |
294
|
|
|
|
|
|
|
# color: green; |
295
|
|
|
|
|
|
|
# } |
296
|
|
|
|
|
|
|
# </style> |
297
|
|
|
|
|
|
|
# </head> |
298
|
|
|
|
|
|
|
# <body> |
299
|
|
|
|
|
|
|
# <div class="messages"> |
300
|
|
|
|
|
|
|
# <span class="error"> |
301
|
|
|
|
|
|
|
# Error #1 |
302
|
|
|
|
|
|
|
# </span> |
303
|
|
|
|
|
|
|
# <br /> |
304
|
|
|
|
|
|
|
# <span class="error"> |
305
|
|
|
|
|
|
|
# Error #2 |
306
|
|
|
|
|
|
|
# </span> |
307
|
|
|
|
|
|
|
# <br /> |
308
|
|
|
|
|
|
|
# <span class="info" lang="en"> |
309
|
|
|
|
|
|
|
# Ok #1 |
310
|
|
|
|
|
|
|
# </span> |
311
|
|
|
|
|
|
|
# <br /> |
312
|
|
|
|
|
|
|
# <span class="info"> |
313
|
|
|
|
|
|
|
# Ok #2 |
314
|
|
|
|
|
|
|
# </span> |
315
|
|
|
|
|
|
|
# </div> |
316
|
|
|
|
|
|
|
# </body> |
317
|
|
|
|
|
|
|
# </html> |
318
|
|
|
|
|
|
|
|
319
|
|
|
|
|
|
|
=head1 DEPENDENCIES |
320
|
|
|
|
|
|
|
|
321
|
|
|
|
|
|
|
L<Class::Utils>, |
322
|
|
|
|
|
|
|
L<Error::Pure>, |
323
|
|
|
|
|
|
|
L<Scalar::Util>, |
324
|
|
|
|
|
|
|
L<Tags::HTML>. |
325
|
|
|
|
|
|
|
|
326
|
|
|
|
|
|
|
=head1 REPOSITORY |
327
|
|
|
|
|
|
|
|
328
|
|
|
|
|
|
|
L<https://github.com/michal-josef-spacek/Tags-HTML-Messages> |
329
|
|
|
|
|
|
|
|
330
|
|
|
|
|
|
|
=head1 AUTHOR |
331
|
|
|
|
|
|
|
|
332
|
|
|
|
|
|
|
Michal Josef Špaček L<mailto:skim@cpan.org> |
333
|
|
|
|
|
|
|
|
334
|
|
|
|
|
|
|
L<http://skim.cz> |
335
|
|
|
|
|
|
|
|
336
|
|
|
|
|
|
|
=head1 LICENSE AND COPYRIGHT |
337
|
|
|
|
|
|
|
|
338
|
|
|
|
|
|
|
© Michal Josef Špaček 2020-2023 |
339
|
|
|
|
|
|
|
|
340
|
|
|
|
|
|
|
BSD 2-Clause License |
341
|
|
|
|
|
|
|
|
342
|
|
|
|
|
|
|
=head1 VERSION |
343
|
|
|
|
|
|
|
|
344
|
|
|
|
|
|
|
0.07 |
345
|
|
|
|
|
|
|
|
346
|
|
|
|
|
|
|
=cut |