]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki/Plugin/table.pm
rssurl and atomurl are only needed where $feeds is set
[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
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},
40                         defined $params{delimiter} ? $params{delimiter} : ",",);
41                 # linkify after parsing since html link quoting can
42                 # confuse CSV parsing
43                 if (! exists $params{file}) {
44                         @data=map {
45                                 [ map {
46                                         IkiWiki::linkify($params{page},
47                                                 $params{destpage}, $_);
48                                 } @$_ ]
49                         } @data;
50                 }
51         }
52         elsif (lc $params{format} eq 'dsv') {
53                 # linkify before parsing since wikilinks can contain the
54                 # delimiter
55                 if (! exists $params{file}) {
56                         $params{data} = IkiWiki::linkify($params{page},
57                                 $params{destpage}, $params{data});
58                 }
59                 @data=split_dsv($params{data},
60                         defined $params{delimiter} ? $params{delimiter} : "|",);
61         }
62         else {
63                 return "[[table ".gettext("unknown data format")."]]";
64         }
65
66         my $header;
67         if (lc($params{header}) eq "yes") {
68                 $header=shift @data;
69         }
70         if (! @data) {
71                 return "[[table ".gettext("empty data")."]]";
72         }
73
74         my @lines;
75         push @lines, defined $params{class}
76                         ? "<table class=\"".$params{class}.'">'
77                         : '<table>';
78         push @lines, "\t<thead>",
79                 genrow($params{page}, $params{destpage}, "th", @$header),
80                 "\t</thead>" if defined $header;
81         push @lines, "\t<tbody>" if defined $header;
82         push @lines, genrow($params{page}, $params{destpage}, "td", @$_)
83                 foreach @data;
84         push @lines, "\t</tbody>" if defined $header;
85         push @lines, '</table>';
86         my $html = join("\n", @lines);
87
88         if (exists $params{file}) {
89                 return $html."\n\n".
90                         htmllink($params{page}, $params{destpage}, $params{file},
91                                 linktext => gettext('Direct data download'));
92         }
93         else {  
94                 return $html;
95         }            
96 } #}}}
97
98 sub is_dsv_data ($) { #{{{
99         my $text = shift;
100
101         my ($line) = split(/\n/, $text);
102         return $line =~ m{.+\|};
103 }
104
105 sub split_csv ($$) { #{{{
106         my @text_lines = split(/\n/, shift);
107         my $delimiter = shift;
108
109         eval q{use Text::CSV};
110         error($@) if $@;
111         my $csv = Text::CSV->new({ 
112                 sep_char        => $delimiter,
113                 binary          => 1,
114                 allow_loose_quotes => 1,
115         }) || error("could not create a Text::CSV object");
116         
117         my $l=0;
118         my @data;
119         foreach my $line (@text_lines) {
120                 $l++;
121                 if ($csv->parse($line)) {
122                         push(@data, [ map { decode_utf8 $_ } $csv->fields() ]);
123                 }
124                 else {
125                         debug(sprintf(gettext('parse fail at line %d: %s'), 
126                                 $l, $csv->error_input()));
127                 }
128         }
129
130         return @data;
131 } #}}}
132
133 sub split_dsv ($$) { #{{{
134         my @text_lines = split(/\n/, shift);
135         my $delimiter = shift;
136         $delimiter="|" unless defined $delimiter;
137
138         my @data;
139         foreach my $line (@text_lines) {
140                 push @data, [ split(/\Q$delimiter\E/, $line, -1) ];
141         }
142     
143         return @data;
144 } #}}}
145
146 sub genrow ($$$@) { #{{{
147         my $page = shift;
148         my $destpage = shift;
149         my $elt = shift;
150         my @data = @_;
151
152         my @ret;
153         push @ret, "\t\t<tr>";
154         for (my $x=0; $x < @data; $x++) {
155                 my $cell=htmlize($page, $destpage, $data[$x]);
156                 my $colspan=1;
157                 while ($x+1 < @data && $data[$x+1] eq '') {
158                         $x++;
159                         $colspan++;
160                 }
161                 if ($colspan > 1) {
162                         push @ret, "\t\t\t<$elt colspan=\"$colspan\">$cell</$elt>"
163                 }
164                 else {
165                         push @ret, "\t\t\t<$elt>$cell</$elt>"
166                 }
167         }
168         push @ret, "\t\t</tr>";
169
170         return @ret;
171 } #}}}
172
173 sub htmlize ($$$) { #{{{
174         my $page = shift;
175         my $destpage = shift;
176         my $text = shift;
177
178         $text=IkiWiki::htmlize($page, pagetype($pagesources{$page}),
179                 IkiWiki::preprocess($page, $destpage, $text));
180
181         # hack to get rid of enclosing junk added by markdown
182         $text=~s!^<p>!!;
183         $text=~s!</p>$!!;
184         chomp $text;
185
186         return $text;
187 }
188
189 1