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,
139 allow_loose_quotes => 1,
140 }) || error("could not create a Text::CSV object");
144 foreach my $line (@text_lines) {
146 if ($csv->parse($line)) {
147 push(@data, [ $csv->fields() ]);
150 debug(sprintf(gettext('parse fail at line %d: %s'),
151 $l, $csv->error_input()));
159 my @text_lines = split(/\n/, shift);
160 my $delimiter = shift;
161 $delimiter="|" unless defined $delimiter;
164 foreach my $line (@text_lines) {
165 push @data, [ split(/\Q$delimiter\E/, $line, -1) ];
172 my %params=%{shift()};
176 my $page=$params{page};
177 my $destpage=$params{destpage};
178 my $type=pagetype($pagesources{$page});
181 push @ret, "\t\t<tr>";
182 for (my $x=0; $x < @data; $x++) {
183 my $cell=IkiWiki::htmlize($page, $destpage, $type,
184 IkiWiki::preprocess($page, $destpage, $data[$x]));
186 # automatic colspan for empty cells
188 while ($x+1 < @data && $data[$x+1] eq '') {
193 # check if the first column should be a header
195 if ($x == 0 && lc($params{header}) eq "column") {
200 push @ret, "\t\t\t<$e colspan=\"$colspan\">$cell</$e>"
203 push @ret, "\t\t\t<$e>$cell</$e>"
206 push @ret, "\t\t</tr>";