]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki/Plugin/table.pm
po plugin: build %translations in needsbuild hook rather than scan
[ikiwiki.git] / IkiWiki / Plugin / table.pm
1 package IkiWiki::Plugin::table;
2 # by Victor Moral <victor@taquiones.net>
3
4 use warnings;
5 use strict;
6 use Encode;
7 use IkiWiki 2.00;
8
9 sub import { #{{{
10         hook(type => "getsetup", id => "table", call => \&getsetup);
11         hook(type => "preprocess", id => "table", call => \&preprocess);
12 } # }}}
13
14 sub getsetup () { #{{{
15         return
16                 plugin => {
17                         safe => 1,
18                         rebuild => undef,
19                 },
20 } #}}}
21
22 sub preprocess (@) { #{{{
23         my %params =(
24                 format  => 'auto',
25                 header  => 'row',
26                 @_
27         );
28
29         if (exists $params{file}) {
30                 if (! $pagesources{$params{file}}) {
31                         error gettext("cannot find file");
32                 }
33                 $params{data} = readfile(srcfile($params{file}));
34                 add_depends($params{page}, $params{file});
35         }
36
37         if (lc $params{format} eq 'auto') {
38                 # first try the more simple format
39                 if (is_dsv_data($params{data})) {
40                         $params{format} = 'dsv';
41                 }
42                 else {
43                         $params{format} = 'csv';
44                 }
45         }
46
47         my @data;
48         if (lc $params{format} eq 'csv') {
49                 @data=split_csv($params{data},
50                         defined $params{delimiter} ? $params{delimiter} : ",",);
51                 # linkify after parsing since html link quoting can
52                 # confuse CSV parsing
53                 if (! exists $params{file}) {
54                         @data=map {
55                                 [ map {
56                                         IkiWiki::linkify($params{page},
57                                                 $params{destpage}, $_);
58                                 } @$_ ]
59                         } @data;
60                 }
61         }
62         elsif (lc $params{format} eq 'dsv') {
63                 # linkify before parsing since wikilinks can contain the
64                 # delimiter
65                 if (! exists $params{file}) {
66                         $params{data} = IkiWiki::linkify($params{page},
67                                 $params{destpage}, $params{data});
68                 }
69                 @data=split_dsv($params{data},
70                         defined $params{delimiter} ? $params{delimiter} : "|",);
71         }
72         else {
73                 error gettext("unknown data format");
74         }
75
76         my $header;
77         if (lc($params{header}) eq "row" || IkiWiki::yesno($params{header})) {
78                 $header=shift @data;
79         }
80         if (! @data) {
81                 error gettext("empty data");
82         }
83
84         my @lines;
85         push @lines, defined $params{class}
86                         ? "<table class=\"".$params{class}.'">'
87                         : '<table>';
88         push @lines, "\t<thead>",
89                 genrow(\%params, "th", @$header),
90                 "\t</thead>" if defined $header;
91         push @lines, "\t<tbody>" if defined $header;
92         push @lines, genrow(\%params, "td", @$_) foreach @data;
93         push @lines, "\t</tbody>" if defined $header;
94         push @lines, '</table>';
95         my $html = join("\n", @lines);
96
97         if (exists $params{file}) {
98                 return $html."\n\n".
99                         htmllink($params{page}, $params{destpage}, $params{file},
100                                 linktext => gettext('Direct data download'));
101         }
102         else {  
103                 return $html;
104         }            
105 } #}}}
106
107 sub is_dsv_data ($) { #{{{
108         my $text = shift;
109
110         my ($line) = split(/\n/, $text);
111         return $line =~ m{.+\|};
112 }
113
114 sub split_csv ($$) { #{{{
115         my @text_lines = split(/\n/, shift);
116         my $delimiter = shift;
117
118         eval q{use Text::CSV};
119         error($@) if $@;
120         my $csv = Text::CSV->new({ 
121                 sep_char        => $delimiter,
122                 binary          => 1,
123                 allow_loose_quotes => 1,
124         }) || error("could not create a Text::CSV object");
125         
126         my $l=0;
127         my @data;
128         foreach my $line (@text_lines) {
129                 $l++;
130                 if ($csv->parse($line)) {
131                         push(@data, [ map { decode_utf8 $_ } $csv->fields() ]);
132                 }
133                 else {
134                         debug(sprintf(gettext('parse fail at line %d: %s'), 
135                                 $l, $csv->error_input()));
136                 }
137         }
138
139         return @data;
140 } #}}}
141
142 sub split_dsv ($$) { #{{{
143         my @text_lines = split(/\n/, shift);
144         my $delimiter = shift;
145         $delimiter="|" unless defined $delimiter;
146
147         my @data;
148         foreach my $line (@text_lines) {
149                 push @data, [ split(/\Q$delimiter\E/, $line, -1) ];
150         }
151     
152         return @data;
153 } #}}}
154
155 sub genrow ($@) { #{{{
156         my %params=%{shift()};
157         my $elt = shift;
158         my @data = @_;
159
160         my $page=$params{page};
161         my $destpage=$params{destpage};
162         my $type=pagetype($pagesources{$page});
163
164         my @ret;
165         push @ret, "\t\t<tr>";
166         for (my $x=0; $x < @data; $x++) {
167                 my $cell=IkiWiki::htmlize($page, $destpage, $type,
168                          IkiWiki::preprocess($page, $destpage, $data[$x]));
169
170                 # automatic colspan for empty cells
171                 my $colspan=1;
172                 while ($x+1 < @data && $data[$x+1] eq '') {
173                         $x++;
174                         $colspan++;
175                 }
176
177                 # check if the first column should be a header
178                 my $e=$elt;
179                 if ($x == 0 && lc($params{header}) eq "column") {
180                         $e="th";
181                 }
182
183                 if ($colspan > 1) {
184                         push @ret, "\t\t\t<$e colspan=\"$colspan\">$cell</$e>"
185                 }
186                 else {
187                         push @ret, "\t\t\t<$e>$cell</$e>"
188                 }
189         }
190         push @ret, "\t\t</tr>";
191
192         return @ret;
193 } #}}}
194
195 1