]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blobdiff - IkiWiki/Plugin/table.pm
(no commit message)
[git.ikiwiki.info.git] / IkiWiki / Plugin / table.pm
index b6f53f607b1741fc959afc452ef65e252b5a4e49..7fea8ab1c2112d98e9d2d6c309b0a69ea3aac31c 100644 (file)
@@ -4,11 +4,11 @@ package IkiWiki::Plugin::table;
 use warnings;
 use strict;
 use Encode;
-use IkiWiki 2.00;
+use IkiWiki 3.00;
 
 sub import {
        hook(type => "getsetup", id => "table", call => \&getsetup);
-       hook(type => "preprocess", id => "table", call => \&preprocess);
+       hook(type => "preprocess", id => "table", call => \&preprocess, scan => 1);
 }
 
 sub getsetup () {
@@ -16,6 +16,7 @@ sub getsetup () {
                plugin => {
                        safe => 1,
                        rebuild => undef,
+                       section => "widget",
                },
 }
 
@@ -27,13 +28,31 @@ sub preprocess (@) {
        );
 
        if (exists $params{file}) {
-               if (! $pagesources{$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};
+
+               # Preprocess in scan-only mode.
+               IkiWiki::preprocess($params{page}, $params{page}, $params{data}, 1);
+
+               IkiWiki::run_hooks(scan => sub {
+                       shift->(
+                               page => $params{page},
+                               content => $params{data},
+                       );
+               });
+
+               return;
+       }
+
        if (lc $params{format} eq 'auto') {
                # first try the more simple format
                if (is_dsv_data($params{data})) {
@@ -50,22 +69,18 @@ sub preprocess (@) {
                        defined $params{delimiter} ? $params{delimiter} : ",",);
                # linkify after parsing since html link quoting can
                # confuse CSV parsing
-               if (! exists $params{file}) {
-                       @data=map {
-                               [ map {
-                                       IkiWiki::linkify($params{page},
-                                               $params{destpage}, $_);
-                               } @$_ ]
-                       } @data;
-               }
+               @data=map {
+                       [ map {
+                               IkiWiki::linkify($params{page},
+                                       $params{destpage}, $_);
+                       } @$_ ]
+               } @data;
        }
        elsif (lc $params{format} eq 'dsv') {
                # linkify before parsing since wikilinks can contain the
                # delimiter
-               if (! exists $params{file}) {
-                       $params{data} = IkiWiki::linkify($params{page},
-                               $params{destpage}, $params{data});
-               }
+               $params{data} = IkiWiki::linkify($params{page},
+                       $params{destpage}, $params{data});
                @data=split_dsv($params{data},
                        defined $params{delimiter} ? $params{delimiter} : "|",);
        }
@@ -120,6 +135,7 @@ sub split_csv ($$) {
        my $csv = Text::CSV->new({ 
                sep_char        => $delimiter,
                binary          => 1,
+               decode_utf8 => 1,
                allow_loose_quotes => 1,
        }) || error("could not create a Text::CSV object");
        
@@ -128,7 +144,7 @@ sub split_csv ($$) {
        foreach my $line (@text_lines) {
                $l++;
                if ($csv->parse($line)) {
-                       push(@data, [ map { decode_utf8 $_ } $csv->fields() ]);
+                       push(@data, [ $csv->fields() ]);
                }
                else {
                        debug(sprintf(gettext('parse fail at line %d: %s'),