add websetup hook
[ikiwiki] / IkiWiki / Plugin / headinganchors.pm
1 #!/usr/bin/perl
2 # quick HTML heading id adder by Paul Wise
3 package IkiWiki::Plugin::headinganchors;
4
5 use warnings;
6 use strict;
7 use IkiWiki 3.00;
8
9 sub import {
10         hook(type => "getsetup", id => "headinganchors", call => \&getsetup);
11         hook(type => "sanitize", id => "headinganchors", call => \&headinganchors);
12 }
13
14 sub getsetup () {
15         return
16                 plugin => {
17                         safe => 1,
18                         rebuild => undef,
19                         section => "widget",
20                 },
21 }
22
23 sub text_to_anchor {
24         my $str = shift;
25         $str =~ s/^\s+//;
26         $str =~ s/\s+$//;
27         $str = lc($str);
28         $str =~ s/[&\?"\'\.,\(\)!]//mig;
29         $str =~ s/[^a-z]/_/mig;
30         return $str;
31 }
32
33 sub headinganchors (@) {
34         my %params=@_;
35         my $content=$params{content};
36         $content=~s{<h([0-9])>([^>]*)</h([0-9])>}{'<h'.$1.' id="'.text_to_anchor($2).'">'.$2.'</h'.$3.'>'}gie;
37         return $content;
38 }
39
40 1