paragraph-link.pl 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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('paragraph-link.pl', 'Paragraph Link Extension');
  18. our ($bol, $OpenPageName, %RuleOrder, @MyRules, $FreeLinkPattern, %PermanentAnchors, %PagePermanentAnchors);
  19. push(@MyRules, \&ParagraphLinkRule);
  20. # The [...] rule conflicts with the [new] in portrait-support.pl
  21. $RuleOrder{\&ParagraphLinkRule} = 100;
  22. sub ParagraphLinkRule {
  23. if ($bol && m/\G(\[(-)?$FreeLinkPattern\])/cg) {
  24. Dirty($1);
  25. my $invisible = $2;
  26. my $orig = $3;
  27. my $id = FreeToNormal($orig);
  28. my $text = $id;
  29. $text =~ s/_/ /g;
  30. my $html = ScriptLink(UrlEncode($id), $invisible ? '' : $text, 'permalink', $id,
  31. Ts('Permalink to "%s"', $orig));
  32. my ($class, $resolved, $title, $exists) = ResolveId($id);
  33. if ($class eq 'alias' and $title ne $OpenPageName) {
  34. $html .= ' [' . Ts('anchor first defined here: %s',
  35. ScriptLink(UrlEncode($resolved), $text, 'alias')) . ']';
  36. } elsif ($PermanentAnchors{$id} ne $OpenPageName
  37. and RequestLockDir('permanentanchors')) { # not fatal
  38. $PermanentAnchors{$id} = $OpenPageName;
  39. WritePermanentAnchors();
  40. ReleaseLockDir('permanentanchors');
  41. }
  42. $PagePermanentAnchors{$id} = 1; # add to the list of anchors in page
  43. $html .= ' [' . Ts('the page %s also exists',
  44. ScriptLink("action=browse;anchor=0;id="
  45. . UrlEncode($id), $id, 'local')) . ']'
  46. if $exists;
  47. print $html;
  48. return '';
  49. }
  50. return;
  51. }