wiki.pl: Port some fixes from upstream
[Orgmuse.git] / modules / setext.pl
blob42bfe901186582e6b9462be44e994ceaf4720ae8
1 # Copyright (C) 2004 Alex Schroeder <alex@emacswiki.org>
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.
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.
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
19 $ModulesDescription .= '<p><a href="http://git.savannah.gnu.org/cgit/oddmuse.git/tree/modules/setext.pl">setext.pl</a>, see <a href="http://www.oddmuse.org/cgi-bin/oddmuse/SeText_Extension">SeText Extension</a></p>';
21 push(@MyRules, \&SeTextRule);
23 # The trickiest part is the first rule. It finds titles like the following:
25 # foo
26 # ===
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.
33 # If the length does not match, pos is reset and zero is returned so
34 # that the remaining rules will be tested instead.
36 my $word = '([-A-Za-z\x{0080}-\x{fffd}]+)';
37 sub SeTextRule {
38 my $oldpos = pos;
39 if ($bol && ((m/\G((.+?)[ \t]*\n(-+|=+)[ \t]*\n)/gc
40 and (length($2) == length($3)))
41 or ((pos = $oldpos) and 0))) {
42 my $html = CloseHtmlEnvironments() . ($PortraitSupportColorDiv ? '</div>' : '');
43 if (substr($3,0,1) eq '=') {
44 $html .= $q->h2($2);
45 } else {
46 $html .= $q->h3($2);
48 $PortraitSupportColorDiv = 0;
49 return $html . AddHtmlEnvironment('p');
50 } elsif ($bol && m/\G((&gt; .*\n)+)/gc) {
51 my $text = $1;
52 return CloseHtmlEnvironments() . $q->pre($text) . AddHtmlEnvironment('p');
53 } elsif (m/\G\*\*($word( $word)*)\*\*/goc) {
54 return "<b>$1</b>";
55 } elsif (m/\G~$word~/goc) {
56 return "<i>$1</i>";
57 } elsif (m/\G\b_($word(_$word)*)_\b/goc) {
58 return '<em style="text-decoration: underline; font-style: normal;">'
59 . join(' ', split(/_/, $1)) . "</em>"; # don't clobber pos
60 } elsif (m/\G`_(.+)_`/gc) {
61 return $1;
63 return undef;