line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
#!/usr/bin/perl -w |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
# |
4
|
|
|
|
|
|
|
# db_to_html_table.pm |
5
|
|
|
|
|
|
|
# Copyright (C) 2007-2015 by John Heidemann |
6
|
|
|
|
|
|
|
# $Id: 82951024a92cc183126ddfc2a823e9e9f098bd27 $ |
7
|
|
|
|
|
|
|
# |
8
|
|
|
|
|
|
|
# This program is distributed under terms of the GNU general |
9
|
|
|
|
|
|
|
# public license, version 2. See the file COPYING |
10
|
|
|
|
|
|
|
# in $dblibdir for details. |
11
|
|
|
|
|
|
|
# |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
package Fsdb::Filter::db_to_html_table; |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
=head1 NAME |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
db_to_html_table - convert db to an HTML table |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
=head1 SYNOPSIS |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
db_to_html_table [-g N] dest.html |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
=head1 DESCRIPTION |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
Covert an existing dbtable to an HTML table. |
26
|
|
|
|
|
|
|
The output is a fragment of an HTML page; |
27
|
|
|
|
|
|
|
we assume the user fills in the rest (head and body, etc.). |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
Input is fsdb format. |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
Output is HTML code (I fsdb), |
32
|
|
|
|
|
|
|
with HTML-specific characters |
33
|
|
|
|
|
|
|
(less than, greater than, ampersand) are escaped. |
34
|
|
|
|
|
|
|
(The fsdb-1.x version assumed input was ISO-8859-1; we now assume |
35
|
|
|
|
|
|
|
both input and output are unicode. |
36
|
|
|
|
|
|
|
This change is considered a feature of the 21st century.) |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
=head1 OPTIONS |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
=over 4 |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
=item B<-g N> or <--group-count N> |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
Color groups of I consecutive rows with one background color. |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
=back |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
=for comment |
49
|
|
|
|
|
|
|
begin_standard_fsdb_options |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
This module also supports the standard fsdb options: |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
=over 4 |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
=item B<-d> |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
Enable debugging output. |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
=item B<-i> or B<--input> InputSource |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
Read from InputSource, typically a file name, or C<-> for standard input, |
62
|
|
|
|
|
|
|
or (if in Perl) a IO::Handle, Fsdb::IO or Fsdb::BoundedQueue objects. |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
=item B<-o> or B<--output> OutputDestination |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
Write to OutputDestination, typically a file name, or C<-> for standard output, |
67
|
|
|
|
|
|
|
or (if in Perl) a IO::Handle, Fsdb::IO or Fsdb::BoundedQueue objects. |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
=item B<--autorun> or B<--noautorun> |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
By default, programs process automatically, |
72
|
|
|
|
|
|
|
but Fsdb::Filter objects in Perl do not run until you invoke |
73
|
|
|
|
|
|
|
the run() method. |
74
|
|
|
|
|
|
|
The C<--(no)autorun> option controls that behavior within Perl. |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
=item B<--help> |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
Show help. |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
=item B<--man> |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
Show full manual. |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
=back |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
=for comment |
87
|
|
|
|
|
|
|
end_standard_fsdb_options |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
=head1 SAMPLE USAGE |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
=head2 Input: |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
#fsdb -F S account passwd uid gid fullname homedir shell |
95
|
|
|
|
|
|
|
johnh * 2274 134 John & Ampersand /home/johnh /bin/bash |
96
|
|
|
|
|
|
|
greg * 2275 134 Greg < Lessthan /home/greg /bin/bash |
97
|
|
|
|
|
|
|
root * 0 0 Root ; Semi /root /bin/bash |
98
|
|
|
|
|
|
|
four * 1 1 Fourth Row /home/four /bin/bash |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
=head2 Command: |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
cat data.fsdb | db_to_csv -g 3 |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
=head2 Output: |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
| account | passwd | uid | gid | fullname | homedir | shell |
108
|
|
|
|
|
|
|
| johnh | * | 2274 | 134 | John & Ampersand | /home/johnh | /bin/bash |
109
|
|
|
|
|
|
|
| greg | * | 2275 | 134 | Greg < Lessthan | /home/greg | /bin/bash |
110
|
|
|
|
|
|
|
| root | * | 0 | 0 | Root ; Semi | /root | /bin/bash |
111
|
|
|
|
|
|
|
| four | * | 1 | 1 | Fourth Row | /home/four | /bin/bash |
112
|
|
|
|
|
|
|
| |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
=head1 SEE ALSO |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
L. |
118
|
|
|
|
|
|
|
L. |
119
|
|
|
|
|
|
|
L. |
120
|
|
|
|
|
|
|
L. |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
=head1 CLASS FUNCTIONS |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
=cut |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
@ISA = qw(Fsdb::Filter); |
128
|
|
|
|
|
|
|
$VERSION = 2.0; |
129
|
|
|
|
|
|
|
|
130
|
1
|
|
|
1
|
|
3821
|
use strict; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
23
|
|
131
|
1
|
|
|
1
|
|
3
|
use Pod::Usage; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
74
|
|
132
|
1
|
|
|
1
|
|
5
|
use Carp; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
39
|
|
133
|
|
|
|
|
|
|
|
134
|
1
|
|
|
1
|
|
3
|
use Fsdb::Filter; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
25
|
|
135
|
1
|
|
|
1
|
|
4
|
use Fsdb::IO::Reader; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
542
|
|
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
=head2 new |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
$filter = new Fsdb::Filter::db_to_html_table(@arguments); |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
Create a new db_to_html_table object, taking command-line arguments. |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
=cut |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
sub new ($@) { |
147
|
0
|
|
|
0
|
1
|
|
my $class = shift @_; |
148
|
0
|
|
|
|
|
|
my $self = $class->SUPER::new(@_); |
149
|
0
|
|
|
|
|
|
bless $self, $class; |
150
|
0
|
|
|
|
|
|
$self->set_defaults; |
151
|
0
|
|
|
|
|
|
$self->parse_options(@_); |
152
|
0
|
|
|
|
|
|
$self->SUPER::post_new(); |
153
|
0
|
|
|
|
|
|
return $self; |
154
|
|
|
|
|
|
|
} |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
=head2 set_defaults |
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
$filter->set_defaults(); |
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
Internal: set up defaults. |
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
=cut |
164
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
sub set_defaults ($) { |
166
|
0
|
|
|
0
|
1
|
|
my($self) = @_; |
167
|
0
|
|
|
|
|
|
$self->SUPER::set_defaults(); |
168
|
0
|
|
|
|
|
|
$self->{_group_count} = undef; |
169
|
0
|
|
|
|
|
|
$self->{_logprog} = undef; # change default |
170
|
|
|
|
|
|
|
} |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
=head2 parse_options |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
$filter->parse_options(@ARGV); |
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
Internal: parse command-line arguments. |
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
=cut |
179
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
sub parse_options ($@) { |
181
|
0
|
|
|
0
|
1
|
|
my $self = shift @_; |
182
|
|
|
|
|
|
|
|
183
|
0
|
|
|
|
|
|
my(@argv) = @_; |
184
|
|
|
|
|
|
|
$self->get_options( |
185
|
|
|
|
|
|
|
\@argv, |
186
|
0
|
|
|
0
|
|
|
'help|?' => sub { pod2usage(1); }, |
187
|
0
|
|
|
0
|
|
|
'man' => sub { pod2usage(-verbose => 2); }, |
188
|
|
|
|
|
|
|
'autorun!' => \$self->{_autorun}, |
189
|
|
|
|
|
|
|
'd|debug+' => \$self->{_debug}, |
190
|
0
|
|
|
0
|
|
|
'i|input=s' => sub { $self->parse_io_option('input', @_); }, |
191
|
|
|
|
|
|
|
'g|group-count=i' => \$self->{_group_count}, |
192
|
|
|
|
|
|
|
'log!' => \$self->{_logprog}, |
193
|
0
|
|
|
0
|
|
|
'o|output=s' => sub { $self->parse_io_option('output', @_); }, |
194
|
0
|
0
|
|
|
|
|
) or pod2usage(2); |
195
|
0
|
0
|
|
|
|
|
pod2usage(2) if ($#argv != -1); |
196
|
|
|
|
|
|
|
} |
197
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
=head2 _format_row |
199
|
|
|
|
|
|
|
|
200
|
|
|
|
|
|
|
$self->_format_row($row_aref, $tag, $color); |
201
|
|
|
|
|
|
|
|
202
|
|
|
|
|
|
|
Return as a string the HTML table row corresponding to |
203
|
|
|
|
|
|
|
C<@$row_aref>, with each element delimited by C<$tag>, |
204
|
|
|
|
|
|
|
with color C<$color>. |
205
|
|
|
|
|
|
|
|
206
|
|
|
|
|
|
|
=cut |
207
|
|
|
|
|
|
|
|
208
|
|
|
|
|
|
|
sub _format_row { |
209
|
0
|
|
|
0
|
|
|
my($self, $row_aref, $tag, $color) = @_; |
210
|
|
|
|
|
|
|
|
211
|
0
|
0
|
|
|
|
|
$tag = "td" if (!defined($tag)); |
212
|
0
|
|
|
|
|
|
my $options = ""; |
213
|
0
|
0
|
|
|
|
|
$options = " bgcolor=\"$color\"" if (defined($color)); |
214
|
0
|
|
|
|
|
|
my $o = " |
";
215
|
0
|
|
|
|
|
|
foreach (@$row_aref) { |
216
|
0
|
|
|
|
|
|
my($content) = $_; |
217
|
0
|
|
|
|
|
|
$content =~ s/\&/&/g; |
218
|
0
|
|
|
|
|
|
$content =~ s/</g; |
219
|
0
|
|
|
|
|
|
$content =~ s/>/>/g; |
220
|
0
|
|
|
|
|
|
$o .= "<$tag>$content$tag> "; |
221
|
|
|
|
|
|
|
}; |
222
|
0
|
|
|
|
|
|
$o .= " |
\n";
223
|
0
|
|
|
|
|
|
return $o; |
224
|
|
|
|
|
|
|
} |
225
|
|
|
|
|
|
|
|
226
|
|
|
|
|
|
|
=head2 setup |
227
|
|
|
|
|
|
|
|
228
|
|
|
|
|
|
|
$filter->setup(); |
229
|
|
|
|
|
|
|
|
230
|
|
|
|
|
|
|
Internal: setup, parse headers. |
231
|
|
|
|
|
|
|
|
232
|
|
|
|
|
|
|
=cut |
233
|
|
|
|
|
|
|
|
234
|
|
|
|
|
|
|
sub setup ($) { |
235
|
0
|
|
|
0
|
1
|
|
my($self) = @_; |
236
|
|
|
|
|
|
|
|
237
|
0
|
|
|
0
|
|
|
$self->finish_io_option('input', -comment_handler => sub {}); |
238
|
|
|
|
|
|
|
|
239
|
0
|
|
|
|
|
|
$self->finish_fh_io_option('output'); |
240
|
|
|
|
|
|
|
|
241
|
|
|
|
|
|
|
# write out the header as the first line |
242
|
0
|
|
|
|
|
|
my $cols_aref = $self->{_in}->cols; |
243
|
0
|
|
|
|
|
|
$self->{_out}->print("\n" .
244
|
|
|
|
|
|
|
$self->_format_row($cols_aref, 'th')); |
245
|
|
|
|
|
|
|
} |
246
|
|
|
|
|
|
|
|
247
|
|
|
|
|
|
|
=head2 run |
248
|
|
|
|
|
|
|
|
249
|
|
|
|
|
|
|
$filter->run(); |
250
|
|
|
|
|
|
|
|
251
|
|
|
|
|
|
|
Internal: run over each rows. |
252
|
|
|
|
|
|
|
|
253
|
|
|
|
|
|
|
=cut |
254
|
|
|
|
|
|
|
sub run ($) { |
255
|
0
|
|
|
0
|
1
|
|
my($self) = @_; |
256
|
0
|
|
|
|
|
|
my $out_fh = $self->{_out}; |
257
|
|
|
|
|
|
|
|
258
|
0
|
|
|
|
|
|
my $read_fastpath_sub = $self->{_in}->fastpath_sub(); |
259
|
0
|
|
|
|
|
|
my $fref; |
260
|
0
|
|
|
|
|
|
my($color, $other_color) = (undef, undef); |
261
|
0
|
|
|
|
|
|
my $group_n = 0; |
262
|
0
|
|
|
|
|
|
my $group_count = $self->{_group_count}; |
263
|
0
|
0
|
|
|
|
|
if (defined($group_count)) { |
264
|
|
|
|
|
|
|
# xxx: hard coded for now |
265
|
0
|
|
|
|
|
|
($color, $other_color) = ("#f0f0f0", "#d0d0d0"); |
266
|
|
|
|
|
|
|
}; |
267
|
0
|
|
|
|
|
|
while ($fref = &{$read_fastpath_sub}()) { |
|
0
|
|
|
|
|
|
|
268
|
0
|
0
|
|
|
|
|
if (defined($group_count)) { |
269
|
0
|
0
|
|
|
|
|
if ($group_n++ >= $group_count) { |
270
|
0
|
|
|
|
|
|
($color, $other_color) = ($other_color, $color); |
271
|
0
|
|
|
|
|
|
$group_n = 1; |
272
|
|
|
|
|
|
|
}; |
273
|
|
|
|
|
|
|
}; |
274
|
0
|
|
|
|
|
|
$out_fh->print($self->_format_row($fref, 'td', $color)); |
275
|
|
|
|
|
|
|
}; |
276
|
|
|
|
|
|
|
|
277
|
0
|
|
|
|
|
|
$out_fh->print(" | \n"); |
278
|
|
|
|
|
|
|
} |
279
|
|
|
|
|
|
|
|
280
|
|
|
|
|
|
|
|
281
|
|
|
|
|
|
|
=head1 AUTHOR and COPYRIGHT |
282
|
|
|
|
|
|
|
|
283
|
|
|
|
|
|
|
Copyright (C) 2007-2015 by John Heidemann |
284
|
|
|
|
|
|
|
|
285
|
|
|
|
|
|
|
This program is distributed under terms of the GNU general |
286
|
|
|
|
|
|
|
public license, version 2. See the file COPYING |
287
|
|
|
|
|
|
|
with the distribution for details. |
288
|
|
|
|
|
|
|
|
289
|
|
|
|
|
|
|
=cut |
290
|
|
|
|
|
|
|
|
291
|
|
|
|
|
|
|
1; |