]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki/Plugin/table.pm
cleanup
[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
7 use IkiWiki;
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
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';
31                 }
32                 else {
33                         $params{format} = 'csv';
34                 }
35         }
36
37         my @data;
38         if (lc $params{format} eq 'csv') {
39                 @data=split_csv($params{data}, $params{delimiter});
40         }
41         elsif (lc $params{format} eq 'dsv') {
42                 @data=split_dsv($params{data}, $params{delimiter});
43         }
44         else {
45                 return "[[table ".gettext("unknown data format")."]]";
46         }
47
48         my $header;
49         if (lc($params{header}) eq "yes") {
50                 $header=shift @data;
51         }
52         if (! @data) {
53                 return "[[table ".gettext("empty data")."]]";
54         }
55
56         my @lines;
57         push @lines, defined $params{class}
58                         ? "<table class=\"".$params{class}.'">'
59                         : '<table>';
60         push @lines, "\t<thead>","\t\t<tr>",
61                 (map {
62                         "\t\t\t<th>".
63                         htmlize($params{page}, $params{destpage}, $_).
64                         "</th>"
65                 } @$header),
66                 "\t\t</tr>", "\t</thead>" if defined $header;
67         push @lines, "\t<tbody>";
68         foreach my $record (@data) {
69                 push @lines, "\t\t<tr>",
70                         (map {
71                                 "\t\t\t<td>".
72                                 htmlize($params{page}, $params{destpage}, $_).
73                                 "</td>"
74                         } @$record),
75                         "\t\t</tr>";
76         }
77         push @lines, "\t</tbody>" if defined $header;
78         push @lines, '</table>';
79         my $html = join("\n", @lines);
80
81         if (exists $params{file}) {
82                 return $html."\n\n".
83                         htmllink($params{page}, $params{destpage}, $params{file},
84                                 linktext => gettext('Direct data download'));
85         }
86         else {  
87                 return $html;
88         }            
89 } #}}}
90
91 sub is_dsv_data ($) { #{{{
92         my $text = shift;
93
94         my ($line) = split(/\n/, $text);
95         return $line =~ m{.+\|};
96 }
97
98 sub split_csv ($$) { #{{{
99         my @text_lines = split(/\n/, shift);
100         my $delimiter = shift;
101
102         eval q{use Text::CSV};
103         error($@) if $@;
104         my $csv = Text::CSV->new({ 
105                 sep_char        => defined $delimiter ? $delimiter : ",",
106                 binary          => 1,
107         }) || error("could not create a Text::CSV object");
108         
109         my $l=0;
110         my @data;
111         foreach my $line (@text_lines) {
112                 $l++;
113                 if ($csv->parse($line)) {
114                         push(@data, [ $csv->fields() ]);
115                 }
116                 else {
117                         debug(sprintf(gettext('parse fail at line %d: %s'), 
118                                 $l, $csv->error_input()));
119                 }
120         }
121
122         return @data;
123 } #}}}
124
125 sub split_dsv ($$) { #{{{
126         my @text_lines = split(/\n/, shift);
127         my $delimiter = shift;
128         $delimiter="|" unless defined $delimiter;
129
130         my @data;
131         foreach my $line (@text_lines) {
132                 push @data, [ split(/\Q$delimiter\E/, $line) ];
133         }
134     
135         return @data;
136 } #}}}
137
138 sub htmlize ($$$) { #{{{
139         my $page = shift;
140         my $destpage = shift;
141         my $text = shift;
142
143         $text=IkiWiki::htmlize($page, pagetype($pagesources{$page}),
144                 IkiWiki::preprocess($page, $destpage, $text));
145
146         # hack to get rid of enclosing junk added by markdown
147         $text=~s!^<p>!!;
148         $text=~s!</p>$!!;
149         chomp $text;
150
151         return $text;
152 }
153
154 1