<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">#!/usr/bin/perl
#
# Produce a hierarchical map of links.
#
# by Alessandro Dotti Contra &lt;alessandro@hyboria.org&gt;
#
# Revision: 0.2
package IkiWiki::Plugin::map;

use warnings;
use strict;
use IkiWiki 2.00;

sub import { #{{{
	hook(type =&gt; "preprocess", id =&gt; "map", call =&gt; \&amp;preprocess);
} # }}}

sub preprocess (@) { #{{{
	my %params=@_;
	$params{pages}="*" unless defined $params{pages};
	
	my $common_prefix;

	# Get all the items to map.
	my %mapitems;
	foreach my $page (keys %pagesources) {
		if (pagespec_match($page, $params{pages}, location =&gt; $params{page})) {
			$mapitems{$page}=1;

			# Check for a common prefix.
			if (! defined $common_prefix) {
				$common_prefix=$page;
			}
			elsif (length $common_prefix &amp;&amp;
			       $page !~ /^\Q$common_prefix\E(\/|$)/) {
				my @a=split(/\//, $page);
				my @b=split(/\//, $common_prefix);
				$common_prefix="";
				while (@a &amp;&amp; @b &amp;&amp; $a[0] eq $b[0]) {
					$common_prefix.=shift(@a);
					shift @b;
				}
			}
		}
	}
	
	# Common prefix should not be a page in the map.
	while (defined $common_prefix &amp;&amp; length $common_prefix &amp;&amp;
	       exists $mapitems{$common_prefix}) {
		$common_prefix=IkiWiki::dirname($common_prefix);
	}

	# Needs to update whenever a page is added or removed, so
	# register a dependency.
	add_depends($params{page}, $params{pages});
	# Explicitly add all currently shown pages, to detect when pages
	# are removed.
	add_depends($params{page}, join(" or ", keys %mapitems));

	# Create the map.
	my $parent="";
	my $indent=0;
	my $openli=0;
	my $dummy=0;
	my $map = "&lt;div class='map'&gt;\n&lt;ul&gt;\n";
	foreach my $item (sort keys %mapitems) {
		$item=~s/^\Q$common_prefix\E\///
			if defined $common_prefix &amp;&amp; length $common_prefix;
		my $depth = ($item =~ tr/\//\//) + 1;
		my $baseitem=IkiWiki::dirname($item);
		while (length $parent &amp;&amp; length $baseitem &amp;&amp; $baseitem !~ /^\Q$parent\E(\/|$)/) {
			$parent=IkiWiki::dirname($parent);
			last if !$dummy &amp;&amp; length $parent &amp;&amp; $baseitem =~ /^\Q$parent\E(\/|$)/;
			$indent--;
			$map .= "&lt;/li&gt;\n";
			if ($indent &gt; 0) {
				$map .= "&lt;/ul&gt;\n";
			}
		}
		$dummy=0;
		while ($depth &lt; $indent) {
			$indent--;
			$map .= "&lt;/li&gt;\n";
			if ($indent &gt; 0) {
				$map .= "&lt;/ul&gt;\n";
			}
		}
		my @bits=split("/", $item);
		my $p="";
		$p.="/".shift(@bits) for 1..$indent;
		while ($depth &gt; $indent) {
			$indent++;
			if ($indent &gt; 1) {
				$map .= "&lt;ul&gt;\n";
			}
			if ($depth &gt; $indent) {
				$dummy=1;
				$p.="/".shift(@bits);
				$map .= "&lt;li&gt;"
					.htmllink($params{page}, $params{destpage}, $p, class =&gt; "mapparent")
					."\n";
				$openli=1;
			}
			else {
				$openli=0;
			}
		}
		$map .= "&lt;/li&gt;\n" if $openli;
		$map .= "&lt;li&gt;"
			.htmllink($params{page}, $params{destpage}, 
				"/".$common_prefix."/".$item, class =&gt; "mapitem")
			."\n";
		$openli=1;
		$parent=$item;
	}
	while ($indent &gt; 0) {
		$indent--;
		$map .= "&lt;/li&gt;\n&lt;/ul&gt;\n";
	}
	$map .= "&lt;/div&gt;\n";
	return $map;
} # }}}

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