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