setext.pl 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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('setext.pl', 'SeText Extension');
  18. our ($q, $bol, @MyRules, $PortraitSupportColorDiv);
  19. push(@MyRules, \&SeTextRule);
  20. # The trickiest part is the first rule. It finds titles like the following:
  21. #
  22. # foo
  23. # ===
  24. #
  25. # It ignores the amount of whitespace after the title and the
  26. # underlining, and it allows underlining using ==== or ----. The
  27. # underlining has to be exactly as wide as the title itself. If it is
  28. # too long or too short, the entire thing is not a title.
  29. #
  30. # If the length does not match, pos is reset and zero is returned so
  31. # that the remaining rules will be tested instead.
  32. my $word = '([-A-Za-z\x{0080}-\x{fffd}]+)';
  33. sub SeTextRule {
  34. my $oldpos = pos;
  35. if ($bol && ((m/\G((.+?)[ \t]*\n(-+|=+)[ \t]*\n)/cg
  36. and (length($2) == length($3)))
  37. or ((pos = $oldpos) and 0))) {
  38. my $html = CloseHtmlEnvironments() . ($PortraitSupportColorDiv ? '</div>' : '');
  39. if (substr($3,0,1) eq '=') {
  40. $html .= $q->h2($2);
  41. } else {
  42. $html .= $q->h3($2);
  43. }
  44. $PortraitSupportColorDiv = 0;
  45. return $html . AddHtmlEnvironment('p');
  46. } elsif ($bol && m/\G((&gt; .*\n)+)/cg) {
  47. my $text = $1;
  48. return CloseHtmlEnvironments() . $q->pre($text) . AddHtmlEnvironment('p');
  49. } elsif (m/\G\*\*($word( $word)*)\*\*/cg) {
  50. return "<b>$1</b>";
  51. } elsif (m/\G~$word~/cg) {
  52. return "<i>$1</i>";
  53. } elsif (m/\G\b_($word(_$word)*)_\b/cg) {
  54. return '<em style="text-decoration: underline; font-style: normal;">'
  55. . join(' ', split(/_/, $1)) . "</em>"; # don't clobber pos
  56. } elsif (m/\G`_(.+)_`/cg) {
  57. return $1;
  58. }
  59. return;
  60. }