compilation.pl 2.5 KB

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