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);
29 if (exists $params{file}) {
30 if (! exists $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 (! defined wantarray) {
38 # scan mode -- if the table uses an external file, need to
40 return unless exists $params{file};
42 IkiWiki::run_hooks(scan => sub {
44 page => $params{page},
45 content => $params{data},
49 # Preprocess in scan-only mode.
50 IkiWiki::preprocess($params{page}, $params{page}, $params{data}, 1);
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';
61 $params{format} = 'csv';
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
73 IkiWiki::linkify($params{page},
74 $params{destpage}, $_);
78 elsif (lc $params{format} eq 'dsv') {
79 # linkify before parsing since wikilinks can contain the
81 $params{data} = IkiWiki::linkify($params{page},
82 $params{destpage}, $params{data});
83 @data=split_dsv($params{data},
84 defined $params{delimiter} ? $params{delimiter} : "|",);
87 error gettext("unknown data format");
91 if (lc($params{header}) eq "row" || IkiWiki::yesno($params{header})) {
95 error gettext("empty data");
99 push @lines, defined $params{class}
100 ? "<table class=\"".$params{class}.'">'
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);
111 if (exists $params{file}) {
113 htmllink($params{page}, $params{destpage}, $params{file},
114 linktext => gettext('Direct data download'));
121 sub is_dsv_data ($) {
124 my ($line) = split(/\n/, $text);
125 return $line =~ m{.+\|};
129 my @text_lines = split(/\n/, shift);
130 my $delimiter = shift;
132 eval q{use Text::CSV};
134 my $csv = Text::CSV->new({
135 sep_char => $delimiter,
137 allow_loose_quotes => 1,
138 }) || error("could not create a Text::CSV object");
142 foreach my $line (@text_lines) {
144 if ($csv->parse($line)) {
145 push(@data, [ map { decode_utf8 $_ } $csv->fields() ]);
148 debug(sprintf(gettext('parse fail at line %d: %s'),
149 $l, $csv->error_input()));
157 my @text_lines = split(/\n/, shift);
158 my $delimiter = shift;
159 $delimiter="|" unless defined $delimiter;
162 foreach my $line (@text_lines) {
163 push @data, [ split(/\Q$delimiter\E/, $line, -1) ];
170 my %params=%{shift()};
174 my $page=$params{page};
175 my $destpage=$params{destpage};
176 my $type=pagetype($pagesources{$page});
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]));
184 # automatic colspan for empty cells
186 while ($x+1 < @data && $data[$x+1] eq '') {
191 # check if the first column should be a header
193 if ($x == 0 && lc($params{header}) eq "column") {
198 push @ret, "\t\t\t<$e colspan=\"$colspan\">$cell</$e>"
201 push @ret, "\t\t\t<$e>$cell</$e>"
204 push @ret, "\t\t</tr>";