Merge branch 'master' of ssh://git.ikiwiki.info/srv/git/ikiwiki.info
[ikiwiki.git] / IkiWiki / Plugin / table.pm
blobf3c425a37dacd9d1389f0c53a0d7fbc52469c70b
1 package IkiWiki::Plugin::table;
2 # by Victor Moral <victor@taquiones.net>
4 use warnings;
5 use strict;
6 use Encode;
7 use IkiWiki 3.00;
9 sub import {
10 hook(type => "getsetup", id => "table", call => \&getsetup);
11 hook(type => "preprocess", id => "table", call => \&preprocess, scan => 1);
14 sub getsetup () {
15 return
16 plugin => {
17 safe => 1,
18 rebuild => undef,
19 section => "widget",
23 sub preprocess (@) {
24 my %params =(
25 format => 'auto',
26 header => 'row',
30 if (exists $params{file}) {
31 if (! exists $pagesources{$params{file}}) {
32 error gettext("cannot find file");
34 $params{data} = readfile(srcfile($params{file}));
35 add_depends($params{page}, $params{file});
38 if (! defined wantarray) {
39 # scan mode -- if the table uses an external file, need to
40 # scan that file too.
41 return unless exists $params{file};
43 # Preprocess in scan-only mode.
44 IkiWiki::preprocess($params{page}, $params{page}, $params{data}, 1);
46 IkiWiki::run_hooks(scan => sub {
47 shift->(
48 page => $params{page},
49 content => $params{data},
51 });
53 return;
56 if (lc $params{format} eq 'auto') {
57 # first try the more simple format
58 if (is_dsv_data($params{data})) {
59 $params{format} = 'dsv';
61 else {
62 $params{format} = 'csv';
66 my @data;
67 if (lc $params{format} eq 'csv') {
68 @data=split_csv($params{data},
69 defined $params{delimiter} ? $params{delimiter} : ",",);
70 # linkify after parsing since html link quoting can
71 # confuse CSV parsing
72 @data=map {
73 [ map {
74 IkiWiki::linkify($params{page},
75 $params{destpage}, $_);
76 } @$_ ]
77 } @data;
79 elsif (lc $params{format} eq 'dsv') {
80 # linkify before parsing since wikilinks can contain the
81 # delimiter
82 $params{data} = IkiWiki::linkify($params{page},
83 $params{destpage}, $params{data});
84 @data=split_dsv($params{data},
85 defined $params{delimiter} ? $params{delimiter} : "|",);
87 else {
88 error gettext("unknown data format");
91 my $header;
92 if (lc($params{header}) eq "row" || IkiWiki::yesno($params{header})) {
93 $header=shift @data;
95 if (! @data) {
96 error gettext("empty data");
99 my @lines;
100 push @lines, defined $params{class}
101 ? "<table class=\"".$params{class}.'">'
102 : '<table>';
103 push @lines, "\t<thead>",
104 genrow(\%params, "th", @$header),
105 "\t</thead>" if defined $header;
106 push @lines, "\t<tbody>" if defined $header;
107 push @lines, genrow(\%params, "td", @$_) foreach @data;
108 push @lines, "\t</tbody>" if defined $header;
109 push @lines, '</table>';
110 my $html = join("\n", @lines);
112 if (exists $params{file}) {
113 return $html."\n\n".
114 htmllink($params{page}, $params{destpage}, $params{file},
115 linktext => gettext('Direct data download'));
117 else {
118 return $html;
122 sub is_dsv_data ($) {
123 my $text = shift;
125 my ($line) = split(/\n/, $text);
126 return $line =~ m{.+\|};
129 sub split_csv ($$) {
130 my @text_lines = split(/\n/, shift);
131 my $delimiter = shift;
133 eval q{use Text::CSV};
134 error($@) if $@;
135 my $csv = Text::CSV->new({
136 sep_char => $delimiter,
137 binary => 1,
138 allow_loose_quotes => 1,
139 }) || error("could not create a Text::CSV object");
141 my $l=0;
142 my @data;
143 foreach my $line (@text_lines) {
144 $l++;
145 if ($csv->parse($line)) {
146 push(@data, [ map { decode_utf8 $_ } $csv->fields() ]);
148 else {
149 debug(sprintf(gettext('parse fail at line %d: %s'),
150 $l, $csv->error_input()));
154 return @data;
157 sub split_dsv ($$) {
158 my @text_lines = split(/\n/, shift);
159 my $delimiter = shift;
160 $delimiter="|" unless defined $delimiter;
162 my @data;
163 foreach my $line (@text_lines) {
164 push @data, [ split(/\Q$delimiter\E/, $line, -1) ];
167 return @data;
170 sub genrow ($@) {
171 my %params=%{shift()};
172 my $elt = shift;
173 my @data = @_;
175 my $page=$params{page};
176 my $destpage=$params{destpage};
177 my $type=pagetype($pagesources{$page});
179 my @ret;
180 push @ret, "\t\t<tr>";
181 for (my $x=0; $x < @data; $x++) {
182 my $cell=IkiWiki::htmlize($page, $destpage, $type,
183 IkiWiki::preprocess($page, $destpage, $data[$x]));
185 # automatic colspan for empty cells
186 my $colspan=1;
187 while ($x+1 < @data && $data[$x+1] eq '') {
188 $x++;
189 $colspan++;
192 # check if the first column should be a header
193 my $e=$elt;
194 if ($x == 0 && lc($params{header}) eq "column") {
195 $e="th";
198 if ($colspan > 1) {
199 push @ret, "\t\t\t<$e colspan=\"$colspan\">$cell</$e>"
201 else {
202 push @ret, "\t\t\t<$e>$cell</$e>"
205 push @ret, "\t\t</tr>";
207 return @ret;