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