<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">#!/usr/bin/perl
package IkiWiki::Plugin::toggle;

use warnings;
use strict;
use IkiWiki 2.00;

# Here's the javascript that makes this possible. A key feature is the use
# of css to hide toggleables, to avoid any flashing on page load. The css
# is only emitted after the javascript tests that it's going to be able to
# show the toggleables.
our $javascript=&lt;&lt;'EOF';
&lt;script type="text/javascript"&gt;
&lt;!--
if (document.getElementById &amp;&amp; document.getElementsByTagName &amp;&amp; document.createTextNode) {
	document.write('&lt;style type="text/css"&gt;div.toggleable { display: none; }&lt;/style&gt;');
	window.onload = inittoggle;
}

function inittoggle() {
	var as = getElementsByClass('toggle');
	for (var i = 0; i &lt; as.length; i++) {
		var id = as[i].href.match(/#(\w.+)/)[1];
		if (document.getElementById(id).className == "toggleable")
			document.getElementById(id).style.display="none";
		as[i].onclick = function() {
			toggle(this);
			return false;
		}
	}
}

function toggle(s) {
	var id = s.href.match(/#(\w.+)/)[1];
	style = document.getElementById(id).style;
	if (style.display == "none")
		style.display = "block";
	else
		style.display = "none";
}

function getElementsByClass(cls, node, tag) {
	if (document.getElementsByClass)
		return document.getElementsByClass(cls, node, tag);
	if (! node) node = document;
	if (! tag) tag = '*';
	var ret = new Array();
	var pattern = new RegExp("(^|\\s)"+cls+"(\\s|$)");
	var els = node.getElementsByTagName(tag);
	for (i = 0; i &lt; els.length; i++) {
		if ( pattern.test(els[i].className) ) {
			ret.push(els[i]);
		}
	}
	return ret;
}

//--&gt;
&lt;/script&gt;
EOF

sub import { #{{{
	hook(type =&gt; "preprocess", id =&gt; "toggle",
		call =&gt; \&amp;preprocess_toggle);
	hook(type =&gt; "preprocess", id =&gt; "toggleable",
		call =&gt; \&amp;preprocess_toggleable);
	hook(type =&gt; "format", id =&gt; "toggle", call =&gt; \&amp;format);
} # }}}

sub genid ($$) { #{{{
	my $page=shift;
	my $id=shift;

	$id="$page.$id";

	# make it a legal html id attribute
	$id=~s/[^-a-zA-Z0-9.]/-/g;
	if ($id !~ /^[a-zA-Z]/) {
		$id="id$id";
	}
	return $id;
} #}}}

sub preprocess_toggle (@) { #{{{
	my %params=(id =&gt; "default", text =&gt; "more", @_);

	my $id=genid($params{page}, $params{id});
	return "&lt;a class=\"toggle\" href=\"#$id\"&gt;$params{text}&lt;/a&gt;";
} # }}}

sub preprocess_toggleable (@) { #{{{
	my %params=(id =&gt; "default", text =&gt; "", open =&gt; "no", @_);

	# Preprocess the text to expand any preprocessor directives
	# embedded inside it.
	$params{text}=IkiWiki::preprocess($params{page}, $params{destpage}, 
		IkiWiki::filter($params{page}, $params{destpage}, $params{text}));
	
	my $id=genid($params{page}, $params{id});
	my $class=(lc($params{open}) ne "yes") ? "toggleable" : "toggleable-open";

	# Should really be a postprocessor directive, oh well. Work around
	# markdown's dislike of markdown inside a &lt;div&gt; with various funky
	# whitespace.
	my ($indent)=$params{text}=~/( +)$/;
	$indent="" unless defined $indent;
	return "&lt;div class=\"$class\" id=\"$id\"&gt;&lt;/div&gt;\n\n$params{text}\n$indent&lt;div class=\"toggleableend\"&gt;&lt;/div&gt;";
} # }}}

sub format (@) { #{{{
        my %params=@_;

	if ($params{content}=~s!(&lt;div class="toggleable(?:-open)?" id="[^"]+"&gt;)&lt;/div&gt;!$1!g) {
		$params{content}=~s/&lt;div class="toggleableend"&gt;//g;
		if (! ($params{content}=~s!^&lt;body&gt;!&lt;body&gt;$javascript!m)) {
			# no &lt;/body&gt; tag, probably in preview mode
			$params{content}=$javascript.$params{content};
		}
	}
	return $params{content};
} # }}}

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