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