]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki/Plugin/table.pm
* meta: Support license and copyright information. The information will
[ikiwiki.git] / IkiWiki / Plugin / table.pm
1 package IkiWiki::Plugin::table;
2 # by Victor Moral <victor@taquiones.net>
3
4 use warnings;
5 use strict;
6 use Encode;
7 use IkiWiki 2.00;
8
9 sub import { #{{{
10         hook(type => "preprocess", id => "table", call => \&preprocess);
11 } # }}}
12
13 sub preprocess (@) { #{{{
14         my %params =(
15                 format  => 'auto',
16                 header  => 'yes',
17                 @_
18         );
19
20         if (exists $params{file}) {
21                 if (! $pagesources{$params{file}}) {
22                         return "[[table ".gettext("cannot find file")."]]";
23                 }
24                 $params{data} = readfile(srcfile($params{file}));
25         }
26         else {
27                 $params{data} = IkiWiki::linkify($params{page},
28                         $params{destpage}, $params{data});
29         }
30
31         if (lc $params{format} eq 'auto') {
32                 # first try the more simple format
33                 if (is_dsv_data($params{data})) {
34                         $params{format} = 'dsv';
35                 }
36                 else {
37                         $params{format} = 'csv';
38                 }
39         }
40
41         my @data;
42         if (lc $params{format} eq 'csv') {
43                 @data=split_csv($params{data}, $params{delimiter});
44         }
45         elsif (lc $params{format} eq 'dsv') {
46                 @data=split_dsv($params{data}, $params{delimiter});
47         }
48         else {
49                 return "[[table ".gettext("unknown data format")."]]";
50         }
51
52         my $header;
53         if (lc($params{header}) eq "yes") {
54                 $header=shift @data;
55         }
56         if (! @data) {
57                 return "[[table ".gettext("empty data")."]]";
58         }
59
60         my @lines;
61         push @lines, defined $params{class}
62                         ? "<table class=\"".$params{class}.'">'
63                         : '<table>';
64         push @lines, "\t<thead>",
65                 genrow($params{page}, $params{destpage}, "th", @$header),
66                 "\t</thead>" if defined $header;
67         push @lines, "\t<tbody>";
68         push @lines, genrow($params{page}, $params{destpage}, "td", @$_)
69                 foreach @data;
70         push @lines, "\t</tbody>" if defined $header;
71         push @lines, '</table>';
72         my $html = join("\n", @lines);
73
74         if (exists $params{file}) {
75                 return $html."\n\n".
76                         htmllink($params{page}, $params{destpage}, $params{file},
77                                 linktext => gettext('Direct data download'));
78         }
79         else {  
80                 return $html;
81         }            
82 } #}}}
83
84 sub is_dsv_data ($) { #{{{
85         my $text = shift;
86
87         my ($line) = split(/\n/, $text);
88         return $line =~ m{.+\|};
89 }
90
91 sub split_csv ($$) { #{{{
92         my @text_lines = split(/\n/, shift);
93         my $delimiter = shift;
94
95         eval q{use Text::CSV};
96         error($@) if $@;
97         my $csv = Text::CSV->new({ 
98                 sep_char        => defined $delimiter ? $delimiter : ",",
99                 binary          => 1,
100                 allow_loose_quotes => 1,
101         }) || error("could not create a Text::CSV object");
102         
103         my $l=0;
104         my @data;
105         foreach my $line (@text_lines) {
106                 $l++;
107                 if ($csv->parse($line)) {
108                         push(@data, [ map { decode_utf8 $_ } $csv->fields() ]);
109                 }
110                 else {
111                         debug(sprintf(gettext('parse fail at line %d: %s'), 
112                                 $l, $csv->error_input()));
113                 }
114         }
115
116         return @data;
117 } #}}}
118
119 sub split_dsv ($$) { #{{{
120         my @text_lines = split(/\n/, shift);
121         my $delimiter = shift;
122         $delimiter="|" unless defined $delimiter;
123
124         my @data;
125         foreach my $line (@text_lines) {
126                 push @data, [ split(/\Q$delimiter\E/, $line, -1) ];
127         }
128     
129         return @data;
130 } #}}}
131
132 sub genrow ($$$@) { #{{{
133         my $page = shift;
134         my $destpage = shift;
135         my $elt = shift;
136         my @data = @_;
137
138         my @ret;
139         push @ret, "\t\t<tr>";
140         for (my $x=0; $x < @data; $x++) {
141                 my $cell=htmlize($page, $destpage, $data[$x]);
142                 my $colspan=1;
143                 while ($x+1 < @data && $data[$x+1] eq '') {
144                         $x++;
145                         $colspan++;
146                 }
147                 if ($colspan > 1) {
148                         push @ret, "\t\t\t<$elt colspan=\"$colspan\">$cell</$elt>"
149                 }
150                 else {
151                         push @ret, "\t\t\t<$elt>$cell</$elt>"
152                 }
153         }
154         push @ret, "\t\t</tr>";
155
156         return @ret;
157 } #}}}
158
159 sub htmlize ($$$) { #{{{
160         my $page = shift;
161         my $destpage = shift;
162         my $text = shift;
163
164         $text=IkiWiki::htmlize($page, pagetype($pagesources{$page}),
165                 IkiWiki::preprocess($page, $destpage, $text));
166
167         # hack to get rid of enclosing junk added by markdown
168         $text=~s!^<p>!!;
169         $text=~s!</p>$!!;
170         chomp $text;
171
172         return $text;
173 }
174
175 1