anchors.pl 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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('anchors.pl', 'Local Anchor Extension');
  17. our ($q, %Page, $FootnoteNumber, $FreeLinkPattern, @MyRules, $BracketWiki);
  18. push(@MyRules, \&AnchorsRule);
  19. sub AnchorsRule {
  20. if (m/\G\[\[\#$FreeLinkPattern\]\]/cg) {
  21. return $q->a({-href=>'#' . FreeToNormal($1), -class=>'local anchor'}, $1);
  22. } elsif ($BracketWiki && m/\G\[\[\#$FreeLinkPattern\|([^\]]+)\]\]/cg) {
  23. return $q->a({-href=>'#' . FreeToNormal($1), -class=>'local anchor'}, $2);
  24. } elsif ($BracketWiki && m/\G(\[\[$FreeLinkPattern\#$FreeLinkPattern\|([^\]]+)\]\])/cg
  25. or m/\G(\[\[\[$FreeLinkPattern\#$FreeLinkPattern\]\]\])/cg
  26. or m/\G(\[\[$FreeLinkPattern\#$FreeLinkPattern\]\])/cg) {
  27. # This one is not a dirty rule because the output is always a page
  28. # link, never an edit link (unlike normal free links).
  29. my $bracket = (substr($1, 0, 3) eq '[[[');
  30. my $id = $2 . '#' . $3;
  31. my $text = $4;
  32. my $class = 'local anchor';
  33. my $title = '';
  34. $id = FreeToNormal($id);
  35. if (!$text && $bracket) {
  36. $text = BracketLink(++$FootnoteNumber); # s/_/ /g happens further down!
  37. $class .= ' number';
  38. # Since we're displaying a number such as [1], the title attribute should tell us where this will go.
  39. $title = "$2 ($3)";
  40. # The user might have writen [[[FooBar#one two]]] or [[[FooBar#one_two]]]
  41. $title =~ s/_/ /g;
  42. }
  43. $text = $id unless $text;
  44. $text =~ s/_/ /g;
  45. return ScriptLink(UrlEncode($id), $text, $class, undef, $title);
  46. } elsif (m/\G\[\:$FreeLinkPattern\]/cg) {
  47. return $q->a({-name=>FreeToNormal($1), -class=>'anchor'}, '');
  48. }
  49. return;
  50. }
  51. *OldAnchorsBrowsePage=\&BrowsePage;
  52. *BrowsePage=\&NewAnchorsBrowsePage;
  53. sub NewAnchorsBrowsePage {
  54. my ($id) = @_;
  55. OpenPage($id);
  56. if (not GetParam('revision', '')
  57. and not GetParam('oldid', '')
  58. and $Page{text} =~ /^\#REDIRECT\s+\[\[$FreeLinkPattern\#$FreeLinkPattern\]\]/) {
  59. return ReBrowsePage(FreeToNormal($1 . '#' . $2), $id);
  60. }
  61. return OldAnchorsBrowsePage(@_);
  62. }