]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - doc/plugins/contrib/headinganchors.mdwn
* Remove .otl file from sandbox to avoid build ugliness. Closes: #454181
[git.ikiwiki.info.git] / doc / plugins / contrib / headinganchors.mdwn
1 This is a simple plugin to add ids to all headings, based on their text. It works as a postprocessing filter, allowing it to work on mdwn, wiki, html, rst and any other format that produces html. The code is available here:
3         #!/usr/bin/perl
4         # quick HTML heading id adder by Paul Wise
5         package IkiWiki::Plugin::headinganchors;
7         use warnings;
8         use strict;
9         use IkiWiki 2.00;
11         sub import { #{{{
12                 hook(type => "sanitize", id => "headinganchors", call => \&headinganchors);
13         } # }}}
15         sub text_to_anchor {
16                 my $str = shift;
17                 $str =~ s/^\s+//;
18                 $str =~ s/\s+$//;
19                 $str = lc($str);
20                 $str =~ s/[&\?"\'\.,\(\)!]//mig;
21                 $str =~ s/[^a-z]/_/mig;
22                 return $str;
23         }
25         sub headinganchors (@) { #{{{
26                 my %params=@_;
27                 my $content=$params{content};
28                 $content=~s{<h([0-9])>([^>]*)</h([0-9])>}{'<h'.$1.' id="'.text_to_anchor($2).'">'.$2.'</h'.$3.'>'}gie;
29                 return $content;
30         } # }}}
32         1