| 1 | package Muster::Hook::Table; | ||||||||||||||||||||||||||||
| 2 | 1 | $Muster::Hook::Table::VERSION = '0.96'; | |||||||||||||||||||||||||||
| 3 | 1 | ▶ | use Mojo::Base 'Muster::Hook::Directives'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use Muster::LeafFile; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use Muster::Hooks; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use Encode; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use Carp 'croak'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | =head1 NAME | ||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | Muster::Hook::Table - Muster table directive. | ||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | =head1 VERSION | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | version 0.96 | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | L<Muster::Hook::Table> simple table formatting. | ||||||||||||||||||||||||||||
| 21 | This does not implement the data-from-file which the IkiWiki plugin does. | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | =head1 METHODS | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | L<Muster::Hook::Table> inherits all methods from L<Muster::Hook::Directives>. | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | =head2 register | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | Do some intialization. | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | =cut | ||||||||||||||||||||||||||||
| 32 | 0 | ▶ | sub register { | ||||||||||||||||||||||||||
|
Subroutine
register: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 33 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 34 | 0 | my $hookmaster = shift; | |||||||||||||||||||||||||||
| 35 | 0 | my $conf = shift; | |||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 0 | $self->{metadb} = $hookmaster->{metadb}; | |||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 0 | ▶ | my $callback = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 40 | 0 | my %args = @_; | |||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 0 | return $self->process(%args); | |||||||||||||||||||||||||||
| 43 | 0 | }; | |||||||||||||||||||||||||||
| 44 | 0 | ▶ | $hookmaster->add_hook('table' => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 45 | 0 | my %args = @_; | |||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 0 | return $self->do_directives( | |||||||||||||||||||||||||||
| 48 | no_scan=>1, | ||||||||||||||||||||||||||||
| 49 | directive=>'table', | ||||||||||||||||||||||||||||
| 50 | call=>$callback, | ||||||||||||||||||||||||||||
| 51 | %args, | ||||||||||||||||||||||||||||
| 52 | ); | ||||||||||||||||||||||||||||
| 53 | }, | ||||||||||||||||||||||||||||
| 54 | 0 | ); | |||||||||||||||||||||||||||
| 55 | 0 | return $self; | |||||||||||||||||||||||||||
| 56 | } # register | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | =head2 process | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | Process table formatting. | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | =cut | ||||||||||||||||||||||||||||
| 63 | 0 | ▶ | sub process { | ||||||||||||||||||||||||||
|
Subroutine
process: not called CC=15 SCAR=54.8
Pod
documented
| |||||||||||||||||||||||||||||
| 64 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 65 | 0 | my %args = @_; | |||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 0 | my $leaf = $args{leaf}; | |||||||||||||||||||||||||||
| 68 | 0 | my $phase = $args{phase}; | |||||||||||||||||||||||||||
| 69 | 0 | my @p = @{$args{params}}; | |||||||||||||||||||||||||||
| 70 | 0 | my %params = ( | |||||||||||||||||||||||||||
| 71 | format => 'auto', | ||||||||||||||||||||||||||||
| 72 | header => 'row', | ||||||||||||||||||||||||||||
| 73 | @p | ||||||||||||||||||||||||||||
| 74 | ); | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | # code from IkiWiki | ||||||||||||||||||||||||||||
| 77 | 0 | ▶ | if (lc $params{format} eq 'auto') | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 78 | { | ||||||||||||||||||||||||||||
| 79 | # first try the more simple format | ||||||||||||||||||||||||||||
| 80 | 0 | ▶ | if (is_dsv_data($params{data})) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 81 | { | ||||||||||||||||||||||||||||
| 82 | 0 | $params{format} = 'dsv'; | |||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | else { | ||||||||||||||||||||||||||||
| 85 | 0 | $params{format} = 'csv'; | |||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | 0 | my @data; | |||||||||||||||||||||||||||
| 89 | 0 | ▶ | if (lc $params{format} eq 'csv') | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 90 | { | ||||||||||||||||||||||||||||
| 91 | @data=split_csv($params{data}, | ||||||||||||||||||||||||||||
| 92 | 0 | ▶ | defined $params{delimiter} ? $params{delimiter} : ",",); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||
| 94 | elsif (lc $params{format} eq 'dsv') | ||||||||||||||||||||||||||||
| 95 | { | ||||||||||||||||||||||||||||
| 96 | @data=split_dsv($params{data}, | ||||||||||||||||||||||||||||
| 97 | 0 | ▶ | defined $params{delimiter} ? $params{delimiter} : "|",); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | else | ||||||||||||||||||||||||||||
| 100 | { | ||||||||||||||||||||||||||||
| 101 | 0 | return __PACKAGE__ . " unknown data format"; | |||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | 0 | my $header; | |||||||||||||||||||||||||||
| 105 | 0 | ▶ | if (lc($params{header}) eq "row" || $params{header} =~ /yes|true|1/i) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
lc $params{"header"} eq "row" or $params{"header"} =~ /yes|true|1/iu
MC/DC0%0/2
lc $params{"header"} eq "row" or $params{"header"} =~ /yes|true|1/iu
lc $params{"header"} eq "row"
$params{"header"} =~ /yes|true|1/iu
Truth table0%0/3
A or B
A= lc $params{"header"} eq "row"B= $params{"header"} =~ /yes|true|1/iu
| |||||||||||||||||||||||||||||
| 106 | { | ||||||||||||||||||||||||||||
| 107 | 0 | $header=shift @data; | |||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||
| 109 | 0 | ▶ | if (! @data) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 110 | { | ||||||||||||||||||||||||||||
| 111 | 0 | return __PACKAGE__ . "empty data"; | |||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | 0 | my @lines; | |||||||||||||||||||||||||||
| 114 | 0 | ▶ | push @lines, defined $params{class} ? "<table class=\"".$params{class}.'">' : '<table>'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 115 | 0 | ▶ | push @lines, "\t<thead>", | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 116 | $self->genrow(\%params, "th", @$header), "\t</thead>" if defined $header; | ||||||||||||||||||||||||||||
| 117 | 0 | ▶ | push @lines, "\t<tbody>" if defined $header; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 118 | push @lines, | ||||||||||||||||||||||||||||
| 119 | 0 | $self->genrow(\%params, "td", @$_) foreach @data; | |||||||||||||||||||||||||||
| 120 | 0 | ▶ | push @lines, "\t</tbody>" if defined $header; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 121 | 0 | push @lines, '</table>'; | |||||||||||||||||||||||||||
| 122 | 0 | my $html = join("\n", @lines); | |||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | 0 | return $html; | |||||||||||||||||||||||||||
| 125 | } # process | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | =head2 is_dsv_data | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | Is the data DSV (delimiter-separated-values)? | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | =cut | ||||||||||||||||||||||||||||
| 132 | 0 | ▶ | sub is_dsv_data ($) { | ||||||||||||||||||||||||||
|
Subroutine
is_dsv_data: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 133 | 0 | my $text = shift; | |||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | 0 | my ($line) = split(/\n/, $text); | |||||||||||||||||||||||||||
| 136 | 0 | return $line =~ m{.+\|}; | |||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | =head2 split_csv | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | Use Text::CSV to split the data into lines and columns. | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | =cut | ||||||||||||||||||||||||||||
| 144 | 0 | ▶ | sub split_csv ($$) { | ||||||||||||||||||||||||||
|
Subroutine
split_csv: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||||||||||||||||
| 145 | 0 | my @text_lines = split(/\n/, shift); | |||||||||||||||||||||||||||
| 146 | 0 | my $delimiter = shift; | |||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | 0 | eval q{use Text::CSV}; | |||||||||||||||||||||||||||
| 149 | 0 | ▶ | croak($@) if $@; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 150 | 0 | ▶ | my $csv = Text::CSV->new({ | ||||||||||||||||||||||||||
|
Condition0%0/2
'Text::CSV'->new({'sep_char', $delimiter, 'binary', 1, 'allow_loose_quotes', 1}) || croak('could not create a Text::CSV object')
MC/DC0%0/1
'Text::CSV'->new({'sep_char', $delimiter, 'binary', 1, 'allow_loose_quotes', 1}) || croak('could not create a Text::CSV object')
'Text::CSV'->new({'sep_char', $delimiter, 'binary', 1, 'allow_loose_quotes', 1})
Truth table0%0/2
A
A= 'Text::CSV'->new({'sep_char', $delimiter, 'binary', 1, 'allow_loose_quotes', 1})
| |||||||||||||||||||||||||||||
| 151 | sep_char => $delimiter, | ||||||||||||||||||||||||||||
| 152 | binary => 1, | ||||||||||||||||||||||||||||
| 153 | allow_loose_quotes => 1, | ||||||||||||||||||||||||||||
| 154 | }) || croak ("could not create a Text::CSV object"); | ||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | 0 | my $l=0; | |||||||||||||||||||||||||||
| 157 | 0 | my @data; | |||||||||||||||||||||||||||
| 158 | 0 | foreach my $line (@text_lines) | |||||||||||||||||||||||||||
| 159 | { | ||||||||||||||||||||||||||||
| 160 | 0 | $l++; | |||||||||||||||||||||||||||
| 161 | 0 | ▶ | if ($csv->parse($line)) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 162 | { | ||||||||||||||||||||||||||||
| 163 | 0 | push(@data, [ map { decode_utf8 $_ } $csv->fields() ]); | |||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||
| 165 | else | ||||||||||||||||||||||||||||
| 166 | { | ||||||||||||||||||||||||||||
| 167 | 0 | warn __PACKAGE__, sprintf('parse fail at line %d: %s', | |||||||||||||||||||||||||||
| 168 | $l, $csv->error_input()); | ||||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||
| 170 | } | ||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | 0 | return @data; | |||||||||||||||||||||||||||
| 173 | } | ||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | =head2 split_dsv | ||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | Split the data into lines and columns using delimiters. | ||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | =cut | ||||||||||||||||||||||||||||
| 180 | 0 | ▶ | sub split_dsv ($$) { | ||||||||||||||||||||||||||
|
Subroutine
split_dsv: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 181 | 0 | my @text_lines = split(/\n/, shift); | |||||||||||||||||||||||||||
| 182 | 0 | my $delimiter = shift; | |||||||||||||||||||||||||||
| 183 | 0 | ▶ | $delimiter="|" unless defined $delimiter; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | 0 | my @data; | |||||||||||||||||||||||||||
| 186 | 0 | foreach my $line (@text_lines) | |||||||||||||||||||||||||||
| 187 | { | ||||||||||||||||||||||||||||
| 188 | 0 | push @data, [ split(/\Q$delimiter\E/, $line, -1) ]; | |||||||||||||||||||||||||||
| 189 | } | ||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | 0 | return @data; | |||||||||||||||||||||||||||
| 192 | } | ||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | =head2 genrow | ||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | Generate a row of the table. | ||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||
| 198 | =cut | ||||||||||||||||||||||||||||
| 199 | 0 | ▶ | sub genrow ($@) { | ||||||||||||||||||||||||||
|
Subroutine
genrow: not called CC=7 SCAR=40.3
Pod
documented
| |||||||||||||||||||||||||||||
| 200 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 201 | 0 | my %params=%{shift()}; | |||||||||||||||||||||||||||
| 202 | 0 | my $elt = shift; | |||||||||||||||||||||||||||
| 203 | 0 | my @data = @_; | |||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||
| 205 | 0 | my $page=$params{page}; | |||||||||||||||||||||||||||
| 206 | 0 | my $destpage=$params{destpage}; | |||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||
| 208 | 0 | my @ret; | |||||||||||||||||||||||||||
| 209 | 0 | push @ret, "\t\t<tr>"; | |||||||||||||||||||||||||||
| 210 | 0 | ▶ | for (my $x=0; $x < @data; $x++) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 211 | { | ||||||||||||||||||||||||||||
| 212 | 0 | my $cell = $data[$x]; | |||||||||||||||||||||||||||
| 213 | ##my $cell=IkiWiki::htmlize($page, $destpage, $type, | ||||||||||||||||||||||||||||
| 214 | ## IkiWiki::preprocess($page, $destpage, $data[$x])); | ||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||
| 216 | # automatic colspan for empty cells | ||||||||||||||||||||||||||||
| 217 | 0 | my $colspan=1; | |||||||||||||||||||||||||||
| 218 | 0 | ▶ | while ($x+1 < @data && $data[$x+1] eq '') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$x + 1 < @data and $data[$x + 1] eq ""
MC/DC0%0/2
$x + 1 < @data and $data[$x + 1] eq ""
$x + 1 < @data
$data[$x + 1] eq ""
Truth table0%0/3
A and B
A= $x + 1 < @dataB= $data[$x + 1] eq ""
| |||||||||||||||||||||||||||||
| 219 | 0 | $x++; | |||||||||||||||||||||||||||
| 220 | 0 | $colspan++; | |||||||||||||||||||||||||||
| 221 | } | ||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||
| 223 | # check if the first column should be a header | ||||||||||||||||||||||||||||
| 224 | 0 | my $e=$elt; | |||||||||||||||||||||||||||
| 225 | 0 | ▶ | if ($x == 0 && lc($params{header}) eq "column") { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$x == 0 and lc $params{"header"} eq "column"
MC/DC0%0/2
$x == 0 and lc $params{"header"} eq "column"
$x == 0
lc $params{"header"} eq "column"
Truth table0%0/3
A and B
A= $x == 0B= lc $params{"header"} eq "column"
| |||||||||||||||||||||||||||||
| 226 | 0 | $e="th"; | |||||||||||||||||||||||||||
| 227 | } | ||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||
| 229 | 0 | ▶ | if ($colspan > 1) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 230 | 0 | push @ret, "\t\t\t<$e colspan=\"$colspan\">$cell</$e>" | |||||||||||||||||||||||||||
| 231 | } | ||||||||||||||||||||||||||||
| 232 | else { | ||||||||||||||||||||||||||||
| 233 | 0 | push @ret, "\t\t\t<$e>$cell</$e>" | |||||||||||||||||||||||||||
| 234 | } | ||||||||||||||||||||||||||||
| 235 | } | ||||||||||||||||||||||||||||
| 236 | 0 | push @ret, "\t\t</tr>"; | |||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||
| 238 | 0 | return @ret; | |||||||||||||||||||||||||||
| 239 | } | ||||||||||||||||||||||||||||
| 240 | 1 | 1; | |||||||||||||||||||||||||||