tables.pl 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. # Copyright (C) 2004 Alex Schroeder <alex@emacswiki.org>
  2. #
  3. # This program is free software; you can redistribute it and/or modify
  4. # it under the terms of the GNU General Public License as published by
  5. # the Free Software Foundation; either version 3 of the License, or
  6. # (at your option) any later version.
  7. #
  8. # This program is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU General Public License for more details.
  12. #
  13. # You should have received a copy of the GNU General Public License
  14. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. use strict;
  16. use v5.10;
  17. AddModuleDescription('tables.pl', 'Table Markup Extension');
  18. our ($bol, @MyRules);
  19. push(@MyRules, \&TablesRule);
  20. my $RowCount;
  21. sub TablesRule {
  22. # tables using || -- the first row of a table
  23. if ($bol && m/\G(\s*\n)*((\|\|)+)([ \t])*(?=.*\|\|[ \t]*(\n|$))/cg) {
  24. $RowCount = 1;
  25. return OpenHtmlEnvironment('table',1,'user')
  26. . AddHtmlEnvironment('tr', 'class="odd first"')
  27. . AddHtmlEnvironment('td', TableAttributes(length($2)/2, $4));
  28. }
  29. # tables using || -- end of the row and beginning of the next row
  30. elsif (InElement('td') && m/\G[ \t]*((\|\|)+)[ \t]*\n((\|\|)+)([ \t]*)/cg) {
  31. my $attr = TableAttributes(length($3)/2, $5);
  32. my $type = ++$RowCount % 2 ? 'odd' : 'even';
  33. $attr = " " . $attr if $attr;
  34. return qq{</td></tr><tr class="$type"><td$attr>};
  35. }
  36. # tables using || -- an ordinary table cell
  37. elsif (InElement('td') && m/\G[ \t]*((\|\|)+)([ \t]*)(?!(\n|$))/cg) {
  38. my $attr = TableAttributes(length($1)/2, $3);
  39. $attr = " " . $attr if $attr;
  40. return "</td><td$attr>";
  41. }
  42. # tables using || -- since "next row" was taken care of above, this must be the last row
  43. elsif (InElement('td') && m/\G[ \t]*((\|\|)+)[ \t]*/cg) {
  44. return CloseHtmlEnvironments() . AddHtmlEnvironment('p');
  45. }
  46. return;
  47. }
  48. sub TableAttributes {
  49. my ($span, $left, $right) = @_;
  50. my $attr = '';
  51. $attr = "colspan=\"$span\"" if ($span != 1);
  52. m/\G(?=.*?([ \t]*)\|\|)/;
  53. $right = $1;
  54. $attr .= ' ' if ($attr and ($left or $right));
  55. if ($left and $right) { $attr .= 'align="center"' }
  56. elsif ($left) { $attr .= 'align="right"' }
  57. elsif ($right) { $attr .= 'align="left"' }
  58. return $attr;
  59. }