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