summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/link.pm
blob: 0638d4bddd0b1d82bd54714601f80d9582d18918 (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 => "getsetup", id => "link", call => \&getsetup);
  9. hook(type => "checkconfig", id => "link", call => \&checkconfig);
  10. hook(type => "linkify", id => "link", call => \&linkify);
  11. hook(type => "scan", id => "link", call => \&scan);
  12. hook(type => "renamepage", id => "link", call => \&renamepage);
  13. } # }}}
  14. sub getsetup () { #{{{
  15. return
  16. plugin => {
  17. safe => 1,
  18. rebuild => 1,
  19. },
  20. } #}}}
  21. sub checkconfig () { #{{{
  22. if ($config{prefix_directives}) {
  23. $link_regexp = qr{
  24. \[\[(?=[^!]) # beginning of link
  25. (?:
  26. ([^\]\|]+) # 1: link text
  27. \| # followed by '|'
  28. )? # optional
  29. ([^\n\r\]#]+) # 2: page to link to
  30. (?:
  31. \# # '#', beginning of anchor
  32. ([^\s\]]+) # 3: anchor text
  33. )? # optional
  34. \]\] # end of link
  35. }x;
  36. }
  37. else {
  38. $link_regexp = qr{
  39. \[\[ # beginning of link
  40. (?:
  41. ([^\]\|\n\s]+) # 1: link text
  42. \| # followed by '|'
  43. )? # optional
  44. ([^\s\]#]+) # 2: page to link to
  45. (?:
  46. \# # '#', beginning of anchor
  47. ([^\s\]]+) # 3: anchor text
  48. )? # optional
  49. \]\] # end of link
  50. }x,
  51. }
  52. } #}}}
  53. sub linkify (@) { #{{{
  54. my %params=@_;
  55. my $page=$params{page};
  56. my $destpage=$params{destpage};
  57. $params{content} =~ s{(\\?)$link_regexp}{
  58. defined $2
  59. ? ( $1
  60. ? "[[$2|$3".($4 ? "#$4" : "")."]]"
  61. : htmllink($page, $destpage, linkpage($3),
  62. anchor => $4, linktext => pagetitle($2)))
  63. : ( $1
  64. ? "[[$3".($4 ? "#$4" : "")."]]"
  65. : htmllink($page, $destpage, linkpage($3),
  66. anchor => $4))
  67. }eg;
  68. return $params{content};
  69. } #}}}
  70. sub scan (@) { #{{{
  71. my %params=@_;
  72. my $page=$params{page};
  73. my $content=$params{content};
  74. while ($content =~ /(?<!\\)$link_regexp/g) {
  75. push @{$links{$page}}, linkpage($2);
  76. }
  77. } # }}}
  78. sub renamepage (@) { #{{{
  79. my %params=@_;
  80. my $page=$params{page};
  81. my $old=$params{oldpage};
  82. my $new=$params{newpage};
  83. $params{content} =~ s{(?<!\\)$link_regexp}{
  84. my $linktext=$2;
  85. my $link=$linktext;
  86. if (bestlink($page, linkpage($linktext)) eq $old) {
  87. $link=pagetitle($new, 1);
  88. $link=~s/ /_/g;
  89. if ($linktext =~ m/.*\/*?[A-Z]/) {
  90. # preserve leading cap of last component
  91. my @bits=split("/", $link);
  92. $link=join("/", @bits[0..$#bits-1], ucfirst($bits[$#bits]));
  93. }
  94. if (index($linktext, "/") == 0) {
  95. # absolute link
  96. $link="/$link";
  97. }
  98. }
  99. defined $1
  100. ? ( "[[$1|$link".($3 ? "#$3" : "")."]]" )
  101. : ( "[[$link". ($3 ? "#$3" : "")."]]" )
  102. }eg;
  103. return $params{content};
  104. } #}}}
  105. 1