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);
29 if (exists $params{file}) {
30 if (! $pagesources{$params{file}}) {
31 error gettext("cannot find file");
33 $params{data} = readfile(srcfile($params{file}));
34 add_depends($params{page}, $params{file});
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';
43 $params{format} = 'csv';
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
55 IkiWiki::linkify($params{page},
56 $params{destpage}, $_);
60 elsif (lc $params{format} eq 'dsv') {
61 # linkify before parsing since wikilinks can contain the
63 $params{data} = IkiWiki::linkify($params{page},
64 $params{destpage}, $params{data});
65 @data=split_dsv($params{data},
66 defined $params{delimiter} ? $params{delimiter} : "|",);
69 error gettext("unknown data format");
73 if (lc($params{header}) eq "row" || IkiWiki::yesno($params{header})) {
77 error gettext("empty data");
81 push @lines, defined $params{class}
82 ? "<table class=\"".$params{class}.'">'
84 push @lines, "\t<thead>",
85 genrow(\%params, "th", @$header),
86 "\t</thead>" if defined $header;
87 push @lines, "\t<tbody>" if defined $header;
88 push @lines, genrow(\%params, "td", @$_) foreach @data;
89 push @lines, "\t</tbody>" if defined $header;
90 push @lines, '</table>';
91 my $html = join("\n", @lines);
93 if (exists $params{file}) {
95 htmllink($params{page}, $params{destpage}, $params{file},
96 linktext => gettext('Direct data download'));
103 sub is_dsv_data ($) {
106 my ($line) = split(/\n/, $text);
107 return $line =~ m{.+\|};
111 my @text_lines = split(/\n/, shift);
112 my $delimiter = shift;
114 eval q{use Text::CSV};
116 my $csv = Text::CSV->new({
117 sep_char => $delimiter,
119 allow_loose_quotes => 1,
120 }) || error("could not create a Text::CSV object");
124 foreach my $line (@text_lines) {
126 if ($csv->parse($line)) {
127 push(@data, [ map { decode_utf8 $_ } $csv->fields() ]);
130 debug(sprintf(gettext('parse fail at line %d: %s'),
131 $l, $csv->error_input()));
139 my @text_lines = split(/\n/, shift);
140 my $delimiter = shift;
141 $delimiter="|" unless defined $delimiter;
144 foreach my $line (@text_lines) {
145 push @data, [ split(/\Q$delimiter\E/, $line, -1) ];
152 my %params=%{shift()};
156 my $page=$params{page};
157 my $destpage=$params{destpage};
158 my $type=pagetype($pagesources{$page});
161 push @ret, "\t\t<tr>";
162 for (my $x=0; $x < @data; $x++) {
163 my $cell=IkiWiki::htmlize($page, $destpage, $type,
164 IkiWiki::preprocess($page, $destpage, $data[$x]));
166 # automatic colspan for empty cells
168 while ($x+1 < @data && $data[$x+1] eq '') {
173 # check if the first column should be a header
175 if ($x == 0 && lc($params{header}) eq "column") {
180 push @ret, "\t\t\t<$e colspan=\"$colspan\">$cell</$e>"
183 push @ret, "\t\t\t<$e>$cell</$e>"
186 push @ret, "\t\t</tr>";