summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/camelcase.pm
blob: bda980d2862e85ba23e670699ec2fa577b21c89f (plain)
  1. #!/usr/bin/perl
  2. # CamelCase links
  3. package IkiWiki::Plugin::camelcase;
  4. use warnings;
  5. use strict;
  6. use IkiWiki 3.00;
  7. # This regexp is based on the one in Text::WikiFormat.
  8. my $link_regexp=qr{
  9. (?<![^A-Za-z0-9\s]) # try to avoid expanding non-links with a
  10. # zero width negative lookbehind for
  11. # characters that suggest it's not a link
  12. \b # word boundry
  13. (
  14. (?:
  15. [A-Z] # Uppercase start
  16. [a-z0-9] # followed by lowercase
  17. \w* # and rest of word
  18. )
  19. {2,} # repeated twice
  20. )
  21. }x;
  22. sub import {
  23. hook(type => "getsetup", id => "camelcase", call => \&getsetup);
  24. hook(type => "linkify", id => "camelcase", call => \&linkify);
  25. hook(type => "scan", id => "camelcase", call => \&scan);
  26. }
  27. sub getsetup () {
  28. return
  29. plugin => {
  30. safe => 1,
  31. rebuild => undef,
  32. };
  33. }
  34. sub linkify (@) {
  35. my %params=@_;
  36. my $page=$params{page};
  37. my $destpage=$params{destpage};
  38. $params{content}=~s{$link_regexp}{
  39. htmllink($page, $destpage, linkpage($1))
  40. }eg;
  41. return $params{content};
  42. }
  43. sub scan (@) {
  44. my %params=@_;
  45. my $page=$params{page};
  46. my $content=$params{content};
  47. while ($content =~ /$link_regexp/g) {
  48. push @{$links{$page}}, linkpage($1);
  49. }
  50. }
  51. 1