2 package IkiWiki::Plugin::toggle;
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">
15 if (document.getElementById && document.getElementsByTagName && document.createTextNode) {
16 document.write('<style type="text/css">div.toggleable { display: none; }</style>');
17 window.onload = inittoggle;
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() {
34 var id = s.href.match(/#(\w.+)/)[1];
35 style = document.getElementById(id).style;
36 if (style.display == "none")
37 style.display = "block";
39 style.display = "none";
42 function getElementsByClass(cls, node, tag) {
43 if (document.getElementsByClass)
44 return document.getElementsByClass(cls, node, tag);
45 if (! node) node = document;
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) ) {
63 hook(type => "preprocess", id => "toggle",
64 call => \&preprocess_toggle);
65 hook(type => "preprocess", id => "toggleable",
66 call => \&preprocess_toggleable);
67 hook(type => "format", id => "toggle", call => \&format);
76 # make it a legal html id attribute
77 $id=~s/[^-a-zA-Z0-9.]/-/g;
78 if ($id !~ /^[a-zA-Z]/) {
84 sub preprocess_toggle (@) { #{{{
85 my %params=(id => "default", text => "more", @_);
87 my $id=genid($params{page}, $params{id});
88 return "<a class=\"toggle\" href=\"#$id\">$params{text}</a>";
91 sub preprocess_toggleable (@) { #{{{
92 my %params=(id => "default", text => "", open => "no", @_);
94 # Preprocess the text to expand any preprocessor directives
96 $params{text}=IkiWiki::preprocess($params{page}, $params{destpage},
97 IkiWiki::filter($params{page}, $params{destpage}, $params{text}));
99 my $id=genid($params{page}, $params{id});
100 my $class=(lc($params{open}) ne "yes") ? "toggleable" : "toggleable-open";
102 # Should really be a postprocessor directive, oh well. Work around
103 # markdown's dislike of markdown inside a <div> with various funky
105 my ($indent)=$params{text}=~/( +)$/;
106 $indent="" unless defined $indent;
107 return "<div class=\"$class\" id=\"$id\"></div>\n\n$params{text}\n$indent<div class=\"toggleableend\"></div>";
110 sub format (@) { #{{{
113 if ($params{content}=~s!(<div class="toggleable(?:-open)?" id="[^"]+">)</div>!$1!g) {
114 $params{content}=~s/<div class="toggleableend">//g;
115 if (! ($params{content}=~s!^<body>!<body>$javascript!m)) {
116 # no </body> tag, probably in preview mode
117 $params{content}=$javascript.$params{content};
120 return $params{content};