1 package IkiWiki::Plugin::table;
2 # by Victor Moral <victor@taquiones.net>
10 hook(type => "preprocess", id => "table", call => \&preprocess);
13 sub preprocess (@) { #{{{
20 if (exists $params{file}) {
21 if (! $pagesources{$params{file}}) {
22 return "[[table ".gettext("cannot find file")."]]";
24 $params{data} = readfile(srcfile($params{file}));
25 add_depends($params{page}, $params{file});
28 if (lc $params{format} eq 'auto') {
29 # first try the more simple format
30 if (is_dsv_data($params{data})) {
31 $params{format} = 'dsv';
34 $params{format} = 'csv';
39 if (lc $params{format} eq 'csv') {
40 @data=split_csv($params{data},
41 defined $params{delimiter} ? $params{delimiter} : ",",);
42 # linkify after parsing since html link quoting can
44 if (! exists $params{file}) {
47 IkiWiki::linkify($params{page},
48 $params{destpage}, $_);
53 elsif (lc $params{format} eq 'dsv') {
54 # linkify before parsing since wikilinks can contain the
56 if (! exists $params{file}) {
57 $params{data} = IkiWiki::linkify($params{page},
58 $params{destpage}, $params{data});
60 @data=split_dsv($params{data},
61 defined $params{delimiter} ? $params{delimiter} : "|",);
64 return "[[table ".gettext("unknown data format")."]]";
68 if (lc($params{header}) eq "yes") {
72 return "[[table ".gettext("empty data")."]]";
76 push @lines, defined $params{class}
77 ? "<table class=\"".$params{class}.'">'
79 push @lines, "\t<thead>",
80 genrow($params{page}, $params{destpage}, "th", @$header),
81 "\t</thead>" if defined $header;
82 push @lines, "\t<tbody>" if defined $header;
83 push @lines, genrow($params{page}, $params{destpage}, "td", @$_)
85 push @lines, "\t</tbody>" if defined $header;
86 push @lines, '</table>';
87 my $html = join("\n", @lines);
89 if (exists $params{file}) {
91 htmllink($params{page}, $params{destpage}, $params{file},
92 linktext => gettext('Direct data download'));
99 sub is_dsv_data ($) { #{{{
102 my ($line) = split(/\n/, $text);
103 return $line =~ m{.+\|};
106 sub split_csv ($$) { #{{{
107 my @text_lines = split(/\n/, shift);
108 my $delimiter = shift;
110 eval q{use Text::CSV};
112 my $csv = Text::CSV->new({
113 sep_char => $delimiter,
115 allow_loose_quotes => 1,
116 }) || error("could not create a Text::CSV object");
120 foreach my $line (@text_lines) {
122 if ($csv->parse($line)) {
123 push(@data, [ map { decode_utf8 $_ } $csv->fields() ]);
126 debug(sprintf(gettext('parse fail at line %d: %s'),
127 $l, $csv->error_input()));
134 sub split_dsv ($$) { #{{{
135 my @text_lines = split(/\n/, shift);
136 my $delimiter = shift;
137 $delimiter="|" unless defined $delimiter;
140 foreach my $line (@text_lines) {
141 push @data, [ split(/\Q$delimiter\E/, $line, -1) ];
147 sub genrow ($$$@) { #{{{
149 my $destpage = shift;
154 push @ret, "\t\t<tr>";
155 for (my $x=0; $x < @data; $x++) {
156 my $cell=htmlize($page, $destpage, $data[$x]);
158 while ($x+1 < @data && $data[$x+1] eq '') {
163 push @ret, "\t\t\t<$elt colspan=\"$colspan\">$cell</$elt>"
166 push @ret, "\t\t\t<$elt>$cell</$elt>"
169 push @ret, "\t\t</tr>";
174 sub htmlize ($$$) { #{{{
176 my $destpage = shift;
178 return IkiWiki::htmlize($page, $destpage, pagetype($pagesources{$page}),
179 IkiWiki::preprocess($page, $destpage, shift));