setext.pl 2.3 KB

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