2006-11-22 03:28:42 +01:00
|
|
|
#!/usr/bin/perl
|
|
|
|
package IkiWiki::Plugin::toggle;
|
|
|
|
|
|
|
|
use warnings;
|
|
|
|
use strict;
|
2008-12-23 22:34:19 +01:00
|
|
|
use IkiWiki 3.00;
|
2006-11-22 03:28:42 +01:00
|
|
|
|
2008-12-17 21:22:16 +01:00
|
|
|
sub import {
|
2008-10-18 02:28:18 +02:00
|
|
|
add_underlay("javascript");
|
2008-08-04 01:35:35 +02:00
|
|
|
hook(type => "getsetup", id => "toggle", call => \&getsetup);
|
2006-11-22 03:28:42 +01:00
|
|
|
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);
|
2008-12-17 21:22:16 +01:00
|
|
|
}
|
2006-11-22 03:28:42 +01:00
|
|
|
|
2008-12-17 21:22:16 +01:00
|
|
|
sub getsetup () {
|
2008-08-04 01:35:35 +02:00
|
|
|
return
|
|
|
|
plugin => {
|
|
|
|
safe => 1,
|
|
|
|
rebuild => undef,
|
|
|
|
},
|
2008-12-17 21:22:16 +01:00
|
|
|
}
|
2008-08-04 01:35:35 +02:00
|
|
|
|
2008-12-17 21:22:16 +01:00
|
|
|
sub genid ($$) {
|
2006-11-22 05:26:44 +01:00
|
|
|
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;
|
2008-12-17 21:22:16 +01:00
|
|
|
}
|
2006-11-22 05:26:44 +01:00
|
|
|
|
2008-12-17 21:22:16 +01:00
|
|
|
sub preprocess_toggle (@) {
|
2006-11-22 03:28:42 +01:00
|
|
|
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>";
|
2008-12-17 21:22:16 +01:00
|
|
|
}
|
2006-11-22 03:28:42 +01:00
|
|
|
|
2008-12-17 21:22:16 +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>";
|
2008-12-17 21:22:16 +01:00
|
|
|
}
|
2006-11-22 03:28:42 +01:00
|
|
|
|
2008-12-17 21:22:16 +01:00
|
|
|
sub format (@) {
|
2006-11-22 03:28:42 +01:00
|
|
|
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;
|
2009-09-28 22:21:03 +02:00
|
|
|
if (! ($params{content}=~s!^(<body[^>]*>)!$1.include_javascript($params{page})!em)) {
|
2008-06-29 05:08:24 +02:00
|
|
|
# no </body> tag, probably in preview mode
|
2008-10-18 02:28:18 +02:00
|
|
|
$params{content}=include_javascript($params{page}, 1).$params{content};
|
2008-06-29 05:08:24 +02:00
|
|
|
}
|
2006-11-22 03:28:42 +01:00
|
|
|
}
|
|
|
|
return $params{content};
|
2008-12-17 21:22:16 +01:00
|
|
|
}
|
2006-11-22 03:28:42 +01:00
|
|
|
|
2008-12-17 21:22:16 +01:00
|
|
|
sub include_javascript ($;$) {
|
2008-10-18 02:28:18 +02:00
|
|
|
my $page=shift;
|
|
|
|
my $absolute=shift;
|
|
|
|
|
|
|
|
return '<script src="'.urlto("ikiwiki.js", $page, $absolute).
|
|
|
|
'" type="text/javascript" charset="utf-8"></script>'."\n".
|
|
|
|
'<script src="'.urlto("toggle.js", $page, $absolute).
|
|
|
|
'" type="text/javascript" charset="utf-8"></script>';
|
2008-12-17 21:22:16 +01:00
|
|
|
}
|
2008-10-18 02:28:18 +02:00
|
|
|
|
2006-11-22 03:28:42 +01:00
|
|
|
1
|