2006-11-22 03:28:42 +01:00
|
|
|
#!/usr/bin/perl
|
|
|
|
package IkiWiki::Plugin::toggle;
|
|
|
|
|
|
|
|
use warnings;
|
|
|
|
use strict;
|
2007-04-27 04:55:52 +02:00
|
|
|
use IkiWiki 2.00;
|
2006-11-22 03:28:42 +01: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.
|
2008-07-02 21:33:52 +02:00
|
|
|
our $javascript=<<'EOF';
|
2006-11-22 03:28:42 +01:00
|
|
|
<script type="text/javascript">
|
|
|
|
<!--
|
|
|
|
if (document.getElementById && document.getElementsByTagName && document.createTextNode) {
|
|
|
|
document.write('<style type="text/css">div.toggleable { display: none; }</style>');
|
|
|
|
window.onload = inittoggle;
|
|
|
|
}
|
|
|
|
|
|
|
|
function inittoggle() {
|
|
|
|
var as = getElementsByClass('toggle');
|
|
|
|
for (var i = 0; i < as.length; i++) {
|
|
|
|
var id = as[i].href.match(/#(\w.+)/)[1];
|
2008-07-02 21:33:52 +02:00
|
|
|
if (document.getElementById(id).className == "toggleable")
|
|
|
|
document.getElementById(id).style.display="none";
|
2006-11-22 03:28:42 +01:00
|
|
|
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";
|
|
|
|
}
|
|
|
|
|
2008-07-25 07:43:37 +02:00
|
|
|
function getElementsByClass(cls, node, tag) {
|
|
|
|
if (document.getElementsByClass)
|
|
|
|
return document.getElementsByClass(cls, node, tag);
|
|
|
|
if (! node) node = document;
|
|
|
|
if (! tag) tag = '*';
|
2006-11-22 03:28:42 +01:00
|
|
|
var ret = new Array();
|
2008-07-25 07:43:37 +02:00
|
|
|
var pattern = new RegExp("(^|\\s)"+cls+"(\\s|$)");
|
|
|
|
var els = node.getElementsByTagName(tag);
|
|
|
|
for (i = 0; i < els.length; i++) {
|
2006-11-22 03:28:42 +01:00
|
|
|
if ( pattern.test(els[i].className) ) {
|
2008-07-25 07:43:37 +02:00
|
|
|
ret.push(els[i]);
|
2006-11-22 03:28:42 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
2008-07-25 07:43:37 +02:00
|
|
|
|
2006-11-22 03:28:42 +01:00
|
|
|
//-->
|
|
|
|
</script>
|
|
|
|
EOF
|
|
|
|
|
|
|
|
sub import { #{{{
|
|
|
|
hook(type => "preprocess", id => "toggle",
|
|
|
|
call => \&preprocess_toggle);
|
|
|
|
hook(type => "preprocess", id => "toggleable",
|
2006-11-26 20:45:39 +01:00
|
|
|
call => \&preprocess_toggleable);
|
2006-11-22 03:28:42 +01:00
|
|
|
hook(type => "format", id => "toggle", call => \&format);
|
|
|
|
} # }}}
|
|
|
|
|
2006-11-22 05:26:44 +01:00
|
|
|
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;
|
|
|
|
} #}}}
|
|
|
|
|
2006-11-22 03:28:42 +01:00
|
|
|
sub preprocess_toggle (@) { #{{{
|
|
|
|
my %params=(id => "default", text => "more", @_);
|
|
|
|
|
2006-11-22 05:26:44 +01:00
|
|
|
my $id=genid($params{page}, $params{id});
|
2008-07-02 22:02:01 +02:00
|
|
|
return "<a class=\"toggle\" href=\"#$id\">$params{text}</a>";
|
2006-11-22 03:28:42 +01:00
|
|
|
} # }}}
|
|
|
|
|
|
|
|
sub preprocess_toggleable (@) { #{{{
|
2008-07-02 22:02:01 +02:00
|
|
|
my %params=(id => "default", text => "", open => "no", @_);
|
2006-11-22 03:28:42 +01:00
|
|
|
|
|
|
|
# Preprocess the text to expand any preprocessor directives
|
2006-11-26 20:45:39 +01:00
|
|
|
# embedded inside it.
|
2007-02-14 01:11:19 +01:00
|
|
|
$params{text}=IkiWiki::preprocess($params{page}, $params{destpage},
|
2007-05-17 21:55:11 +02:00
|
|
|
IkiWiki::filter($params{page}, $params{destpage}, $params{text}));
|
2006-11-22 05:26:44 +01:00
|
|
|
|
|
|
|
my $id=genid($params{page}, $params{id});
|
2008-07-02 22:02:01 +02:00
|
|
|
my $class=(lc($params{open}) ne "yes") ? "toggleable" : "toggleable-open";
|
2006-11-22 03:28:42 +01:00
|
|
|
|
|
|
|
# Should really be a postprocessor directive, oh well. Work around
|
2006-11-22 15:28:38 +01:00
|
|
|
# markdown's dislike of markdown inside a <div> with various funky
|
|
|
|
# whitespace.
|
|
|
|
my ($indent)=$params{text}=~/( +)$/;
|
|
|
|
$indent="" unless defined $indent;
|
2008-07-02 22:02:01 +02:00
|
|
|
return "<div class=\"$class\" id=\"$id\"></div>\n\n$params{text}\n$indent<div class=\"toggleableend\"></div>";
|
2006-11-22 03:28:42 +01:00
|
|
|
} # }}}
|
|
|
|
|
|
|
|
sub format (@) { #{{{
|
|
|
|
my %params=@_;
|
|
|
|
|
2008-07-27 00:05:22 +02:00
|
|
|
if ($params{content}=~s!(<div class="toggleable(?:-open)?" id="[^"]+">\s*)</div>!$1!g) {
|
2006-11-22 03:28:42 +01:00
|
|
|
$params{content}=~s/<div class="toggleableend">//g;
|
2008-07-02 22:14:18 +02:00
|
|
|
if (! ($params{content}=~s!^<body>!<body>$javascript!m)) {
|
2008-06-29 05:08:24 +02:00
|
|
|
# no </body> tag, probably in preview mode
|
2008-07-02 22:14:18 +02:00
|
|
|
$params{content}=$javascript.$params{content};
|
2008-06-29 05:08:24 +02:00
|
|
|
}
|
2006-11-22 03:28:42 +01:00
|
|
|
}
|
|
|
|
return $params{content};
|
|
|
|
} # }}}
|
|
|
|
|
|
|
|
1
|