summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/table.pm
blob: 1302646b130084fefd87456d2f905b8c4c2cfea6 (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 => 'yes',
  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 "yes") {
  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{page}, $params{destpage}, "th", @$header),
  81. "\t</thead>" if defined $header;
  82. push @lines, "\t<tbody>" if defined $header;
  83. push @lines, genrow($params{page}, $params{destpage}, "td", @$_)
  84. foreach @data;
  85. push @lines, "\t</tbody>" if defined $header;
  86. push @lines, '</table>';
  87. my $html = join("\n", @lines);
  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. sub is_dsv_data ($) { #{{{
  98. my $text = shift;
  99. my ($line) = split(/\n/, $text);
  100. return $line =~ m{.+\|};
  101. }
  102. sub split_csv ($$) { #{{{
  103. my @text_lines = split(/\n/, shift);
  104. my $delimiter = shift;
  105. eval q{use Text::CSV};
  106. error($@) if $@;
  107. my $csv = Text::CSV->new({
  108. sep_char => $delimiter,
  109. binary => 1,
  110. allow_loose_quotes => 1,
  111. }) || error("could not create a Text::CSV object");
  112. my $l=0;
  113. my @data;
  114. foreach my $line (@text_lines) {
  115. $l++;
  116. if ($csv->parse($line)) {
  117. push(@data, [ map { decode_utf8 $_ } $csv->fields() ]);
  118. }
  119. else {
  120. debug(sprintf(gettext('parse fail at line %d: %s'),
  121. $l, $csv->error_input()));
  122. }
  123. }
  124. return @data;
  125. } #}}}
  126. sub split_dsv ($$) { #{{{
  127. my @text_lines = split(/\n/, shift);
  128. my $delimiter = shift;
  129. $delimiter="|" unless defined $delimiter;
  130. my @data;
  131. foreach my $line (@text_lines) {
  132. push @data, [ split(/\Q$delimiter\E/, $line, -1) ];
  133. }
  134. return @data;
  135. } #}}}
  136. sub genrow ($$$@) { #{{{
  137. my $page = shift;
  138. my $destpage = shift;
  139. my $elt = shift;
  140. my @data = @_;
  141. my @ret;
  142. push @ret, "\t\t<tr>";
  143. for (my $x=0; $x < @data; $x++) {
  144. my $cell=htmlize($page, $destpage, $data[$x]);
  145. my $colspan=1;
  146. while ($x+1 < @data && $data[$x+1] eq '') {
  147. $x++;
  148. $colspan++;
  149. }
  150. if ($colspan > 1) {
  151. push @ret, "\t\t\t<$elt colspan=\"$colspan\">$cell</$elt>"
  152. }
  153. else {
  154. push @ret, "\t\t\t<$elt>$cell</$elt>"
  155. }
  156. }
  157. push @ret, "\t\t</tr>";
  158. return @ret;
  159. } #}}}
  160. sub htmlize ($$$) { #{{{
  161. my $page = shift;
  162. my $destpage = shift;
  163. return IkiWiki::htmlize($page, $destpage, pagetype($pagesources{$page}),
  164. IkiWiki::preprocess($page, $destpage, shift));
  165. }
  166. 1