]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki/Plugin/table.pm
blogspam: New plugin, adding spam filtering for page editing / comment posting using...
[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 3.00;
8
9 sub import {
10         hook(type => "getsetup", id => "table", call => \&getsetup);
11         hook(type => "preprocess", id => "table", call => \&preprocess, scan => 1);
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 (! exists $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 (! defined wantarray) {
38                 # scan mode --  if the table uses an external file, need to
39                 # scan that file too.
40                 return unless exists $params{file};
41
42                 IkiWiki::run_hooks(scan => sub {
43                         shift->(
44                                 page => $params{page},
45                                 content => $params{data},
46                         );
47                 });
48
49                 # Preprocess in scan-only mode.
50                 IkiWiki::preprocess($params{page}, $params{page}, $params{data}, 1);
51
52                 return;
53         }
54
55         if (lc $params{format} eq 'auto') {
56                 # first try the more simple format
57                 if (is_dsv_data($params{data})) {
58                         $params{format} = 'dsv';
59                 }
60                 else {
61                         $params{format} = 'csv';
62                 }
63         }
64
65         my @data;
66         if (lc $params{format} eq 'csv') {
67                 @data=split_csv($params{data},
68                         defined $params{delimiter} ? $params{delimiter} : ",",);
69                 # linkify after parsing since html link quoting can
70                 # confuse CSV parsing
71                 @data=map {
72                         [ map {
73                                 IkiWiki::linkify($params{page},
74                                         $params{destpage}, $_);
75                         } @$_ ]
76                 } @data;
77         }
78         elsif (lc $params{format} eq 'dsv') {
79                 # linkify before parsing since wikilinks can contain the
80                 # delimiter
81                 $params{data} = IkiWiki::linkify($params{page},
82                         $params{destpage}, $params{data});
83                 @data=split_dsv($params{data},
84                         defined $params{delimiter} ? $params{delimiter} : "|",);
85         }
86         else {
87                 error gettext("unknown data format");
88         }
89
90         my $header;
91         if (lc($params{header}) eq "row" || IkiWiki::yesno($params{header})) {
92                 $header=shift @data;
93         }
94         if (! @data) {
95                 error gettext("empty data");
96         }
97
98         my @lines;
99         push @lines, defined $params{class}
100                         ? "<table class=\"".$params{class}.'">'
101                         : '<table>';
102         push @lines, "\t<thead>",
103                 genrow(\%params, "th", @$header),
104                 "\t</thead>" if defined $header;
105         push @lines, "\t<tbody>" if defined $header;
106         push @lines, genrow(\%params, "td", @$_) foreach @data;
107         push @lines, "\t</tbody>" if defined $header;
108         push @lines, '</table>';
109         my $html = join("\n", @lines);
110
111         if (exists $params{file}) {
112                 return $html."\n\n".
113                         htmllink($params{page}, $params{destpage}, $params{file},
114                                 linktext => gettext('Direct data download'));
115         }
116         else {  
117                 return $html;
118         }            
119 }
120
121 sub is_dsv_data ($) {
122         my $text = shift;
123
124         my ($line) = split(/\n/, $text);
125         return $line =~ m{.+\|};
126 }
127
128 sub split_csv ($$) {
129         my @text_lines = split(/\n/, shift);
130         my $delimiter = shift;
131
132         eval q{use Text::CSV};
133         error($@) if $@;
134         my $csv = Text::CSV->new({ 
135                 sep_char        => $delimiter,
136                 binary          => 1,
137                 allow_loose_quotes => 1,
138         }) || error("could not create a Text::CSV object");
139         
140         my $l=0;
141         my @data;
142         foreach my $line (@text_lines) {
143                 $l++;
144                 if ($csv->parse($line)) {
145                         push(@data, [ map { decode_utf8 $_ } $csv->fields() ]);
146                 }
147                 else {
148                         debug(sprintf(gettext('parse fail at line %d: %s'), 
149                                 $l, $csv->error_input()));
150                 }
151         }
152
153         return @data;
154 }
155
156 sub split_dsv ($$) {
157         my @text_lines = split(/\n/, shift);
158         my $delimiter = shift;
159         $delimiter="|" unless defined $delimiter;
160
161         my @data;
162         foreach my $line (@text_lines) {
163                 push @data, [ split(/\Q$delimiter\E/, $line, -1) ];
164         }
165     
166         return @data;
167 }
168
169 sub genrow ($@) {
170         my %params=%{shift()};
171         my $elt = shift;
172         my @data = @_;
173
174         my $page=$params{page};
175         my $destpage=$params{destpage};
176         my $type=pagetype($pagesources{$page});
177
178         my @ret;
179         push @ret, "\t\t<tr>";
180         for (my $x=0; $x < @data; $x++) {
181                 my $cell=IkiWiki::htmlize($page, $destpage, $type,
182                          IkiWiki::preprocess($page, $destpage, $data[$x]));
183
184                 # automatic colspan for empty cells
185                 my $colspan=1;
186                 while ($x+1 < @data && $data[$x+1] eq '') {
187                         $x++;
188                         $colspan++;
189                 }
190
191                 # check if the first column should be a header
192                 my $e=$elt;
193                 if ($x == 0 && lc($params{header}) eq "column") {
194                         $e="th";
195                 }
196
197                 if ($colspan > 1) {
198                         push @ret, "\t\t\t<$e colspan=\"$colspan\">$cell</$e>"
199                 }
200                 else {
201                         push @ret, "\t\t\t<$e>$cell</$e>"
202                 }
203         }
204         push @ret, "\t\t</tr>";
205
206         return @ret;
207 }
208
209 1