lib/TWiki/Plugins/SlideShowPlugin/SlideShow.pm
changeset 0 414e01d06fd5
child 1 e2915a7cbdfa
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/lib/TWiki/Plugins/SlideShowPlugin/SlideShow.pm	Sat Jan 26 15:50:53 2008 +0100
     1.3 @@ -0,0 +1,284 @@
     1.4 +# Plugin for TWiki Enterprise Collaboration Platform, http://TWiki.org/
     1.5 +#
     1.6 +# Copyright (C) 2002-2007 Peter Thoeny, peter@thoeny.org
     1.7 +#
     1.8 +# This program is free software; you can redistribute it and/or
     1.9 +# modify it under the terms of the GNU General Public License
    1.10 +# as published by the Free Software Foundation; either version 2
    1.11 +# of the License, or (at your option) any later version. For
    1.12 +# more details read LICENSE in the root of this distribution.
    1.13 +#
    1.14 +# This program is distributed in the hope that it will be useful,
    1.15 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.16 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.17 +# GNU General Public License for more details, published at
    1.18 +# http://www.gnu.org/copyleft/gpl.html
    1.19 +#
    1.20 +# As per the GPL, removal of this notice is prohibited.
    1.21 +
    1.22 +use strict;
    1.23 +use TWiki::Func;
    1.24 +
    1.25 +package TWiki::Plugins::SlideShowPlugin::SlideShow;
    1.26 +
    1.27 +use vars qw( $imgRoot $installWeb );
    1.28 +
    1.29 +# =========================
    1.30 +sub init
    1.31 +{
    1.32 +    $installWeb = shift;
    1.33 +    $imgRoot = '%PUBURLPATH%/'.$installWeb.'/SlideShowPlugin';
    1.34 +}
    1.35 +
    1.36 +# =========================
    1.37 +sub handler
    1.38 +{
    1.39 +    my( $text, $theTopic, $theWeb ) = @_;
    1.40 +
    1.41 +    my $textPre = "";
    1.42 +    my $textPost = "";
    1.43 +    my $args = "";
    1.44 +    if( $text =~ /^(.*)%SLIDESHOWSTART%(.*)$/s ) {
    1.45 +        $textPre = $1;
    1.46 +        $text = $2;
    1.47 +    } elsif( $text =~ /^(.*)%SLIDESHOWSTART{(.*?)}%(.*)$/s ) {
    1.48 +        $textPre = $1;
    1.49 +        $args = $2;
    1.50 +        $text = $3;
    1.51 +    }
    1.52 +    if( $text =~ /^(.*)%SLIDESHOWEND%(.*)$/s ) {
    1.53 +        $text = $1;
    1.54 +        $textPost = $2;
    1.55 +    }
    1.56 +
    1.57 +    # Make sure we don't end up back in the handler again
    1.58 +    # SMELL: there should be a better block
    1.59 +    $text =~ s/%SLIDESHOW/%<nop>SLIDESHOW/g;
    1.60 +
    1.61 +    my $query = TWiki::Func::getCgiQuery();
    1.62 +
    1.63 +    # Build query string based on existingURL parameters
    1.64 +    my $qparams = '?slideshow=on;skin=print';
    1.65 +    foreach my $name ( $query->param ) {
    1.66 +        next if ( $name =~ /(keywords|web|topic|slideshow|skin|\#)/ );
    1.67 +        $qparams .= ';' . $name . '=' . urlEncode( $query->param($name) );
    1.68 +    }
    1.69 +
    1.70 +    if( $query && $query->param( 'slideshow' ) ) {
    1.71 +        # in presentation mode
    1.72 +
    1.73 +        $textPre .= "\n#StartPresentation\n";
    1.74 +        $textPre .= renderSlideNav( $theWeb, $theTopic, 1, 1, "e", $qparams );
    1.75 +
    1.76 +        my $slideMax = 0;
    1.77 +
    1.78 +        if( $text =~ /(.*?[\n\r])\-\-\-+(\++)\!* (.*)/s ) {
    1.79 +            $textPre .= $1;
    1.80 +            $text = $3;
    1.81 +            my $level = $2;
    1.82 +            $level =~ s/\+/\\\+/go;
    1.83 +            my @slides = split( /[\n\r]\-\-\-+$level\!* /, $text );
    1.84 +            $text = "";
    1.85 +
    1.86 +            my $hideComments = TWiki::Func::getPreferencesValue( 'SLIDESHOWPLUGIN_HIDECOMMENTS' ) || '';
    1.87 +
    1.88 +            my $tmplText = readTmplText( $theWeb, $args );
    1.89 +            my $slideText = "";
    1.90 +            my $slideTitle = "";
    1.91 +            my $slideBody = "";
    1.92 +            my $slideComment = "";
    1.93 +            my $slideNum = 1;
    1.94 +            $slideMax = @slides;
    1.95 +            my @titles = ();
    1.96 +            foreach( @slides ) {
    1.97 +                next unless /^([^\n\r]*)(.*)$/s;
    1.98 +                $slideTitle = $1 || '';
    1.99 +                $slideBody  = $2 || '';
   1.100 +                $slideComment = '';
   1.101 +                if( $hideComments && $slideBody =~ s/(\-\-\-+\+$level+\!*\s*Comments.*)//is ) {
   1.102 +                    $slideComment = $1;
   1.103 +                }
   1.104 +                push( @titles, $slideTitle );
   1.105 +                $slideText = $tmplText;
   1.106 +                $slideText =~ s/%SLIDETITLE%/$slideTitle/go;
   1.107 +                $slideText =~ s/%SLIDETEXT%/$slideBody/go;
   1.108 +                $slideText =~ s/%SLIDENUM%/$slideNum/go;
   1.109 +                $slideText =~ s/%SLIDEMAX%/$slideMax/go;
   1.110 +                $slideText =~ s/%SLIDENAV%/renderSlideNav(
   1.111 +                    $theWeb, $theTopic, $slideNum, $slideMax, "f p n", $qparams )/geo;
   1.112 +                $slideText =~ s/%SLIDENAVALL%/renderSlideNav(
   1.113 +                    $theWeb, $theTopic, $slideNum, $slideMax, "f p n l", $qparams )/geo;
   1.114 +                $slideText =~ s/%SLIDENAVFIRST%/renderSlideNav(
   1.115 +                    $theWeb, $theTopic, $slideNum, $slideMax, "f", $qparams )/geo;
   1.116 +                $slideText =~ s/%SLIDENAVPREV%/renderSlideNav(
   1.117 +                    $theWeb, $theTopic, $slideNum, $slideMax, "p", $qparams )/geo;
   1.118 +                $slideText =~ s/%SLIDENAVNEXT%/renderSlideNav(
   1.119 +                    $theWeb, $theTopic, $slideNum, $slideMax, "n", $qparams )/geo;
   1.120 +                $slideText =~ s/%SLIDENAVLAST%/renderSlideNav(
   1.121 +                    $theWeb, $theTopic, $slideNum, $slideMax, "l", $qparams )/geo;
   1.122 +                $text .= "\n\n-----\n#GoSlide$slideNum\n$slideText";
   1.123 +                unless( $text =~ s/%SLIDECOMMENT%/\n$slideComment\n/go ) {
   1.124 +                    $text .= "\n$slideComment\n\n" if( $slideComment );
   1.125 +                }
   1.126 +                $text .= "%BR%\n\n" x 20;
   1.127 +                $slideNum++;
   1.128 +            }
   1.129 +            $text =~ s/%TOC(?:\{.*?\})*%/renderSlideToc( $theWeb, $theTopic, @titles )/geo;
   1.130 +            $text .= "\n#GoSlide$slideNum\n%BR%\n";
   1.131 +        }
   1.132 +
   1.133 +        $text = "$textPre\n$text\n";
   1.134 +        $text .= renderSlideNav( $theWeb, $theTopic, $slideMax + 1, $slideMax, "f p e", $qparams );
   1.135 +        $text .= "\n";
   1.136 +        $text .= "%BR%\n\n" x 30;
   1.137 +        $text =~ s/%BR%/<br \/>/go;
   1.138 +        $text .= $textPost;
   1.139 +
   1.140 +    } else {
   1.141 +        # in normal topic view mode
   1.142 +        if( $text =~ /[\n\r]\-\-\-+(\++)/s ) {
   1.143 +            my $level = $1;
   1.144 +            $level =~ s/\+/\\\+/go;
   1.145 +            # add slide number to heading
   1.146 +            my $slideNum = 1;
   1.147 +            $text =~ s/([\n\r]\-\-\-+$level\!*) ([^\n\r]+)/"$1 Slide " . $slideNum++ . ": $2"/ges;
   1.148 +        }
   1.149 +        $text = "$textPre \n#StartPresentation\n"
   1.150 +              . renderSlideNav( $theWeb, $theTopic, 1, 1, "s", $qparams )
   1.151 +              . "\n$text $textPost";
   1.152 +    }
   1.153 +
   1.154 +    return $text;
   1.155 +}
   1.156 +
   1.157 +# =========================
   1.158 +sub renderSlideNav
   1.159 +{
   1.160 +    my( $theWeb, $theTopic, $theNum, $theMax, $theButtons, $qstring ) = @_;
   1.161 +    my $prev = $theNum - 1 || 1;
   1.162 +    my $next = $theNum + 1;
   1.163 +    my $text = '<span style="white-space: nowrap">';
   1.164 +    my $viewUrl = TWiki::Func::getViewUrl($theWeb, $theTopic);
   1.165 +    if( $theButtons =~ /f/ ) {
   1.166 +        # first slide button
   1.167 +        if( $theButtons =~ / f/ ) {
   1.168 +            $text .= "&nbsp;";
   1.169 +        }
   1.170 +        $text .= "<a href=\"$viewUrl$qstring#GoSlide1\">"
   1.171 +               . "<img src=\"$imgRoot/first.gif\" border=\"0\""
   1.172 +               . " alt=\"First slide\" /></a>";
   1.173 +    }
   1.174 +    if( $theButtons =~ /p/ ) {
   1.175 +        # previous slide button
   1.176 +        if( $theButtons =~ / p/ ) {
   1.177 +            $text .= "&nbsp;";
   1.178 +        }
   1.179 +        $text .= "<a href=\"$viewUrl$qstring#GoSlide$prev\">"
   1.180 +               . "<img src=\"$imgRoot/prev.gif\" border=\"0\""
   1.181 +               . " alt=\"Previous\" /></a>";
   1.182 +    }
   1.183 +    if( $theButtons =~ /n/ ) {
   1.184 +        # next slide button
   1.185 +        if( $theButtons =~ / n/ ) {
   1.186 +            $text .= "&nbsp;";
   1.187 +        }
   1.188 +        $text .= "<a href=\"$viewUrl$qstring#GoSlide$next\">"
   1.189 +               . "<img src=\"$imgRoot/next.gif\" border=\"0\""
   1.190 +               . " alt=\"Next\" /></a>";
   1.191 +    }
   1.192 +    if( $theButtons =~ /l/ ) {
   1.193 +        # last slide button
   1.194 +        if( $theButtons =~ / l/ ) {
   1.195 +            $text .= "&nbsp;";
   1.196 +        }
   1.197 +        $text .= "<a href=\"$viewUrl$qstring#GoSlide$theMax\">"
   1.198 +               . "<img src=\"$imgRoot/last.gif\" border=\"0\""
   1.199 +               . " alt=\"Last slide\" /></a>";
   1.200 +    }
   1.201 +    if( $theButtons =~ /e/ ) {
   1.202 +        # end slideshow button
   1.203 +        if( $theButtons =~ / e/ ) {
   1.204 +            $text .= "&nbsp;";
   1.205 +        }
   1.206 +        $text .= "<a href=\"$viewUrl\">"
   1.207 +               . "<img src=\"$imgRoot/endpres.gif\" border=\"0\""
   1.208 +               . " alt=\"End Presentation\" /></a>";
   1.209 +    }
   1.210 +    if( $theButtons =~ /s/ ) {
   1.211 +        # start slideshow button
   1.212 +        if( $theButtons =~ / s/ ) {
   1.213 +            $text .= "&nbsp;";
   1.214 +        }
   1.215 +        $text .= "<a href=\"$viewUrl$qstring#GoSlide1\">"
   1.216 +               . "<img src=\"$imgRoot/startpres.gif\" border=\"0\""
   1.217 +               . " alt=\"Start Presentation\" /></a>";
   1.218 +    }
   1.219 +    $text .= '</span>';
   1.220 +    return $text;
   1.221 +}
   1.222 +
   1.223 +# =========================
   1.224 +sub renderSlideToc
   1.225 +{
   1.226 +    my( $theWeb, $theTopic, @theTitles ) = @_;
   1.227 +
   1.228 +    my $slideNum = 1;
   1.229 +    my $text = '';
   1.230 +    my $viewUrl = TWiki::Func::getViewUrl($theWeb, $theTopic);
   1.231 +    foreach( @theTitles ) {
   1.232 +        $text .= "\t\* ";
   1.233 +        $text .= "<a href=\"$viewUrl?slideshow=on&amp;skin=print#GoSlide$slideNum\">";
   1.234 +        $text .= " $_ </a>\n";
   1.235 +        $slideNum++;
   1.236 +    }
   1.237 +    return $text;
   1.238 +}
   1.239 +
   1.240 +# =========================
   1.241 +sub readTmplText
   1.242 +{
   1.243 +    my( $theWeb, $theArgs ) = @_;
   1.244 +
   1.245 +    my $tmplTopic =  TWiki::Func::extractNameValuePair( $theArgs, "template" );
   1.246 +    unless( $tmplTopic ) {
   1.247 +        $theWeb = $installWeb;
   1.248 +        $tmplTopic =  TWiki::Func::getPreferencesValue( "SLIDESHOWPLUGIN_TEMPLATE" )
   1.249 +                   || "SlideShowPlugin";
   1.250 +    }
   1.251 +    if( $tmplTopic =~ /^([^\.]+)\.(.*)$/o ) {
   1.252 +        $theWeb = $1;
   1.253 +        $tmplTopic = $2;
   1.254 +    }
   1.255 +    my( $meta, $text ) = TWiki::Func::readTopic( $theWeb, $tmplTopic );
   1.256 +    # remove everything before %STARTINCLUDE% and after %STOPINCLUDE%
   1.257 +    $text =~ s/.*?%STARTINCLUDE%//os;
   1.258 +    $text =~ s/%STOPINCLUDE%.*//os;
   1.259 +
   1.260 +    unless( $text ) {
   1.261 +        $text = "<font color=\"red\"> $installWeb.SlideShowPlugin Error: </font>"
   1.262 +              . "Slide template topic <nop>$theWeb.$tmplTopic not found or empty!\n\n"
   1.263 +              . "%SLIDETITLE%\n\n%SLIDETEXT%\n\n";
   1.264 +    } elsif( $text =~ /%SLIDETITLE%/ && $text =~ /%SLIDETEXT%/ ) {
   1.265 +        # assume that format is OK
   1.266 +    } else {
   1.267 +        $text = "<font color=\"red\"> $installWeb.SlideShowPlugin Error: </font>"
   1.268 +              . "Missing =%<nop>SLIDETITLE%= or =%<nop>SLIDETEXT%= in "
   1.269 +              . "slide template topic $theWeb.$tmplTopic.\n\n"
   1.270 +              . "%SLIDETITLE%\n\n%SLIDETEXT%\n\n";
   1.271 +    }
   1.272 +    $text =~ s/%WEB%/$theWeb/go;
   1.273 +    $text =~ s/%TOPIC%/$tmplTopic/go;
   1.274 +    $text =~ s/%ATTACHURL%/%PUBURL%\/$theWeb\/$tmplTopic/go;
   1.275 +    return $text;
   1.276 +}
   1.277 +
   1.278 +# =========================
   1.279 +sub urlEncode
   1.280 +{
   1.281 +    my $text = shift;
   1.282 +    $text =~ s/([^0-9a-zA-Z-_.:~!*'()\/%])/'%'.sprintf('%02x',ord($1))/ge;
   1.283 +    $text =~ s/\%20/+/g;
   1.284 +    return $text;
   1.285 +}
   1.286 +
   1.287 +1;