]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki/Plugin/camelcase.pm
formatting
[ikiwiki.git] / IkiWiki / Plugin / camelcase.pm
1 #!/usr/bin/perl
2 # CamelCase links
3 package IkiWiki::Plugin::camelcase;
4
5 use warnings;
6 use strict;
7 use IkiWiki 3.00;
8
9 # This regexp is based on the one in Text::WikiFormat.
10 my $link_regexp=qr{
11         (?<![^A-Za-z0-9\s])     # try to avoid expanding non-links with a
12                                 # zero width negative lookbehind for
13                                 # characters that suggest it's not a link
14         \b                      # word boundry
15         (
16                 (?:
17                         [A-Z]           # Uppercase start
18                         [a-z0-9]        # followed by lowercase
19                         \w*             # and rest of word
20                 )
21                 {2,}                    # repeated twice
22         )
23 }x;
24
25 sub import {
26         hook(type => "getsetup", id => "camelcase", call => \&getsetup);
27         hook(type => "linkify", id => "camelcase", call => \&linkify);
28         hook(type => "scan", id => "camelcase", call => \&scan);
29 }
30
31 sub getsetup () {
32         return
33                 plugin => {
34                         safe => 1,
35                         rebuild => undef,
36                 },
37                 camelcase_ignore => {
38                         type => "string",
39                         example => [],
40                         description => "list of words to not turn into links",
41                         safe => 1,
42                         rebuild => undef, # might change links
43                 },
44 }
45
46 sub linkify (@) {
47         my %params=@_;
48         my $page=$params{page};
49         my $destpage=$params{destpage};
50
51         $params{content}=~s{$link_regexp}{
52                 ignored($1) ? $1 : htmllink($page, $destpage, linkpage($1))
53         }eg;
54
55         return $params{content};
56 }
57
58 sub scan (@) {
59         my %params=@_;
60         my $page=$params{page};
61         my $content=$params{content};
62
63         while ($content =~ /$link_regexp/g) {
64                 add_link($page, linkpage($1)) unless ignored($1)
65         }
66 }
67
68 sub ignored ($) {
69         my $word=lc shift;
70         grep { $word eq lc $_ } @{$config{'camelcase_ignore'}}
71 }
72
73 1