~kosova/+junk/tuxfamily-twiki

« back to all changes in this revision

Viewing changes to foswiki/lib/Foswiki/Plugins/SlideShowPlugin/SlideShow.pm

  • Committer: James Michael DuPont
  • Date: 2009-07-18 19:58:49 UTC
  • Revision ID: jamesmikedupont@gmail.com-20090718195849-vgbmaht2ys791uo2
added foswiki

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Plugin for Foswiki - The Free and Open Source Wiki, http://foswiki.org/
 
2
#
 
3
# Copyright (C) 2002-2007 Peter Thoeny, peter@thoeny.org
 
4
# Copyright (C) 2008 Foswiki Contributors
 
5
#
 
6
# This program is free software; you can redistribute it and/or
 
7
# modify it under the terms of the GNU General Public License
 
8
# as published by the Free Software Foundation; either version 2
 
9
# of the License, or (at your option) any later version. For
 
10
# more details read LICENSE in the root of this distribution.
 
11
#
 
12
# This program is distributed in the hope that it will be useful,
 
13
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
# GNU General Public License for more details, published at
 
16
# http://www.gnu.org/copyleft/gpl.html
 
17
#
 
18
# As per the GPL, removal of this notice is prohibited.
 
19
 
 
20
use strict;
 
21
use Foswiki::Func;
 
22
 
 
23
package Foswiki::Plugins::SlideShowPlugin::SlideShow;
 
24
 
 
25
use vars qw( $imgRoot $installWeb );
 
26
 
 
27
# =========================
 
28
sub init
 
29
{
 
30
    $installWeb = shift;
 
31
    $imgRoot = '%PUBURLPATH%/'.$installWeb.'/SlideShowPlugin';
 
32
}
 
33
 
 
34
# =========================
 
35
sub handler
 
