compilation.pl 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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('compilation.pl', 'Compilation Extension');
  18. our ($q, $bol, %Action, %Page, $OpenPageName, $CollectingJournal, @MyRules);
  19. $Action{compilation} = \&DoCompilation;
  20. sub DoCompilation {
  21. my $match = GetParam('match', '') or ReportError(T('The match parameter is missing.'));
  22. print GetHeader('', Ts('Compilation for %s', $match), '');
  23. my @pages = PrintCompilation(undef, $match, GetParam('reverse', 0));
  24. print $q->p(Ts('%s pages found.', ($#pages + 1)));
  25. PrintFooter();
  26. }
  27. # like PrintJournal
  28. sub PrintCompilation {
  29. return if $CollectingJournal; # avoid infinite loops
  30. local $CollectingJournal = 1;
  31. my ($num, $regexp, $mode) = @_;
  32. return $q->p($q->strong(T('Compilation tag is missing a regular expression.'))) unless $regexp;
  33. my @pages = SearchTitleAndBody($regexp);
  34. if (defined &CompilationSort) {
  35. @pages = sort CompilationSort @pages;
  36. } else {
  37. @pages = sort @pages;
  38. }
  39. if ($mode eq 'reverse') {
  40. @pages = reverse @pages;
  41. }
  42. @pages = @pages[0 .. $num - 1] if $num and $#pages >= $num;
  43. if (@pages) {
  44. # Now save information required for saving the cache of the current page.
  45. local %Page;
  46. local $OpenPageName='';
  47. print '<div class="compilation">';
  48. PrintAllPages(1, 1, undef, undef, @pages);
  49. print '</div>';
  50. }
  51. return @pages;
  52. }
  53. push(@MyRules, \&CompilationRule);
  54. sub CompilationRule {
  55. if ($bol && m/\G(\&lt;compilation(\s+(\d*))?(\s+"(.*)")(\s+(reverse))?\&gt;[ \t]*\n?)/cgi) {
  56. # <journal 10 "regexp"> includes 10 pages matching regexp
  57. Clean(CloseHtmlEnvironments());
  58. Dirty($1);
  59. my $oldpos = pos;
  60. PrintCompilation($3, $5, $7);
  61. pos = $oldpos; # restore \G after call to ApplyRules
  62. return AddHtmlEnvironment('p');
  63. }
  64. return;
  65. }