]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki/Plugin/toggle.pm
external: Fix support for hooks called in an array context.
[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 our $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                 if (document.getElementById(id).className == "toggleable")
25                         document.getElementById(id).style.display="none";
26                 as[i].onclick = function() {
27                         toggle(this);
28                         return false;
29                 }
30         }
31 }
32
33 function toggle(s) {
34         var id = s.href.match(/#(\w.+)/)[1];
35         style = document.getElementById(id).style;
36         if (style.display == "none")
37                 style.display = "block";
38         else
39                 style.display = "none";
40 }
41
42 function getElementsByClass(cls, node, tag) {
43         if (document.getElementsByClass)
44                 return document.getElementsByClass(cls, node, tag);
45         if (! node) node = document;
46         if (! tag) tag = '*';
47         var ret = new Array();
48         var pattern = new RegExp("(^|\\s)"+cls+"(\\s|$)");
49         var els = node.getElementsByTagName(tag);
50         for (i = 0; i < els.length; i++) {
51                 if ( pattern.test(els[i].className) ) {
52                         ret.push(els[i]);
53                 }
54         }
55         return ret;
56 }
57
58 //-->
59 </script>
60 EOF
61
62 sub import { #{{{
63         hook(type => "getsetup", id => "toggle", call => \&getsetup);
64         hook(type => "preprocess", id => "toggle",
65                 call => \&preprocess_toggle);
66         hook(type => "preprocess", id => "toggleable",
67                 call => \&preprocess_toggleable);
68         hook(type => "format", id => "toggle", call => \&format);
69 } # }}}
70
71 sub getsetup () { #{{{
72         return
73                 plugin => {
74                         safe => 1,
75                         rebuild => undef,
76                 },
77 } #}}}
78
79 sub genid ($$) { #{{{
80         my $page=shift;
81         my $id=shift;
82
83         $id="$page.$id";
84
85         # make it a legal html id attribute
86         $id=~s/[^-a-zA-Z0-9.]/-/g;
87         if ($id !~ /^[a-zA-Z]/) {
88                 $id="id$id";
89         }
90         return $id;
91 } #}}}
92
93 sub preprocess_toggle (@) { #{{{
94         my %params=(id => "default", text => "more", @_);
95
96         my $id=genid($params{page}, $params{id});
97         return "<a class=\"toggle\" href=\"#$id\">$params{text}</a>";
98 } # }}}
99
100 sub preprocess_toggleable (@) { #{{{
101         my %params=(id => "default", text => "", open => "no", @_);
102
103         # Preprocess the text to expand any preprocessor directives
104         # embedded inside it.
105         $params{text}=IkiWiki::preprocess($params{page}, $params{destpage}, 
106                 IkiWiki::filter($params{page}, $params{destpage}, $params{text}));
107         
108         my $id=genid($params{page}, $params{id});
109         my $class=(lc($params{open}) ne "yes") ? "toggleable" : "toggleable-open";
110
111         # Should really be a postprocessor directive, oh well. Work around
112         # markdown's dislike of markdown inside a <div> with various funky
113         # whitespace.
114         my ($indent)=$params{text}=~/( +)$/;
115         $indent="" unless defined $indent;
116         return "<div class=\"$class\" id=\"$id\"></div>\n\n$params{text}\n$indent<div class=\"toggleableend\"></div>";
117 } # }}}
118
119 sub format (@) { #{{{
120         my %params=@_;
121
122         if ($params{content}=~s!(<div class="toggleable(?:-open)?" id="[^"]+">\s*)</div>!$1!g) {
123                 $params{content}=~s/<div class="toggleableend">//g;
124                 if (! ($params{content}=~s!^<body>!<body>$javascript!m)) {
125                         # no </body> tag, probably in preview mode
126                         $params{content}=$javascript.$params{content};
127                 }
128         }
129         return $params{content};
130 } # }}}
131
132 1