crumbs.pl 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. # Copyright (C) 2004, 2005 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('crumbs.pl', 'List Parent Pages Extension');
  21. our ($q, %RuleOrder, @MyRules, $LinkPattern, $FreeLinks, $FreeLinkPattern, $WikiLinks);
  22. push(@MyRules, \&CrumbsRule);
  23. $RuleOrder{\&CrumbsRule} = -10; # run before default rules!
  24. sub CrumbsRule {
  25. if (not (pos) # first!
  26. and (($WikiLinks && /\G($LinkPattern\n)/cg)
  27. or ($FreeLinks && /\G(\[\[$FreeLinkPattern\]\]\n)/cg))) {
  28. my $oldpos = pos; # will be trashed below
  29. my $cluster = FreeToNormal($2);
  30. my %seen = ($cluster => 1);
  31. my @links = ($cluster);
  32. AllPagesList(); # set IndexHash
  33. while ($cluster) {
  34. my $text = GetPageContent($cluster); # opening n files is slow!
  35. if (($WikiLinks && $text =~ /^$LinkPattern\n/)
  36. or ($FreeLinks && $text =~ /^\[\[$FreeLinkPattern\]\]\n/)) {
  37. $cluster = FreeToNormal($1);
  38. }
  39. last if not $cluster or $seen{$cluster};
  40. $seen{$cluster} = 1;
  41. push(@links, $cluster);
  42. }
  43. my $result = $q->span({-class=>'crumbs'}, map { GetPageLink($_) } reverse(@links));
  44. pos = $oldpos; # set after $_ is set!
  45. return $result; # clean rule, will be cached!
  46. }
  47. return;
  48. }