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