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}));
27 if (lc $params{format} eq 'auto') {
28 # first try the more simple format
29 if (is_dsv_data($params{data})) {
30 $params{format} = 'dsv';
33 $params{format} = 'csv';
38 if (lc $params{format} eq 'csv') {
39 @data=split_csv($params{data},
40 defined $params{delimiter} ? $params{delimiter} : ",",);
41 # linkify after parsing since html link quoting can
43 if (! exists $params{file}) {
46 IkiWiki::linkify($params{page},
47 $params{destpage}, $_);
52 elsif (lc $params{format} eq 'dsv') {
53 # linkify before parsing since wikilinks can contain the
55 if (! exists $params{file}) {
56 $params{data} = IkiWiki::linkify($params{page},
57 $params{destpage}, $params{data});
59 @data=split_dsv($params{data},
60 defined $params{delimiter} ? $params{delimiter} : "|",);
63 return "[[table ".gettext("unknown data format")."]]";
67 if (lc($params{header}) eq "yes") {
71 return "[[table ".gettext("empty data")."]]";
75 push @lines, defined $params{class}
76 ? "<table class=\"".$params{class}.'">'
78 push @lines, "\t<thead>",
79 genrow($params{page}, $params{destpage}, "th", @$header),
80 "\t</thead>" if defined $header;
81 push @lines, "\t<tbody>";
82 push @lines, genrow($params{page}, $params{destpage}, "td", @$_)
84 push @lines, "\t</tbody>" if defined $header;
85 push @lines, '</table>';
86 my $html = join("\n", @lines);
88 if (exists $params{file}) {
90 htmllink($params{page}, $params{destpage}, $params{file},
91 linktext => gettext('Direct data download'));
98 sub is_dsv_data ($) { #{{{
101 my ($line) = split(/\n/, $text);
102 return $line =~ m{.+\|};
105 sub split_csv ($$) { #{{{
106 my @text_lines = split(/\n/, shift);
107 my $delimiter = shift;
109 eval q{use Text::CSV};
111 my $csv = Text::CSV->new({
112 sep_char => $delimiter,
114 allow_loose_quotes => 1,
115 }) || error("could not create a Text::CSV object");
119 foreach my $line (@text_lines) {
121 if ($csv->parse($line)) {
122 push(@data, [ map { decode_utf8 $_ } $csv->fields() ]);
125 debug(sprintf(gettext('parse fail at line %d: %s'),
126 $l, $csv->error_input()));
133 sub split_dsv ($$) { #{{{
134 my @text_lines = split(/\n/, shift);
135 my $delimiter = shift;
136 $delimiter="|" unless defined $delimiter;
139 foreach my $line (@text_lines) {
140 push @data, [ split(/\Q$delimiter\E/, $line, -1) ];
146 sub genrow ($$$@) { #{{{
148 my $destpage = shift;
153 push @ret, "\t\t<tr>";
154 for (my $x=0; $x < @data; $x++) {
155 my $cell=htmlize($page, $destpage, $data[$x]);
157 while ($x+1 < @data && $data[$x+1] eq '') {
162 push @ret, "\t\t\t<$elt colspan=\"$colspan\">$cell</$elt>"
165 push @ret, "\t\t\t<$elt>$cell</$elt>"
168 push @ret, "\t\t</tr>";
173 sub htmlize ($$$) { #{{{
175 my $destpage = shift;
178 $text=IkiWiki::htmlize($page, pagetype($pagesources{$page}),
179 IkiWiki::preprocess($page, $destpage, $text));
181 # hack to get rid of enclosing junk added by markdown