summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/link.pm
blob: 24579057ccb159450bf5d9a49e892be848747eb6 (plain)
  1. #!/usr/bin/perl
  2. package IkiWiki::Plugin::link;
  3. use warnings;
  4. use strict;
  5. use IkiWiki 2.00;
  6. my $link_regexp;
  7. sub import { #{{{
  8. hook(type => "checkconfig", id => "link", call => \&checkconfig);
  9. hook(type => "linkify", id => "link", call => \&linkify);
  10. hook(type => "scan", id => "link", call => \&scan);
  11. } # }}}
  12. sub checkconfig () { #{{{
  13. if ($config{prefix_directives}) {
  14. $link_regexp = qr{
  15. \[\[(?=[^!]) # beginning of link
  16. (?:
  17. ([^\]\|]+) # 1: link text
  18. \| # followed by '|'
  19. )? # optional
  20. ([^\n\r\]#]+) # 2: page to link to
  21. (?:
  22. \# # '#', beginning of anchor
  23. ([^\s\]]+) # 3: anchor text
  24. )? # optional
  25. \]\] # end of link
  26. }x;
  27. }
  28. else {
  29. $link_regexp = qr{
  30. \[\[ # beginning of link
  31. (?:
  32. ([^\]\|\n\s]+) # 1: link text
  33. \| # followed by '|'
  34. )? # optional
  35. ([^\s\]#]+) # 2: page to link to
  36. (?:
  37. \# # '#', beginning of anchor
  38. ([^\s\]]+) # 3: anchor text
  39. )? # optional
  40. \]\] # end of link
  41. }x,
  42. }
  43. } #}}}
  44. sub linkify (@) { #{{{
  45. my %params=@_;
  46. my $page=$params{page};
  47. my $destpage=$params{destpage};
  48. $params{content} =~ s{(\\?)$link_regexp}{
  49. defined $2
  50. ? ( $1
  51. ? "[[$2|$3".($4 ? "#$4" : "")."]]"
  52. : htmllink($page, $destpage, IkiWiki::linkpage($3),
  53. anchor => $4, linktext => IkiWiki::pagetitle($2)))
  54. : ( $1
  55. ? "[[$3".($4 ? "#$4" : "")."]]"
  56. : htmllink($page, $destpage, IkiWiki::linkpage($3),
  57. anchor => $4))
  58. }eg;
  59. return $params{content};
  60. } #}}}
  61. sub scan (@) { #{{{
  62. my %params=@_;
  63. my $page=$params{page};
  64. my $content=$params{content};
  65. while ($content =~ /(?<!\\)$link_regexp/g) {
  66. push @{$links{$page}}, IkiWiki::linkpage($2);
  67. }
  68. } # }}}
  69. 1