<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">#!/usr/bin/perl
# quick HTML heading id adder by Paul Wise
package IkiWiki::Plugin::headinganchors;

use warnings;
use strict;
use IkiWiki 3.00;
use URI::Escape;

sub import {
	hook(type =&gt; "getsetup", id =&gt; "headinganchors", call =&gt; \&amp;getsetup);
	hook(type =&gt; "sanitize", id =&gt; "headinganchors", call =&gt; \&amp;headinganchors);
}

sub getsetup () {
	return
		plugin =&gt; {
			safe =&gt; 1,
			rebuild =&gt; undef,
			section =&gt; "widget",
		},
}

sub text_to_anchor {
	my $str = shift;
	$str =~ s/^\s+//;
	$str =~ s/\s+$//;
	$str =~ s/\s/_/g;
	$str =~ s/"//g;
	$str =~ s/^[^a-zA-Z]/z-/; # must start with an alphabetical character
	$str = uri_escape_utf8($str);
	$str =~ s/%/./g;
	return $str;
}

sub headinganchors (@) {
	my %params=@_;
	my $content=$params{content};
	$content=~s{&lt;h([0-9])&gt;([^&gt;]*)&lt;/h([0-9])&gt;}{'&lt;h'.$1.' id="'.text_to_anchor($2).'"&gt;'.$2.'&lt;/h'.$3.'&gt;'}gie;
	return $content;
}

1
</pre></body></html>