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