headers.pl 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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('headers.pl', 'Header Markup Extension');
  21. our ($q, $bol, %RuleOrder, @MyRules, $PortraitSupportColor, $PortraitSupportColorDiv);
  22. # After toc.pl but before usemod.pl
  23. push(@MyRules, \&HeadersRule);
  24. $RuleOrder{\&HeadersRule} = 95;
  25. # The trickiest part is the first rule. It finds titles like the following:
  26. #
  27. # foo
  28. # ---
  29. #
  30. # This assumes that --- is not found at the beginning of a line.
  31. # Normally this is used as an M-dash in the middle of text with no
  32. # surrounding whitespace---like this.
  33. #
  34. # Similarly, the horizontal rule requires an empty preceding line to
  35. # work. We'll see how it goes. ;)
  36. sub HeadersRule {
  37. my $oldpos = pos;
  38. if ($bol && (m/\G((.+?)[ \t]*\n(---+|===+)[ \t]*\n)/cg)) {
  39. my $html = CloseHtmlEnvironments() . ($PortraitSupportColorDiv ? '</div>' : '');
  40. if (substr($3,0,1) eq '=') {
  41. $html .= $q->h2($2);
  42. } else {
  43. $html .= $q->h3($2);
  44. }
  45. $PortraitSupportColorDiv = 0;
  46. $PortraitSupportColor = 0;
  47. return $html . AddHtmlEnvironment('p');
  48. } elsif ($bol && m/\G(\s*\n)*----+[ \t]*\n?/cg) {
  49. my $html = CloseHtmlEnvironments() . ($PortraitSupportColorDiv ? '</div>' : '') . $q->hr();
  50. $PortraitSupportColorDiv = 0;
  51. $PortraitSupportColor = 0;
  52. return $html . AddHtmlEnvironment('p');
  53. }
  54. return;
  55. }