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);
14 sub getsetup () { #{{{
22 sub 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
53 if (! exists $params{file}) {
56 IkiWiki::linkify($params{page},
57 $params{destpage}, $_);
62 elsif (lc $params{format} eq 'dsv') {
63 # linkify before parsing since wikilinks can contain the
65 if (! exists $params{file}) {
66 $params{data} = IkiWiki::linkify($params{page},
67 $params{destpage}, $params{data});
69 @data=split_dsv($params{data},
70 defined $params{delimiter} ? $params{delimiter} : "|",);
73 error gettext("unknown data format");
77 if (lc($params{header}) eq "row" || IkiWiki::yesno($params{header})) {
81 error gettext("empty data");
85 push @lines, defined $params{class}
86 ? "<table class=\"".$params{class}.'">'
88 push @lines, "\t<thead>",
89 genrow(\%params, "th", @$header),
90 "\t</thead>" if defined $header;
91 push @lines, "\t<tbody>" if defined $header;
92 push @lines, genrow(\%params, "td", @$_) foreach @data;
93 push @lines, "\t</tbody>" if defined $header;
94 push @lines, '</table>';
95 my $html = join("\n", @lines);
97 if (exists $params{file}) {
99 htmllink($params{page}, $params{destpage}, $params{file},
100 linktext => gettext('Direct data download'));
107 sub is_dsv_data ($) { #{{{
110 my ($line) = split(/\n/, $text);
111 return $line =~ m{.+\|};
114 sub split_csv ($$) { #{{{
115 my @text_lines = split(/\n/, shift);
116 my $delimiter = shift;
118 eval q{use Text::CSV};
120 my $csv = Text::CSV->new({
121 sep_char => $delimiter,
123 allow_loose_quotes => 1,
124 }) || error("could not create a Text::CSV object");
128 foreach my $line (@text_lines) {
130 if ($csv->parse($line)) {
131 push(@data, [ map { decode_utf8 $_ } $csv->fields() ]);
134 debug(sprintf(gettext('parse fail at line %d: %s'),
135 $l, $csv->error_input()));
142 sub split_dsv ($$) { #{{{
143 my @text_lines = split(/\n/, shift);
144 my $delimiter = shift;
145 $delimiter="|" unless defined $delimiter;
148 foreach my $line (@text_lines) {
149 push @data, [ split(/\Q$delimiter\E/, $line, -1) ];
155 sub genrow ($@) { #{{{
156 my %params=%{shift()};
160 my $page=$params{page};
161 my $destpage=$params{destpage};
162 my $type=pagetype($pagesources{$page});
165 push @ret, "\t\t<tr>";
166 for (my $x=0; $x < @data; $x++) {
167 my $cell=IkiWiki::htmlize($page, $destpage, $type,
168 IkiWiki::preprocess($page, $destpage, $data[$x]));
170 # automatic colspan for empty cells
172 while ($x+1 < @data && $data[$x+1] eq '') {
177 # check if the first column should be a header
179 if ($x == 0 && lc($params{header}) eq "column") {
184 push @ret, "\t\t\t<$e colspan=\"$colspan\">$cell</$e>"
187 push @ret, "\t\t\t<$e>$cell</$e>"
190 push @ret, "\t\t</tr>";