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