36
{
 
37
    my( $text, $theTopic, $theWeb ) = @_;
 
38
 
 
39
    my $textPre = "";
 
40
    my $textPost = "";
 
41
    my $args = "";
 
42
    if( $text =~ /^(.*)%SLIDESHOWSTART%(.*)$/s ) {
 
43
        $textPre = $1;
 
44
        $text = $2;
 
45
    } elsif( $text =~ /^(.*)%SLIDESHOWSTART{(.*?)}%(.*)$/s ) {
 
46
        $textPre = $1;
 
47
        $args = $2;
 
48
        $text = $3;
 
49
    }
 
50
    if( $text =~ /^(.*)%SLIDESHOWEND%(.*)$/s ) {
 
51
        $text = $1;
 
52
        $textPost = $2;
 
53
    }
 
54
 
 
55
    # Make sure we don't end up back in the handler again
 
56
    # SMELL: there should be a better block
 
57
    $text =~ s/%SLIDESHOW/%<nop>SLIDESHOW/g;
 
58
 
 
59
    my $query = Foswiki::Func::getCgiQuery();
 
60
 
 
61
    # Build query string based on existingURL parameters
 
62
    my $qparams = '?slideshow=on;skin=print';
 
63
    foreach my $name ( $query->param ) {
 
64
        next if ( $name =~ /(text|keywords|web|topic|slideshow|skin|\#)/ );
 
65
        $qparams .= ';' . $name . '=' . urlEncode( $query->param($name) );
 
66
    }
 
67
 
 
68
    if( $query && $query->param( 'slideshow' ) ) {
 
69
        # in presentation mode
 
70
 
 
71
        $textPre .= "\n#StartPresentation\n";
 
72
        $textPre .= renderSlideNav( $theWeb, $theTopic, 1, 1, "e", $qparams );
 
73
 
 
74
        my $slideMax = 0;
 
75
 
 
76
        if( $text =~ /(.*?[\n\r])\-\-\-+(\++)\!* (.*)/s ) {
 
77
            $textPre .= $1;
 
78
            $text = $3;
 
79
            my $level = $2;
 
80
            $level =~ s/\+/\\\+/go;
 
81
            my @slides = split( /[\n\r]\-\-\-+$level\!* /, $text );
 
82
            $text = "";
 
83
 
 
84
            my $hideComments = Foswiki::Func::getPreferencesValue( 'SLIDESHOWPLUGIN_HIDECOMMENTS' ) || '';
 
85
 
 
86
            my $tmplText = readTmplText( $theWeb, $args );
 
87
            my $slideText = "";
 
88
            my $slideTitle = "";
 
89
            my $slideBody = "";
 
90
            my $slideComment = "";
 
91
            my $slideNum = 1;
 
92
            $slideMax = @slides;
 
93
            my @titles = ();
 
94
            foreach( @slides ) {
 
95
                next unless /^([^\n\r]*)(.*)$/s;
 
96
                $slideTitle = $1 || '';
 
97
                $slideBody  = $2 || '';
 
98
                $slideComment = '';
 
99
                if( $hideComments && $slideBody =~ s/(\-\-\-+\+$level+\!*\s*Comments.*)//is ) {
 
100
                    $slideComment = $1;
 
101
                }
 
102
                push( @titles, $slideTitle );
 
103
                $slideText = $tmplText;
 
104
                $slideText =~ s/%SLIDETITLE%/$slideTitle/go;
 
105
                $slideText =~ s/%SLIDETEXT%/$slideBody/go;
 
106
                $slideText =~ s/%SLIDENUM%/$slideNum/go;
 
107
                $slideText =~ s/%SLIDEMAX%/$slideMax/go;
 
108
                $slideText =~ s/%SLIDENAV%/renderSlideNav(
 
109
                    $theWeb, $theTopic, $slideNum, $slideMax, "f p n", $qparams )/geo;
 
110
                $slideText =~ s/%SLIDENAVALL%/renderSlideNav(
 
111
                    $theWeb, $theTopic, $slideNum, $slideMax, "f p n l", $qparams )/geo;
 
112
                $slideText =~ s/%SLIDENAVFIRST%/renderSlideNav(
 
113
                    $theWeb, $theTopic, $slideNum, $slideMax, "f", $qparams )/geo;
 
114
                $slideText =~ s/%SLIDENAVPREV%/renderSlideNav(
 
115
                    $theWeb, $theTopic, $slideNum, $slideMax, "p", $qparams )/geo;
 
116
                $slideText =~ s/%SLIDENAVNEXT%/renderSlideNav(
 
117
                    $theWeb, $theTopic, $slideNum, $slideMax, "n", $qparams )/geo;
 
118
                $slideText =~ s/%SLIDENAVLAST%/renderSlideNav(
 
119
                    $theWeb, $theTopic, $slideNum, $slideMax, "l", $qparams )/geo;
 
120
                $text .= "\n\n-----\n#GoSlide$slideNum\n$slideText";
 
121
                unless( $text =~ s/%SLIDECOMMENT%/\n$slideComment\n/go ) {
 
122
                    $text .= "\n$slideComment\n\n" if( $slideComment );
 
123
                }
 
124
                $text .= "%BR%\n\n" x 20;
 
125
                $slideNum++;
 
126
            }
 
127
            $text =~ s/%TOC(?:\{.*?\})*%/renderSlideToc( $theWeb, $theTopic, @titles )/geo;
 
128
            $text .= "\n#GoSlide$slideNum\n%BR%\n";
 
129
        }
 
130
 
 
131
        $text = "$textPre\n$text\n";
 
132
        $text .= renderSlideNav( $theWeb, $theTopic, $slideMax + 1, $slideMax, "f p e", $qparams );
 
133
        $text .= "\n";
 
134
        $text .= "%BR%\n\n" x 30;
 
135
        $text =~ s/%BR%/<br \/>/go;
 
136
        $text .= $textPost;
 
137
 
 
138
    } else {
 
139
        # in normal topic view mode
 
140
        if( $text =~ /[\n\r]\-\-\-+(\++)/s ) {
 
141
            my $level = $1;
 
142
            $level =~ s/\+/\\\+/go;
 
143
            # add slide number to heading
 
144
            my $slideNum = 1;
 
145
            $text =~ s/([\n\r]\-\-\-+$level\!*) ([^\n\r]+)/"$1 Slide " . $slideNum++ . ": $2"/ges;
 
146
        }
 
147
        $text = "$textPre \n#StartPresentation\n"
 
148
              . renderSlideNav( $theWeb, $theTopic, 1, 1, "s", $qparams )
 
149
              . "\n$text $textPost";
 
150
    }
 
151
 
 
152
    return $text;
 
153
}
 
154
 
 
155
# =========================
 
156
sub renderSlideNav
 
157
{
 
158
    my( $theWeb, $theTopic, $theNum, $theMax, $theButtons, $qstring ) = @_;
 
159
    my $prev = $theNum - 1 || 1;
 
160
    my $next = $theNum + 1;
 
161
    my $text = '<span style="white-space: nowrap">';
 
162
    my $viewUrl = Foswiki::Func::getViewUrl($theWeb, $theTopic);
 
163
    if( $theButtons =~ /f/ ) {
 
164
        # first slide button
 
165
        if( $theButtons =~ / f/ ) {
 
166
            $text .= "&nbsp;";
 
167
        }
 
168
        $text .= "<a href=\"$viewUrl$qstring#GoSlide1\">"
 
169
               . "<img src=\"$imgRoot/first.gif\" border=\"0\""
 
170
               . " alt=\"First slide\" /></a>";
 
171
    }
 
172
    if( $theButtons =~ /p/ ) {
 
173
        # previous slide button
 
174
        if( $theButtons =~ / p/ ) {
 
175
            $text .= "&nbsp;";
 
176
        }
 
177
        $text .= "<a href=\"$viewUrl$qstring#GoSlide$prev\">"
 
178
               . "<img src=\"$imgRoot/prev.gif\" border=\"0\""
 
179
               . " alt=\"Previous\" /></a>";
 
180
    }
 
181
    if( $theButtons =~ /n/ ) {
 
182
        # next slide button
 
183
        if( $theButtons =~ / n/ ) {
 
184
            $text .= "&nbsp;";
 
185
        }
 
186
        $text .= "<a href=\"$viewUrl$qstring#GoSlide$next\">"
 
187
               . "<img src=\"$imgRoot/next.gif\" border=\"0\""
 
188
               . " alt=\"Next\" /></a>";
 
189
    }
 
190
    if( $theButtons =~ /l/ ) {
 
191
        # last slide button
 
192
        if( $theButtons =~ / l/ ) {
 
193
            $text .= "&nbsp;";
 
194
        }
 
195
        $text .= "<a href=\"$viewUrl$qstring#GoSlide$theMax\">"
 
196
               . "<img src=\"$imgRoot/last.gif\" border=\"0\""
 
197
               . " alt=\"Last slide\" /></a>";
 
198
    }
 
199
    if( $theButtons =~ /e/ ) {
 
200
        # end slideshow button
 
201
        if( $theButtons =~ / e/ ) {
 
202
            $text .= "&nbsp;";
 
203
        }
 
204
        $text .= "<a href=\"$viewUrl\">"
 
205
               . "<img src=\"$imgRoot/endpres.gif\" border=\"0\""
 
206
               . " alt=\"End Presentation\" /></a>";
 
207
    }
 
208
    if( $theButtons =~ /s/ ) {
 
209
        # start slideshow button
 
210
        if( $theButtons =~ / s/ ) {
 
211
            $text .= "&nbsp;";
 
212
        }
 
213
        $text .= "<a href=\"$viewUrl$qstring#GoSlide1\">"
 
214
               . "<img src=\"$imgRoot/startpres.gif\" border=\"0\""
 
215
               . " alt=\"Start Presentation\" /></a>";
 
216
    }
 
217
    $text .= '</span>';
 
218
    return $text;
 
219
}
 
220
 
 
221
# =========================
 
222
sub renderSlideToc
 
223
{
 
224
    my( $theWeb, $theTopic, @theTitles ) = @_;
 
225
 
 
226
    my $slideNum = 1;
 
227
    my $text = '';
 
228
    my $viewUrl = Foswiki::Func::getViewUrl($theWeb, $theTopic);
 
229
    foreach( @theTitles ) {
 
230
        $text .= "\t\* ";
 
231
        $text .= "<a href=\"$viewUrl?slideshow=on&amp;skin=print#GoSlide$slideNum\">";
 
232
        $text .= " $_ </a>\n";
 
233
        $slideNum++;
 
234
    }
 
235
    return $text;
 
236
}
 
237
 
 
238
# =========================
 
239
sub readTmplText
 
240
{
 
241
    my( $theWeb, $theArgs ) = @_;
 
242
 
 
243
    my $tmplTopic =  Foswiki::Func::extractNameValuePair( $theArgs, "template" );
 
244
    unless( $tmplTopic ) {
 
245
        $theWeb = $installWeb;
 
246
        $tmplTopic =  Foswiki::Func::getPreferencesValue( "SLIDESHOWPLUGIN_TEMPLATE" )
 
247
                   || "SlideShowPlugin";
 
248
    }
 
249
    if( $tmplTopic =~ /^([^\.]+)\.(.*)$/o ) {
 
250
        $theWeb = $1;
 
251
        $tmplTopic = $2;
 
252
    }
 
253
    my( $meta, $text ) = Foswiki::Func::readTopic( $theWeb, $tmplTopic );
 
254
    # remove everything before %STARTINCLUDE% and after %STOPINCLUDE%
 
255
    $text =~ s/.*?%STARTINCLUDE%//os;
 
256
    $text =~ s/%STOPINCLUDE%.*//os;
 
257
 
 
258
    unless( $text ) {
 
259
        $text = "<font color=\"red\"> $installWeb.SlideShowPlugin Error: </font>"
 
260
              . "Slide template topic <nop>$theWeb.$tmplTopic not found or empty!\n\n"
 
261
              . "%SLIDETITLE%\n\n%SLIDETEXT%\n\n";
 
262
    } elsif( $text =~ /%SLIDETITLE%/ && $text =~ /%SLIDETEXT%/ ) {
 
263
        # assume that format is OK
 
264
    } else {
 
265
        $text = "<font color=\"red\"> $installWeb.SlideShowPlugin Error: </font>"
 
266
              . "Missing =%<nop>SLIDETITLE%= or =%<nop>SLIDETEXT%= in "
 
267
              . "slide template topic $theWeb.$tmplTopic.\n\n"
 
268
              . "%SLIDETITLE%\n\n%SLIDETEXT%\n\n";
 
269
    }
 
270
    $text =~ s/%WEB%/$theWeb/go;
 
271
    $text =~ s/%TOPIC%/$tmplTopic/go;
 
272
    $text =~ s/%ATTACHURL%/%PUBURL%\/$theWeb\/$tmplTopic/go;
 
273
    return $text;
 
274
}
 
275
 
 
276
# =========================
 
277
sub urlEncode
 
278
{
 
279
    my $text = shift;
 
280
    $text =~ s/([^0-9a-zA-Z-_.:~!*'()\/%])/'%'.sprintf('%02x',ord($1))/ge;
 
281
    $text =~ s/\%20/+/g;
 
282
    return $text;
 
283
}
 
284
 
 
285
1;