1 package IkiWiki::Plugin::table;
2 # by Victor Moral <victor@taquiones.net>
10 hook(type => "getsetup", id => "table", call => \&getsetup);
11 hook(type => "preprocess", id => "table", call => \&preprocess, scan => 1);
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
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 {
48 page => $params{page},
49 content => $params{data},
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';
62 $params{format} = 'csv';
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
74 IkiWiki::linkify($params{page},
75 $params{destpage}, $_);
79 elsif (lc $params{format} eq 'dsv') {
80 # linkify before parsing since wikilinks can contain the
82 $params{data} = IkiWiki::linkify($params{page},
83 $params{destpage}, $params{data});
84 @data=split_dsv($params{data},
85 defined $params{delimiter} ? $params{delimiter} : "|",);
88 error gettext("unknown data format");
92 if (lc($params{header}) eq "row" || IkiWiki::yesno($params{header})) {
96 error gettext("empty data");
100 push @lines, defined $params{class}
101 ? "<table class=\"".$params{class}.'">'
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}) {
114 htmllink($params{page}, $params{destpage}, $params{file},
115 linktext => gettext('Direct data download'));
122 sub is_dsv_data ($) {
125 my ($line) = split(/\n/, $text);
126 return $line =~ m{.+\|};
130 my @text_lines = split(/\n/, shift);
131 my $delimiter = shift;
133 eval q{use Text::CSV};
135 my $csv = Text::CSV->new({
136 sep_char => $delimiter,
138 allow_loose_quotes => 1,
139 }) || error("could not create a Text::CSV object");
143 foreach my $line (@text_lines) {
145 if ($csv->parse($line)) {
146 push(@data, [ map { decode_utf8 $_ } $csv->fields() ]);
149 debug(sprintf(gettext('parse fail at line %d: %s'),
150 $l, $csv->error_input()));
158 my @text_lines = split(/\n/, shift);
159 my $delimiter = shift;
160 $delimiter="|" unless defined $delimiter;
163 foreach my $line (@text_lines) {
164 push @data, [ split(/\Q$delimiter\E/, $line, -1) ];
171 my %params=%{shift()};
175 my $page=$params{page};
176 my $destpage=$params{destpage};
177 my $type=pagetype($pagesources{$page});
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
187 while ($x+1 < @data && $data[$x+1] eq '') {
192 # check if the first column should be a header
194 if ($x == 0 && lc($params{header}) eq "column") {
199 push @ret, "\t\t\t<$e colspan=\"$colspan\">$cell</$e>"
202 push @ret, "\t\t\t<$e>$cell</$e>"
205 push @ret, "\t\t</tr>";