simple-rules.pl 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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('simple-rules.pl', 'Simple Fast Alternate Text Formatting Rules');
  21. our ($q, $OpenPageName, $FS, $UrlPattern, $FreeLinkPattern);
  22. *ApplyRules = \&NewSimpleRulesApplyRules;
  23. my $PROT = "\x1c";
  24. my $DIRT = "\x1d";
  25. # Variables which are essentially global and which contain state are localized before they are set. In order to localize
  26. # them, they have to be declared here, first.
  27. our ($counter, %protected, %dirty);
  28. sub NewSimpleRulesApplyRules {
  29. # locallinks: apply rules that create links depending on local config (incl. interlink!)
  30. my ($text, $locallinks, $withanchors, $revision) = @_;
  31. # shortcut for dirty blocks (if this is the content of a real page: no caching!)
  32. local $counter = 0;
  33. local %protected = ();
  34. local %dirty = ();
  35. my $result;
  36. $text = NewSimpleRulesApplyDirtyInlineRules($text, $locallinks);
  37. if ($text =~ /^${DIRT}[0-9]+${DIRT}$/) { # shortcut
  38. $result = $text;
  39. } else {
  40. $text =~ s/[ \t]+\n/\n/g; # no trailing whitespace to worry about
  41. $text =~ s/\n$//g;
  42. $text =~ s/^\n//g;
  43. my @paragraphs = split(/\n\n+/, $text);
  44. foreach my $block (@paragraphs) {
  45. if ($block =~ /^(.+?)\n(--+)$/ and length($1) == length($2)) {
  46. $block = SimpleRulesProtect($q->h3($1));
  47. } elsif ($block =~ /^(.+?)\n(==+)$/ and length($1) == length($2)) {
  48. $block = SimpleRulesProtect($q->h2($1));
  49. } elsif ($block =~ /^\* (.*)/s) {
  50. $block = SimpleRulesProtect($q->ul(join('', # avoid extra space in CGI.pm code
  51. map{$q->li(NewSimpleRulesApplyInlineRules($_))}
  52. split(/\n\* +/, $1))));
  53. } elsif ($block =~ /^[0-9]\. (.*)/s) {
  54. $block = SimpleRulesProtect($q->ol(join('', # avoid extra space in CGI.pm code
  55. map{$q->li(NewSimpleRulesApplyInlineRules($_))}
  56. split(/\n[0-9]\. */, $1))));
  57. } elsif ($block =~ m/^#FILE ([^ \n]+)\n(.*)/s) {
  58. $block = SimpleRulesProtect(GetDownloadLink(
  59. $OpenPageName, (substr($1, 0, 6) eq 'image/'), $revision));
  60. } else {
  61. $block = SimpleRulesProtect('<p>') . $block . SimpleRulesProtect('</p>');
  62. }
  63. ($block =~ s/(\&lt;journal(\s+(\d*))?(\s+"(.*)")?(\s+(reverse))?\&gt;)/
  64. my ($str, $num, $regexp, $reverse) = ($1, $3, $5, $7);
  65. SimpleRulesDirty($str, sub { PrintJournal($num, $regexp, $reverse)});/eg);
  66. $result .= NewSimpleRulesApplyInlineRules($block);
  67. }
  68. }
  69. return SimpleRulesMungeResult($result);
  70. }
  71. sub NewSimpleRulesApplyInlineRules {
  72. my ($block, $locallinks) = @_;
  73. $block = NewSimpleRulesApplyDirtyInlineRules($block, $locallinks);
  74. $block =~ s/$UrlPattern/SimpleRulesProtect($q->a({-href=>$1}, $1))/egs;
  75. $block =~ s/~(\S+)~/SimpleRulesProtect($q->em($1))/eg;
  76. $block =~ s/\*\*(.+?)\*\*/SimpleRulesProtect($q->strong($1))/egs;
  77. $block =~ s/\/\/(.+?)\/\//SimpleRulesProtect($q->em($1))/egs;
  78. $block =~ s/\_\_(.+?)\_\_/SimpleRulesProtect($q->u($1))/egs;
  79. $block =~ s/\*(.+?)\*/SimpleRulesProtect($q->b($1))/egs;
  80. $block =~ s/\/(.+?)\//SimpleRulesProtect($q->i($1))/egs;
  81. $block =~ s/\_(.+?)\_/SimpleRulesProtect($q->u($1))/egs;
  82. return $block;
  83. }
  84. sub NewSimpleRulesApplyDirtyInlineRules {
  85. my ($block, $locallinks) = @_;
  86. if ($locallinks) {
  87. ($block =~ s/(\[\[$FreeLinkPattern\]\])/
  88. my ($str, $link) = ($1, $2);
  89. SimpleRulesDirty($str, GetPageOrEditLink($link,0,0,1))/eg);
  90. ($block =~ s/(\[\[image:$FreeLinkPattern\]\])/
  91. my ($str, $link) = ($1, $2);
  92. SimpleRulesDirty($str, GetDownloadLink($link, 1))/eg);
  93. }
  94. return $block;
  95. }
  96. sub SimpleRulesProtect {
  97. my $html = shift;
  98. $counter++;
  99. $protected{$counter} = $html;
  100. return $PROT . $counter . $PROT;
  101. }
  102. sub SimpleRulesDirty {
  103. my ($str, $html) = @_;
  104. $counter++;
  105. $dirty{$counter} = $str;
  106. $protected{$counter} = $html;
  107. return $DIRT . $counter . $DIRT;
  108. }
  109. sub SimpleRulesMungeResult {
  110. my $raw = shift;
  111. $raw = SimpleRulesUnprotect($raw);
  112. # now do the dirty and clean block stuff
  113. my @blocks;
  114. my @flags;
  115. my $count = 0;
  116. my $html;
  117. foreach my $item (split(/$DIRT([0-9]+)$DIRT/, $raw)) {
  118. if ($count % 2) { # deal with reference
  119. if ($dirty{$item}) { # dirty block
  120. if ($html) {
  121. push (@blocks, $html); # store what we have as a clean block
  122. push (@flags, 0);
  123. print $html; # flush what we have
  124. $html = '';
  125. }
  126. push (@blocks, $dirty{$item}); # store the raw fragment as dirty block
  127. push (@flags, 1);
  128. if (ref($protected{$item}) eq 'CODE') { # print stored html or execute code
  129. &{$protected{$item}};
  130. } else {
  131. print $protected{$item};
  132. }
  133. } else { # clean reference
  134. $html .= $protected{$item};
  135. }
  136. } else { # deal with normal text
  137. $html .= $item;
  138. }
  139. $count++;
  140. }
  141. if ($html) { # deal last bit of unprinted normal text
  142. print $html;
  143. push (@blocks, $html); # store what we have as a clean block
  144. push (@flags, 0);
  145. }
  146. return (join($FS, @blocks), join($FS, @flags));
  147. }
  148. sub SimpleRulesUnprotect {
  149. my $raw = shift;
  150. $raw =~ s/$PROT([0-9]+)$PROT/$protected{$1}/eg
  151. while $raw =~ /$PROT([0-9]+)$PROT/; # find recursive replacements!
  152. return $raw;
  153. }
  154. __DATA__
  155. This is the text page for the rules.
  156. This is a single paragraph.
  157. With a link to [[other paragraphs]].
  158. * This is a list
  159. with three items.
  160. * Second item.
  161. * Third item with a link: [[list items]].
  162. We also have numbered lists:
  163. 1. We use something like setext...
  164. 2. But we ~extend~ it.
  165. 3. **Really we do!**
  166. //multi-word emphasis// and
  167. __multi-word underlining__, and we also
  168. allow the similar /single/ _word_ *rules*.
  169. I think that's all the rules we [[implemented]].