link-all.pl 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. # Copyright (C) 2004–2015 Alex Schroeder <alex@gnu.org>
  2. #
  3. # This program is free software: you can redistribute it and/or modify it under
  4. # the terms of the GNU General Public License as published by the Free Software
  5. # Foundation, either version 3 of the License, or (at your option) any later
  6. # version.
  7. #
  8. # This program is distributed in the hope that it will be useful, but WITHOUT
  9. # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  10. # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  11. #
  12. # You should have received a copy of the GNU General Public License along with
  13. # this program. If not, see <http://www.gnu.org/licenses/>.
  14. use strict;
  15. use v5.10;
  16. AddModuleDescription('link-all.pl', 'Link All Words Extension');
  17. our (%IndexHash, %RuleOrder, @MyRules, $UserGotoBar, $ScriptName);
  18. push(@MyRules, \&LinkAllRule);
  19. $RuleOrder{\&LinkAllRule} = 1000;
  20. sub LinkAllRule {
  21. if (/\G([A-Za-z\x{0080}-\x{fffd}]+)/cg) {
  22. my $oldpos = pos;
  23. Dirty($1);
  24. # print the word, or the link to the word
  25. print LinkAllGetPageLinkIfItExists($1);
  26. pos = $oldpos; # protect against changes in pos
  27. # the block is cached so we don't return anything
  28. return '';
  29. }
  30. return;
  31. }
  32. sub LinkAllGetPageLinkIfItExists {
  33. my $id = shift;
  34. AllPagesList();
  35. if ($IndexHash{$id}) {
  36. return GetPageLink($id);
  37. } elsif (GetParam('define', 0)) {
  38. return GetEditLink($id, $id);
  39. } else {
  40. return $id;
  41. }
  42. }
  43. *OldLinkAllGetGotoBar = \&GetGotoBar;
  44. *GetGotoBar = \&NewLinkAllGetGotoBar;
  45. sub NewLinkAllGetGotoBar {
  46. my $id = shift;
  47. my $define = T('Define');
  48. my $addition = "<a href=\"$ScriptName?action=browse;id=$id;define=1\">$define</a>";
  49. if (index($UserGotoBar, $addition) < 0) {
  50. $UserGotoBar .= ' ' if $UserGotoBar;
  51. $UserGotoBar .= $addition;
  52. }
  53. return OldLinkAllGetGotoBar();
  54. }