X-Git-Url: http://git.vanrenterghem.biz/git.ikiwiki.info.git/blobdiff_plain/5d57a8436f2c41687cf047abf0cbd31af61de83c..37a4cf13744ee6e96338409fe1e35debeee91f4d:/IkiWiki/Plugin/table.pm diff --git a/IkiWiki/Plugin/table.pm b/IkiWiki/Plugin/table.pm index e4d987dd3..2edd1eacd 100644 --- a/IkiWiki/Plugin/table.pm +++ b/IkiWiki/Plugin/table.pm @@ -3,75 +3,113 @@ package IkiWiki::Plugin::table; use warnings; use strict; +use Encode; +use IkiWiki 3.00; -use IkiWiki; -use IkiWiki::Plugin::mdwn; - -my %defaults = ( - data => undef, - file => undef, - format => 'auto', - sep_char => { - 'csv' => ',', - 'dsv' => '\|', - }, - class => undef, - header => 1, -); - -sub import { #{{{ - hook(type => "preprocess", id => "table", call => \&preprocess); -} # }}} - -sub preprocess (@) { #{{{ - my %params = (%defaults, @_); - - if (defined $params{delimiter}) { - $params{sep_char}->{$params{format}} = $params{delimiter}; - } - if (defined $params{file}) { - if (! $pagesources{$params{file}}) { - return "[[table cannot find file]]"; +sub import { + hook(type => "getsetup", id => "table", call => \&getsetup); + hook(type => "preprocess", id => "table", call => \&preprocess, scan => 1); +} + +sub getsetup () { + return + plugin => { + safe => 1, + rebuild => undef, + section => "widget", + }, +} + +sub preprocess (@) { + my %params =( + format => 'auto', + header => 'row', + @_ + ); + + if (exists $params{file}) { + if (! exists $pagesources{$params{file}}) { + error gettext("cannot find file"); } $params{data} = readfile(srcfile($params{file})); + add_depends($params{page}, $params{file}); + } + + if (! defined wantarray) { + # scan mode -- if the table uses an external file, need to + # scan that file too. + return unless exists $params{file}; + + IkiWiki::run_hooks(scan => sub { + shift->( + page => $params{page}, + content => $params{data}, + ); + }); + + # Preprocess in scan-only mode. + IkiWiki::preprocess($params{page}, $params{page}, $params{data}, 1); + + return; } if (lc $params{format} eq 'auto') { # first try the more simple format if (is_dsv_data($params{data})) { $params{format} = 'dsv'; - $params{sep_char}->{dsv} = '\|'; } else { $params{format} = 'csv'; - $params{sep_char}->{csv} = ','; } } my @data; if (lc $params{format} eq 'csv') { - @data=read_csv(\%params); + @data=split_csv($params{data}, + defined $params{delimiter} ? $params{delimiter} : ",",); + # linkify after parsing since html link quoting can + # confuse CSV parsing + @data=map { + [ map { + IkiWiki::linkify($params{page}, + $params{destpage}, $_); + } @$_ ] + } @data; } elsif (lc $params{format} eq 'dsv') { - @data=read_dsv(\%params); + # linkify before parsing since wikilinks can contain the + # delimiter + $params{data} = IkiWiki::linkify($params{page}, + $params{destpage}, $params{data}); + @data=split_dsv($params{data}, + defined $params{delimiter} ? $params{delimiter} : "|",); } else { - return "[[table unknown data format]]"; + error gettext("unknown data format"); } - + my $header; - if ($params{header} != 1) { + if (lc($params{header}) eq "row" || IkiWiki::yesno($params{header})) { $header=shift @data; } if (! @data) { - return "[[table has empty data]]"; + error gettext("empty data"); } - my $html = tidy_up(open_table(\%params, $header), - build_rows(\%params, @data), - close_table(\%params, $header)); - - if (defined $params{file}) { + my @lines; + push @lines, defined $params{class} + ? "
".htmlize($params, $_)." | " } @$header), - '
---|
".htmlize($params, $_)." | " } @$record), - '
!!; - $text=~s!
$!!; - chomp $text; + if ($colspan > 1) { + push @ret, "\t\t\t<$e colspan=\"$colspan\">$cell$e>" + } + else { + push @ret, "\t\t\t<$e>$cell$e>" + } + } + push @ret, "\t\t