summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/camelcase.pm
blob: f9ee30fe288456666d8008045a589c5db24f3366 (plain)
  1. #!/usr/bin/perl
  2. # CamelCase links
  3. package IkiWiki::Plugin::camelcase;
  4. use warnings;
  5. use strict;
  6. use IkiWiki 2.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. camelcase_ignore => {
  34. type => "string",
  35. example => [],
  36. description => "list of words to not turn into links",
  37. safe => 1,
  38. rebuild => undef, # might change links
  39. },
  40. }
  41. sub linkify (@) {
  42. my %params=@_;
  43. my $page=$params{page};
  44. my $destpage=$params{destpage};
  45. $params{content}=~s{$link_regexp}{
  46. ignored($1) ? $1 : htmllink($page, $destpage, linkpage($1))
  47. }eg;
  48. return $params{content};
  49. }
  50. sub scan (@) {
  51. my %params=@_;
  52. my $page=$params{page};
  53. my $content=$params{content};
  54. while ($content =~ /$link_regexp/g) {
  55. push @{$links{$page}}, linkpage($1) unless ignored($1)
  56. }
  57. }
  58. sub ignored ($) {
  59. my $word=lc shift;
  60. grep { $word eq lc $_ } @{$config{'camelcase_ignore'}}
  61. }
  62. 1