tables-in-lists.pl 2.6 KB

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