tables-in-lists.pl 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. # Copyright (C) 2004, 2005 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-in-lists.pl', 'Table Markup Extension');
  18. our ($bol, @MyRules);
  19. push(@MyRules, \&TablesInListsRule);
  20. sub TablesInListsRule {
  21. # tables using || -- the first row of a table inside a list
  22. my $rowcount;
  23. if ($bol && m/\G((\|\|)+)([ \t])*(?=.*\|\|[ \t]*(\n|$))/cg) {
  24. $rowcount = 1;
  25. if (InElement('li')) {
  26. return CloseHtmlEnvironmentUntil('li')
  27. . AddHtmlEnvironment('table', 'class="user"')
  28. . AddHtmlEnvironment('tr', 'class="odd first"')
  29. . AddHtmlEnvironment('td', UsemodTableAttributes(length($1)/2, $3));
  30. } else {
  31. return OpenHtmlEnvironment('table',1,'user')
  32. . AddHtmlEnvironment('tr', 'class="odd first"')
  33. . AddHtmlEnvironment('td', UsemodTableAttributes(length($2)/2, $4));
  34. }
  35. }
  36. # tables using || -- end of the row and beginning of the next row
  37. elsif (InElement('td') && m/\G[ \t]*((\|\|)+)[ \t]*\n((\|\|)+)([ \t]*)/cg) {
  38. my $attr = UsemodTableAttributes(length($3)/2, $5);
  39. my $type = ++$rowcount % 2 ? 'odd' : 'even';
  40. $attr = " " . $attr if $attr;
  41. return qq{</td></tr><tr class="$type"><td$attr>};
  42. }
  43. # tables using || -- an ordinary table cell
  44. elsif (InElement('td') && m/\G[ \t]*((\|\|)+)([ \t]*)(?!(\n|$))/cg) {
  45. my $attr = UsemodTableAttributes(length($1)/2, $3);
  46. $attr = " " . $attr if $attr;
  47. return "</td><td$attr>";
  48. }
  49. # tables using || -- since "next row" was taken care of above, this must be the last row
  50. elsif (InElement('td') && m/\G[ \t]*((\|\|)+)[ \t]*/cg) {
  51. if (InElement('li')) {
  52. return CloseHtmlEnvironmentUntil('li');
  53. } else {
  54. return CloseHtmlEnvironments() . AddHtmlEnvironment('p');
  55. }
  56. }
  57. return; # sonst geht Oddmuse in eine Endlosschleife
  58. }