]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki/Plugin/table.pm
* Fix urls generated by mirrorlist plugin.
[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>",
61                 genrow($params{page}, $params{destpage}, "th", @$header),
62                 "\t</thead>" if defined $header;
63         push @lines, "\t<tbody>";
64         push @lines, genrow($params{page}, $params{destpage}, "td", @$_)
65                 foreach @data;
66         push @lines, "\t</tbody>" if defined $header;
67         push @lines, '</table>';
68         my $html = join("\n", @lines);
69
70         if (exists $params{file}) {
71                 return $html."\n\n".
72                         htmllink($params{page}, $params{destpage}, $params{file},
73                                 linktext => gettext('Direct data download'));
74         }
75         else {  
76                 return $html;
77         }            
78 } #}}}
79
80 sub is_dsv_data ($) { #{{{
81         my $text = shift;
82
83         my ($line) = split(/\n/, $text);
84         return $line =~ m{.+\|};
85 }
86
87 sub split_csv ($$) { #{{{
88         my @text_lines = split(/\n/, shift);
89         my $delimiter = shift;
90
91         eval q{use Text::CSV};
92         error($@) if $@;
93         my $csv = Text::CSV->new({ 
94                 sep_char        => defined $delimiter ? $delimiter : ",",
95                 binary          => 1,
96         }) || error("could not create a Text::CSV object");
97         
98         my $l=0;
99         my @data;
100         foreach my $line (@text_lines) {
101                 $l++;
102                 if ($csv->parse($line)) {
103                         push(@data, [ $csv->fields() ]);
104                 }
105                 else {
106                         debug(sprintf(gettext('parse fail at line %d: %s'), 
107                                 $l, $csv->error_input()));
108                 }
109         }
110
111         return @data;
112 } #}}}
113
114 sub split_dsv ($$) { #{{{
115         my @text_lines = split(/\n/, shift);
116         my $delimiter = shift;
117         $delimiter="|" unless defined $delimiter;
118
119         my @data;
120         foreach my $line (@text_lines) {
121                 push @data, [ split(/\Q$delimiter\E/, $line, -1) ];
122         }
123     
124         return @data;
125 } #}}}
126
127 sub genrow ($$$@) { #{{{
128         my $page = shift;
129         my $destpage = shift;
130         my $elt = shift;
131         my @data = @_;
132
133         my @ret;
134         push @ret, "\t\t<tr>";
135         for (my $x=0; $x < @data; $x++) {
136                 my $cell=htmlize($page, $destpage, $data[$x]);
137                 my $colspan=1;
138                 while ($x+1 < @data && $data[$x+1] eq '') {
139                         $x++;
140                         $colspan++;
141                 }
142                 if ($colspan > 1) {
143                         push @ret, "\t\t\t<$elt colspan=\"$colspan\">$cell</$elt>"
144                 }
145                 else {
146                         push @ret, "\t\t\t<$elt>$cell</$elt>"
147                 }
148         }
149         push @ret, "\t\t</tr>";
150
151         return @ret;
152 } #}}}
153
154 sub htmlize ($$$) { #{{{
155         my $page = shift;
156         my $destpage = shift;
157         my $text = shift;
158
159         $text=IkiWiki::htmlize($page, pagetype($pagesources{$page}),
160                 IkiWiki::preprocess($page, $destpage, $text));
161
162         # hack to get rid of enclosing junk added by markdown
163         $text=~s!^<p>!!;
164         $text=~s!</p>$!!;
165         chomp $text;
166
167         return $text;
168 }
169
170 1