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,
|
2010-02-12 12:35:52 +01:00
|
|
|
section => "widget",
|
2008-08-04 01:35:35 +02:00
|
|
|
},
|
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.
|
remove unnecessary and troublesome filter calls
This better defines what the filter hook is passed, to only be the raw,
complete text of a page. Not some snippet, or data read in from an
unrelated template.
Several plugins that filtered text that originates from an (already
filtered) page were modified not to do that. Note that this was not
done very consistently before; other plugins that receive text from a
page called preprocess on it w/o first calling filter.
The template plugin gets text from elsewhere, and was also changed not to
filter it. That leads to one known regression -- the embed plugin cannot
be used to embed stuff in templates now. But that plugin is deprecated
anyway.
Later we may want to increase the coverage of what is filtered. Perhaps
a good goal would be to allow writing a filter plugin that filters
out unwanted words, from any input. We're not there yet; not only
does the template plugin load unfiltered text from its templates now,
but so can the table plugin, and other plugins that use templates (like
inline!). I think we can cross that bridge when we come to it. If I wanted
such a censoring plugin, I'd probably make it use a sanitize hook instead,
for the better coverage.
For now I am concentrating on the needs of the two non-deprecated users
of filter. This should fix bugs/po_vs_templates, and it probably fixes
an obscure bug around txt's use of filter for robots.txt.
2010-07-04 21:00:51 +02:00
|
|
|
$params{text}=IkiWiki::preprocess($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;
|
2017-11-06 20:15:22 +01:00
|
|
|
if (! ($params{content}=~s!^(\s*</body[^>]*>)!include_javascript($params{page}).$1!em)) {
|
2010-08-12 20:29:22 +02:00
|
|
|
# no <body> tag, probably in preview mode
|
2017-11-06 20:15:22 +01:00
|
|
|
$params{content}=$params{content}.include_javascript(undef);
|
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
|
|
|
|
2010-11-23 01:06:07 +01:00
|
|
|
sub include_javascript ($) {
|
|
|
|
my $from=shift;
|
2008-10-18 02:28:18 +02:00
|
|
|
|
2010-11-23 01:06:07 +01:00
|
|
|
return '<script src="'.urlto("ikiwiki/ikiwiki.js", $from).
|
2008-10-18 02:28:18 +02:00
|
|
|
'" type="text/javascript" charset="utf-8"></script>'."\n".
|
2010-11-23 01:06:07 +01:00
|
|
|
'<script src="'.urlto("ikiwiki/toggle.js", $from).
|
2008-10-18 02:28:18 +02:00
|
|
|
'" 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
|