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 "yes") {
81 error gettext("empty data");
85 push @lines, defined $params{class}
86 ? "<table class=\"".$params{class}.'">'
88 push @lines, "\t<thead>",
89 genrow($params{page}, $params{destpage}, "th", @$header),
90 "\t</thead>" if defined $header;
91 push @lines, "\t<tbody>" if defined $header;
92 push @lines, genrow($params{page}, $params{destpage}, "td", @$_)
94 push @lines, "\t</tbody>" if defined $header;
95 push @lines, '</table>';
96 my $html = join("\n", @lines);
98 if (exists $params{file}) {
100 htmllink($params{page}, $params{destpage}, $params{file},
101 linktext => gettext('Direct data download'));
108 sub is_dsv_data ($) { #{{{
111 my ($line) = split(/\n/, $text);
112 return $line =~ m{.+\|};
115 sub split_csv ($$) { #{{{
116 my @text_lines = split(/\n/, shift);
117 my $delimiter = shift;
119 eval q{use Text::CSV};
121 my $csv = Text::CSV->new({
122 sep_char => $delimiter,
124 allow_loose_quotes => 1,
125 }) || error("could not create a Text::CSV object");
129 foreach my $line (@text_lines) {
131 if ($csv->parse($line)) {
132 push(@data, [ map { decode_utf8 $_ } $csv->fields() ]);
135 debug(sprintf(gettext('parse fail at line %d: %s'),
136 $l, $csv->error_input()));
143 sub split_dsv ($$) { #{{{
144 my @text_lines = split(/\n/, shift);
145 my $delimiter = shift;
146 $delimiter="|" unless defined $delimiter;
149 foreach my $line (@text_lines) {
150 push @data, [ split(/\Q$delimiter\E/, $line, -1) ];
156 sub genrow ($$$@) { #{{{
158 my $destpage = shift;
163 push @ret, "\t\t<tr>";
164 for (my $x=0; $x < @data; $x++) {
165 my $cell=htmlize($page, $destpage, $data[$x]);
167 while ($x+1 < @data && $data[$x+1] eq '') {
172 push @ret, "\t\t\t<$elt colspan=\"$colspan\">$cell</$elt>"
175 push @ret, "\t\t\t<$elt>$cell</$elt>"
178 push @ret, "\t\t</tr>";
183 sub htmlize ($$$) { #{{{
185 my $destpage = shift;
187 return IkiWiki::htmlize($page, $destpage, pagetype($pagesources{$page}),
188 IkiWiki::preprocess($page, $destpage, shift));