headers.pl 2.0 KB

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