2008-07-21 13:31:57 +02:00
|
|
|
[[!template id=plugin name=headinganchors author="[[PaulWise]]"]]
|
2008-02-11 05:00:30 +01:00
|
|
|
|
2009-06-05 22:24:46 +02:00
|
|
|
This is a simple plugin to add ids (which will serve as [[anchor]]s) to all headings, based on their text. It
|
2008-02-11 05:00:30 +01:00
|
|
|
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:
|
2007-08-09 08:34:20 +02:00
|
|
|
|
|
|
|
#!/usr/bin/perl
|
|
|
|
# quick HTML heading id adder by Paul Wise
|
|
|
|
package IkiWiki::Plugin::headinganchors;
|
|
|
|
|
|
|
|
use warnings;
|
|
|
|
use strict;
|
|
|
|
use IkiWiki 2.00;
|
|
|
|
|
2008-12-17 21:22:16 +01:00
|
|
|
sub import {
|
2007-08-09 08:34:20 +02:00
|
|
|
hook(type => "sanitize", id => "headinganchors", call => \&headinganchors);
|
2008-12-17 21:22:16 +01:00
|
|
|
}
|
2007-08-09 08:34:20 +02:00
|
|
|
|
|
|
|
sub text_to_anchor {
|
|
|
|
my $str = shift;
|
|
|
|
$str =~ s/^\s+//;
|
|
|
|
$str =~ s/\s+$//;
|
|
|
|
$str = lc($str);
|
|
|
|
$str =~ s/[&\?"\'\.,\(\)!]//mig;
|
|
|
|
$str =~ s/[^a-z]/_/mig;
|
|
|
|
return $str;
|
|
|
|
}
|
|
|
|
|
2008-12-17 21:22:16 +01:00
|
|
|
sub headinganchors (@) {
|
2007-08-09 08:34:20 +02:00
|
|
|
my %params=@_;
|
|
|
|
my $content=$params{content};
|
|
|
|
$content=~s{<h([0-9])>([^>]*)</h([0-9])>}{'<h'.$1.' id="'.text_to_anchor($2).'">'.$2.'</h'.$3.'>'}gie;
|
|
|
|
return $content;
|
2008-12-17 21:22:16 +01:00
|
|
|
}
|
2007-08-09 08:34:20 +02:00
|
|
|
|
|
|
|
1
|