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