]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki/Plugin/headinganchors.pm
80b39ec05c7262c37c4a2113f1fa2c4b4aca5ca6
[ikiwiki.git] / 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 2.00;
8
9 sub import {
10         hook(type => "sanitize", id => "headinganchors", call => \&headinganchors);
11 }
12
13 sub text_to_anchor {
14         my $str = shift;
15         $str =~ s/^\s+//;
16         $str =~ s/\s+$//;
17         $str = lc($str);
18         $str =~ s/[&\?"\'\.,\(\)!]//mig;
19         $str =~ s/[^a-z]/_/mig;
20         return $str;
21 }
22
23 sub headinganchors (@) {
24         my %params=@_;
25         my $content=$params{content};
26         $content=~s{<h([0-9])>([^>]*)</h([0-9])>}{'<h'.$1.' id="'.text_to_anchor($2).'">'.$2.'</h'.$3.'>'}gie;
27         return $content;
28 }
29
30 1