tables.pl 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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 2 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, write to the
  15. # Free Software Foundation, Inc.
  16. # 59 Temple Place, Suite 330
  17. # Boston, MA 02111-1307 USA
  18. use strict;
  19. use v5.10;
  20. AddModuleDescription('tables.pl', 'Table Markup Extension');
  21. our ($bol, @MyRules);
  22. push(@MyRules, \&TablesRule);
  23. my $RowCount;
  24. sub TablesRule {
  25. # tables using || -- the first row of a table
  26. if ($bol && m/\G(\s*\n)*((\|\|)+)([ \t])*(?=.*\|\|[ \t]*(\n|$))/cg) {
  27. $RowCount = 1;
  28. return OpenHtmlEnvironment('table',1,'user')
  29. . AddHtmlEnvironment('tr', 'class="odd first"')
  30. . AddHtmlEnvironment('td', TableAttributes(length($2)/2, $4));
  31. }
  32. # tables using || -- end of the row and beginning of the next row
  33. elsif (InElement('td') && m/\G[ \t]*((\|\|)+)[ \t]*\n((\|\|)+)([ \t]*)/cg) {
  34. my $attr = TableAttributes(length($3)/2, $5);
  35. my $type = ++$RowCount % 2 ? 'odd' : 'even';
  36. $attr = " " . $attr if $attr;
  37. return qq{</td></tr><tr class="$type"><td$attr>};
  38. }
  39. # tables using || -- an ordinary table cell
  40. elsif (InElement('td') && m/\G[ \t]*((\|\|)+)([ \t]*)(?!(\n|$))/cg) {
  41. my $attr = TableAttributes(length($1)/2, $3);
  42. $attr = " " . $attr if $attr;
  43. return "</td><td$attr>";
  44. }
  45. # tables using || -- since "next row" was taken care of above, this must be the last row
  46. elsif (InElement('td') && m/\G[ \t]*((\|\|)+)[ \t]*/cg) {
  47. return CloseHtmlEnvironments() . AddHtmlEnvironment('p');
  48. }
  49. return;
  50. }
  51. sub TableAttributes {
  52. my ($span, $left, $right) = @_;
  53. my $attr = '';
  54. $attr = "colspan=\"$span\"" if ($span != 1);
  55. m/\G(?=.*?([ \t]*)\|\|)/;
  56. $right = $1;
  57. $attr .= ' ' if ($attr and ($left or $right));
  58. if ($left and $right) { $attr .= 'align="center"' }
  59. elsif ($left) { $attr .= 'align="right"' }
  60. elsif ($right) { $attr .= 'align="left"' }
  61. return $attr;
  62. }