File Coverage

blib/lib/App/RecordStream/Operation/tohtml.pm
Criterion Covered Total %
statement 60 64 93.7
branch 6 6 100.0
condition 1 2 50.0
subroutine 10 11 90.9
pod 0 7 0.0
total 77 90 85.5


"); "); ");
line stmt bran cond sub pod time code
1             package App::RecordStream::Operation::tohtml;
2              
3             our $VERSION = "4.0.25";
4              
5 2     2   844 use strict;
  2         4  
  2         41  
6 2     2   7 use warnings;
  2         3  
  2         39  
7              
8 2     2   6 use base qw(App::RecordStream::Operation);
  2         4  
  2         1022  
9              
10             sub init {
11 5     5 0 6 my $this = shift;
12 5         5 my $args = shift;
13              
14 5         6 my $no_header;
15 5         5 my $row_attributes = '';
16 5         8 my $cell_attributes = '';
17              
18 5         19 my $key_groups = App::RecordStream::KeyGroups->new();
19              
20             my $spec = {
21 2     2   1083 'keys|k|fields|f=s' => sub { $key_groups->add_groups($_[1]); },
22 5         26 'noheader' => \$no_header,
23             'rowattributes=s' => \$row_attributes,
24             'cellattributes=s' => \$cell_attributes,
25             };
26              
27 5         16 $this->parse_options($args, $spec);
28              
29 5         9 $this->{'KEY_GROUPS'} = $key_groups;
30 5         7 $this->{'NO_HEADER'} = $no_header;
31 5         9 $this->{'ROW_ATTRIBUTES'} = $row_attributes;
32 5         36 $this->{'CELL_ATTRIBUTES'} = $cell_attributes;
33             }
34              
35             sub accept_record {
36 25     25 0 29 my $this = shift;
37 25         24 my $record = shift;
38              
39 25         45 $this->print_start($record);
40              
41 25         26 my $fields = $this->{'FIELDS'};
42 25         28 my $row_attributes = $this->{'ROW_ATTRIBUTES'};
43 25         25 my $cell_attributes = $this->{'CELL_ATTRIBUTES'};
44              
45 25         57 $this->push_line("
46              
47 25         33 foreach my $field (@$fields) {
48 45   50     42 my $value = ${$record->guess_key_from_spec($field)} || '';
49 45         116 $this->push_line(" $value
50             }
51              
52 25         48 $this->push_line("
53              
54 25         90 return 1;
55             }
56              
57             sub print_start {
58 25     25 0 27 my $this = shift;
59 25         26 my $record = shift;
60              
61 25 100       40 return if ( $this->{'PRINTED_START'} );
62 5         9 $this->{'PRINTED_START'} = 1;
63              
64 5         13 $this->push_line(""); "); "); ");
65              
66 5         7 my $groups = $this->{'KEY_GROUPS'};
67 5 100       10 if ( $groups->has_any_group() ) {
68 2         5 my $specs = $this->{'KEY_GROUPS'}->get_keyspecs($record);
69 2         4 $this->{'FIELDS'} = $specs;
70             }
71             else {
72 3         12 $this->{'FIELDS'} = [sort keys %$record];
73             }
74              
75 5 100       10 return if ( $this->{'NO_HEADER'} );
76              
77 4         9 $this->print_header();
78             }
79              
80             sub print_header {
81 4     4 0 5 my $this = shift;
82              
83 4         5 my $fields = $this->{'FIELDS'};
84              
85 4         4 my $row_attributes = $this->{'ROW_ATTRIBUTES'};
86 4         5 my $cell_attributes = $this->{'CELL_ATTRIBUTES'};
87              
88 4         13 $this->push_line("
89              
90 4         5 foreach my $field (@$fields) {
91 7         14 $this->push_line(" $field
92             }
93              
94 4         12 $this->push_line("
95             }
96              
97             sub stream_done {
98 5     5 0 4 my $this = shift;
99 5         10 $this->push_line("
");
100             }
101              
102             sub add_help_types {
103 5     5 0 8 my $this = shift;
104 5         15 $this->use_help_type('keyspecs');
105 5         10 $this->use_help_type('keygroups');
106 5         10 $this->use_help_type('keys');
107             }
108              
109             sub usage {
110 0     0 0   my $this = shift;
111              
112 0           my $options = [
113             ['keys ', 'Keys to print in the table. May be specified multiple times, may be comma separated. Default to all fields in the first record. May be a keyspec or a keygroup, see \'--help-keys\' for more information'],
114             ['noheader', 'Do not print the header row'],
115             ['rowattributes', 'HTML attributes to put on the tr tags'],
116             ['cellattributes', 'HTML attributes to put on the td and th tag'],
117             ];
118              
119 0           my $args_string = $this->options_string($options);
120              
121 0           return <
122             Usage: recs-totable []
123             __FORMAT_TEXT__
124             Prints out an html table for the records from input or from .
125             __FORMAT_TEXT__
126              
127             $args_string
128              
129             Examples:
130             Print all fields
131             recs-tohhtml
132             Print foo and bar fields, without a header
133             recs-tohtml --fields foo,bar --noheader
134             USAGE
135             }
136              
137             1;