]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki/Plugin/toggle.pm
add --delete-bucket option
[ikiwiki.git] / IkiWiki / Plugin / toggle.pm
1 #!/usr/bin/perl
2 package IkiWiki::Plugin::toggle;
3
4 use warnings;
5 use strict;
6 use IkiWiki 2.00;
7
8 # Here's the javascript that makes this possible. A key feature is the use
9 # of css to hide toggleables, to avoid any flashing on page load. The css
10 # is only emitted after the javascript tests that it's going to be able to
11 # show the toggleables.
12 my $javascript=<<'EOF';
13 <script type="text/javascript">
14 <!--
15 if (document.getElementById && document.getElementsByTagName && document.createTextNode) {
16         document.write('<style type="text/css">div.toggleable { display: none; }</style>');
17         window.onload = inittoggle;
18 }
19
20 function inittoggle() {
21         var as = getElementsByClass('toggle');
22         for (var i = 0; i < as.length; i++) {
23                 var id = as[i].href.match(/#(\w.+)/)[1];
24                 document.getElementById(id).style.display="none";
25                 as[i].onclick = function() {
26                         toggle(this);
27                         return false;
28                 }
29         }
30 }
31
32 function toggle(s) {
33         var id = s.href.match(/#(\w.+)/)[1];
34         style = document.getElementById(id).style;
35         if (style.display == "none")
36                 style.display = "block";
37         else
38                 style.display = "none";
39 }
40
41 function getElementsByClass(class) {
42         var ret = new Array();
43         var pattern = new RegExp("(^|\\s)"+class+"(\\s|$)");
44         var els = document.getElementsByTagName('*');
45         for (i = 0, j = 0; i < els.length; i++) {
46                 if ( pattern.test(els[i].className) ) {
47                         ret[j] = els[i];
48                         j++;
49                 }
50         }
51         return ret;
52 }
53 //-->
54 </script>
55 EOF
56
57 sub import { #{{{
58         hook(type => "preprocess", id => "toggle",
59                 call => \&preprocess_toggle);
60         hook(type => "preprocess", id => "toggleable",
61                 call => \&preprocess_toggleable);
62         hook(type => "format", id => "toggle", call => \&format);
63 } # }}}
64
65 sub genid ($$) { #{{{
66         my $page=shift;
67         my $id=shift;
68
69         $id="$page.$id";
70
71         # make it a legal html id attribute
72         $id=~s/[^-a-zA-Z0-9.]/-/g;
73         if ($id !~ /^[a-zA-Z]/) {
74                 $id="id$id";
75         }
76         return $id;
77 } #}}}
78
79 sub preprocess_toggle (@) { #{{{
80         my %params=(id => "default", text => "more", @_);
81
82         my $id=genid($params{page}, $params{id});
83         if (! $params{preview}) {
84                 return "<a class=\"toggle\" href=\"#$id\">$params{text}</a>";
85         }
86         else {
87                 return "$params{text} ".
88                         gettext("(not toggleable in preview mode)");
89         }
90 } # }}}
91
92 sub preprocess_toggleable (@) { #{{{
93         my %params=(id => "default", text => "", @_);
94
95         # Preprocess the text to expand any preprocessor directives
96         # embedded inside it.
97         $params{text}=IkiWiki::preprocess($params{page}, $params{destpage}, 
98                 IkiWiki::filter($params{page}, $params{destpage}, $params{text}));
99         
100         my $id=genid($params{page}, $params{id});
101
102         # Should really be a postprocessor directive, oh well. Work around
103         # markdown's dislike of markdown inside a <div> with various funky
104         # whitespace.
105         my ($indent)=$params{text}=~/( +)$/;
106         $indent="" unless defined $indent;
107         return "<div class=\"toggleable\" id=\"$id\"></div>\n\n$params{text}\n$indent<div class=\"toggleableend\"></div>";
108 } # }}}
109
110 sub format (@) { #{{{
111         my %params=@_;
112
113         if ($params{content}=~s!(<div class="toggleable" id="[^"]+">)</div>!$1!g) {
114                 $params{content}=~s/<div class="toggleableend">//g;
115                 $params{content}=~s!^<\/body>!$javascript</body>!m;
116         }
117         return $params{content};
118 } # }}}
119
120 1