Merge branch 'master' of ssh://git.ikiwiki.info/srv/git/ikiwiki.info
[ikiwiki] / IkiWiki / Plugin / toggle.pm
1 #!/usr/bin/perl
2 package IkiWiki::Plugin::toggle;
3
4 use warnings;
5 use strict;
6 use IkiWiki 3.00;
7
8 sub import {
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);
16 }
17
18 sub getsetup () {
19         return
20                 plugin => {
21                         safe => 1,
22                         rebuild => undef,
23                 },
24 }
25
26 sub genid ($$) {
27         my $page=shift;
28         my $id=shift;
29
30         $id="$page.$id";
31
32         # make it a legal html id attribute
33         $id=~s/[^-a-zA-Z0-9.]/-/g;
34         if ($id !~ /^[a-zA-Z]/) {
35                 $id="id$id";
36         }
37         return $id;
38 }
39
40 sub preprocess_toggle (@) {
41         my %params=(id => "default", text => "more", @_);
42
43         my $id=genid($params{page}, $params{id});
44         return "<a class=\"toggle\" href=\"#$id\">$params{text}</a>";
45 }
46
47 sub preprocess_toggleable (@) {
48         my %params=(id => "default", text => "", open => "no", @_);
49
50         # Preprocess the text to expand any preprocessor directives
51         # embedded inside it.
52         $params{text}=IkiWiki::preprocess($params{page}, $params{destpage}, 
53                 IkiWiki::filter($params{page}, $params{destpage}, $params{text}));
54         
55         my $id=genid($params{page}, $params{id});
56         my $class=(lc($params{open}) ne "yes") ? "toggleable" : "toggleable-open";
57
58         # Should really be a postprocessor directive, oh well. Work around
59         # markdown's dislike of markdown inside a <div> with various funky
60         # whitespace.
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>";
64 }
65
66 sub format (@) {
67         my %params=@_;
68
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!^(<body>)!$1.include_javascript($params{page})!em)) {
72                         # no </body> tag, probably in preview mode
73                         $params{content}=include_javascript($params{page}, 1).$params{content};
74                 }
75         }
76         return $params{content};
77 }
78
79 sub include_javascript ($;$) {
80         my $page=shift;
81         my $absolute=shift;
82         
83         return '<script src="'.urlto("ikiwiki.js", $page, $absolute).
84                 '" type="text/javascript" charset="utf-8"></script>'."\n".
85                 '<script src="'.urlto("toggle.js", $page, $absolute).
86                 '" type="text/javascript" charset="utf-8"></script>';
87 }
88
89 1