further sanitize nickname characters
[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                         section => "widget",
24                 },
25 }
26
27 sub genid ($$) {
28         my $page=shift;
29         my $id=shift;
30
31         $id="$page.$id";
32
33         # make it a legal html id attribute
34         $id=~s/[^-a-zA-Z0-9.]/-/g;
35         if ($id !~ /^[a-zA-Z]/) {
36                 $id="id$id";
37         }
38         return $id;
39 }
40
41 sub preprocess_toggle (@) {
42         my %params=(id => "default", text => "more", @_);
43
44         my $id=genid($params{page}, $params{id});
45         return "<a class=\"toggle\" href=\"#$id\">$params{text}</a>";
46 }
47
48 sub preprocess_toggleable (@) {
49         my %params=(id => "default", text => "", open => "no", @_);
50
51         # Preprocess the text to expand any preprocessor directives
52         # embedded inside it.
53         $params{text}=IkiWiki::preprocess($params{page}, $params{destpage}, 
54                 IkiWiki::filter($params{page}, $params{destpage}, $params{text}));
55         
56         my $id=genid($params{page}, $params{id});
57         my $class=(lc($params{open}) ne "yes") ? "toggleable" : "toggleable-open";
58
59         # Should really be a postprocessor directive, oh well. Work around
60         # markdown's dislike of markdown inside a <div> with various funky
61         # whitespace.
62         my ($indent)=$params{text}=~/( +)$/;
63         $indent="" unless defined $indent;
64         return "<div class=\"$class\" id=\"$id\"></div>\n\n$params{text}\n$indent<div class=\"toggleableend\"></div>";
65 }
66
67 sub format (@) {
68         my %params=@_;
69
70         if ($params{content}=~s!(<div class="toggleable(?:-open)?" id="[^"]+">\s*)</div>!$1!g) {
71                 $params{content}=~s/<div class="toggleableend">//g;
72                 if (! ($params{content}=~s!^(<body[^>]*>)!$1.include_javascript($params{page})!em)) {
73                         # no </body> tag, probably in preview mode
74                         $params{content}=include_javascript($params{page}, 1).$params{content};
75                 }
76         }
77         return $params{content};
78 }
79
80 sub include_javascript ($;$) {
81         my $page=shift;
82         my $absolute=shift;
83         
84         return '<script src="'.urlto("ikiwiki/ikiwiki.js", $page, $absolute).
85                 '" type="text/javascript" charset="utf-8"></script>'."\n".
86                 '<script src="'.urlto("ikiwiki/toggle.js", $page, $absolute).
87                 '" type="text/javascript" charset="utf-8"></script>';
88 }
89
90 1