]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki/Plugin/toggle.pm
Add a postscan hook.
[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(class) {
43         var ret = new Array();
44         var pattern = new RegExp("(^|\\s)"+class+"(\\s|$)");
45         var els = document.getElementsByTagName('*');
46         for (i = 0, j = 0; i < els.length; i++) {
47                 if ( pattern.test(els[i].className) ) {
48                         ret[j] = els[i];
49                         j++;
50                 }
51         }
52         return ret;
53 }
54 //-->
55 </script>
56 EOF
57
58 sub import { #{{{
59         hook(type => "preprocess", id => "toggle",
60                 call => \&preprocess_toggle);
61         hook(type => "preprocess", id => "toggleable",
62                 call => \&preprocess_toggleable);
63         hook(type => "format", id => "toggle", call => \&format);
64 } # }}}
65
66 sub genid ($$) { #{{{
67         my $page=shift;
68         my $id=shift;
69
70         $id="$page.$id";
71
72         # make it a legal html id attribute
73         $id=~s/[^-a-zA-Z0-9.]/-/g;
74         if ($id !~ /^[a-zA-Z]/) {
75                 $id="id$id";
76         }
77         return $id;
78 } #}}}
79
80 sub preprocess_toggle (@) { #{{{
81         my %params=(id => "default", text => "more", @_);
82
83         my $id=genid($params{page}, $params{id});
84         return "<a class=\"toggle\" href=\"#$id\">$params{text}</a>";
85 } # }}}
86
87 sub preprocess_toggleable (@) { #{{{
88         my %params=(id => "default", text => "", open => "no", @_);
89
90         # Preprocess the text to expand any preprocessor directives
91         # embedded inside it.
92         $params{text}=IkiWiki::preprocess($params{page}, $params{destpage}, 
93                 IkiWiki::filter($params{page}, $params{destpage}, $params{text}));
94         
95         my $id=genid($params{page}, $params{id});
96         my $class=(lc($params{open}) ne "yes") ? "toggleable" : "toggleable-open";
97
98         # Should really be a postprocessor directive, oh well. Work around
99         # markdown's dislike of markdown inside a <div> with various funky
100         # whitespace.
101         my ($indent)=$params{text}=~/( +)$/;
102         $indent="" unless defined $indent;
103         return "<div class=\"$class\" id=\"$id\"></div>\n\n$params{text}\n$indent<div class=\"toggleableend\"></div>";
104 } # }}}
105
106 sub format (@) { #{{{
107         my %params=@_;
108
109         if ($params{content}=~s!(<div class="toggleable(?:-open)?" id="[^"]+">)</div>!$1!g) {
110                 $params{content}=~s/<div class="toggleableend">//g;
111                 if (! ($params{content}=~s!^<body>!<body>$javascript!m)) {
112                         # no </body> tag, probably in preview mode
113                         $params{content}=$javascript.$params{content};
114                 }
115         }
116         return $params{content};
117 } # }}}
118
119 1