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