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