2 package IkiWiki::Plugin::toggle;
9 add_underlay("javascript");
10 hook(type => "getsetup", id => "toggle", call => \&getsetup);
11 hook(type => "preprocess", id => "toggle",
12 call => \&preprocess_toggle);
13 hook(type => "preprocess", id => "toggleable",
14 call => \&preprocess_toggleable);
15 hook(type => "format", id => "toggle", call => \&format);
33 # make it a legal html id attribute
34 $id=~s/[^-a-zA-Z0-9.]/-/g;
35 if ($id !~ /^[a-zA-Z]/) {
41 sub preprocess_toggle (@) {
42 my %params=(id => "default", text => "more", @_);
44 my $id=genid($params{page}, $params{id});
45 return "<a class=\"toggle\" href=\"#$id\">$params{text}</a>";
48 sub preprocess_toggleable (@) {
49 my %params=(id => "default", text => "", open => "no", @_);
51 # Preprocess the text to expand any preprocessor directives
53 $params{text}=IkiWiki::preprocess($params{page}, $params{destpage}, $params{text});
55 my $id=genid($params{page}, $params{id});
56 my $class=(lc($params{open}) ne "yes") ? "toggleable" : "toggleable-open";
58 # Should really be a postprocessor directive, oh well. Work around
59 # markdown's dislike of markdown inside a <div> with various funky
61 my ($indent)=$params{text}=~/( +)$/;
62 $indent="" unless defined $indent;
63 return "<div class=\"$class\" id=\"$id\"></div>\n\n$params{text}\n$indent<div class=\"toggleableend\"></div>";
69 if ($params{content}=~s!(<div class="toggleable(?:-open)?" id="[^"]+">\s*)</div>!$1!g) {
70 $params{content}=~s/<div class="toggleableend">//g;
71 if (! ($params{content}=~s!^(\s*</body[^>]*>)!include_javascript($params{page}).$1!em)) {
72 # no <body> tag, probably in preview mode
73 $params{content}=$params{content}.include_javascript(undef);
76 return $params{content};
79 sub include_javascript ($) {
82 return '<script src="'.urlto("ikiwiki/ikiwiki.js", $from).
83 '" type="text/javascript" charset="utf-8"></script>'."\n".
84 '<script src="'.urlto("ikiwiki/toggle.js", $from).
85 '" type="text/javascript" charset="utf-8"></script>';