lib/TWiki/Plugins/TwistyPlugin.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/TwistyPlugin.pm	Sat Jan 26 15:50:53 2008 +0100
     1.3 @@ -0,0 +1,452 @@
     1.4 +# Plugin for TWiki Collaboration Platform, http://TWiki.org/
     1.5 +#
     1.6 +# Copyright (C) 2005 Rafael Alvarez, soronthar@sourceforge.net
     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.
    1.12 +#
    1.13 +# This program is distributed in the hope that it will be useful,
    1.14 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.15 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.16 +# GNU General Public License for more details, published at
    1.17 +# http://www.gnu.org/copyleft/gpl.html
    1.18 +#
    1.19 +
    1.20 +=pod
    1.21 +
    1.22 +---+ package TwistyPlugin
    1.23 +
    1.24 +Convenience plugin for TWiki:Plugins.TwistyContrib.
    1.25 +It has two major features:
    1.26 +   * When active, the Twisty javascript library is included in every topic.
    1.27 +   * Provides a convenience syntax to define twisty areas.
    1.28 +
    1.29 +=cut
    1.30 +
    1.31 +package TWiki::Plugins::TwistyPlugin;
    1.32 +
    1.33 +use TWiki::Func;
    1.34 +use CGI::Cookie;
    1.35 +use strict;
    1.36 +
    1.37 +use vars
    1.38 +  qw( $VERSION $RELEASE $pluginName $debug @modes $doneHeader $twistyCount
    1.39 +  $prefMode $prefShowLink $prefHideLink $prefRemember
    1.40 +  $defaultMode $defaultShowLink $defaultHideLink $defaultRemember );
    1.41 +
    1.42 +# This should always be $Rev: 15653 (19 Nov 2007) $ so that TWiki can determine the checked-in
    1.43 +# status of the plugin. It is used by the build automation tools, so
    1.44 +# you should leave it alone.
    1.45 +$VERSION = '$Rev: 15653 (19 Nov 2007) $';
    1.46 +
    1.47 +# This is a free-form string you can use to "name" your own plugin version.
    1.48 +# It is *not* used by the build automation tools, but is reported as part
    1.49 +# of the version number in PLUGINDESCRIPTIONS.
    1.50 +$RELEASE = '1.4.9';
    1.51 +
    1.52 +$pluginName = 'TwistyPlugin';
    1.53 +
    1.54 +my $TWISTYPLUGIN_COOKIE_PREFIX  = "TwistyContrib_";
    1.55 +my $TWISTYPLUGIN_CONTENT_HIDDEN = 0;
    1.56 +my $TWISTYPLUGIN_CONTENT_SHOWN  = 1;
    1.57 +
    1.58 +#there is no need to document this.
    1.59 +sub initPlugin {
    1.60 +    my ( $topic, $web, $user, $installWeb ) = @_;
    1.61 +
    1.62 +    # check for Plugins.pm versions
    1.63 +    if ( $TWiki::Plugins::VERSION < 1.1 ) {
    1.64 +        TWiki::Func::writeWarning(
    1.65 +            "Version mismatch between $pluginName and Plugins.pm");
    1.66 +        return 0;
    1.67 +    }
    1.68 +
    1.69 +    _setDefaults();
    1.70 +
    1.71 +    $debug = TWiki::Func::getPluginPreferencesFlag("DEBUG");
    1.72 +
    1.73 +    $doneHeader  = 0;
    1.74 +    $twistyCount = 0;
    1.75 +
    1.76 +    $prefMode = TWiki::Func::getPreferencesValue('TWISTYMODE')
    1.77 +      || TWiki::Func::getPluginPreferencesValue('TWISTYMODE')
    1.78 +      || $defaultMode;
    1.79 +    $prefShowLink = TWiki::Func::getPreferencesValue('TWISTYSHOWLINK')
    1.80 +      || TWiki::Func::getPluginPreferencesValue('TWISTYSHOWLINK')
    1.81 +      || $defaultShowLink;
    1.82 +    $prefHideLink = TWiki::Func::getPreferencesValue('TWISTYHIDELINK')
    1.83 +      || TWiki::Func::getPluginPreferencesValue('TWISTYHIDELINK')
    1.84 +      || $defaultHideLink;
    1.85 +    $prefRemember = TWiki::Func::getPreferencesValue('TWISTYREMEMBER')
    1.86 +      || TWiki::Func::getPluginPreferencesValue('TWISTYREMEMBER')
    1.87 +      || $defaultRemember;
    1.88 +
    1.89 +    TWiki::Func::registerTagHandler( 'TWISTYSHOW',      \&_TWISTYSHOW );
    1.90 +    TWiki::Func::registerTagHandler( 'TWISTYHIDE',      \&_TWISTYHIDE );
    1.91 +    TWiki::Func::registerTagHandler( 'TWISTYBUTTON',    \&_TWISTYBUTTON );
    1.92 +    TWiki::Func::registerTagHandler( 'TWISTY',          \&_TWISTY );
    1.93 +    TWiki::Func::registerTagHandler( 'ENDTWISTY',       \&_ENDTWISTYTOGGLE );
    1.94 +    TWiki::Func::registerTagHandler( 'TWISTYTOGGLE',    \&_TWISTYTOGGLE );
    1.95 +    TWiki::Func::registerTagHandler( 'ENDTWISTYTOGGLE', \&_ENDTWISTYTOGGLE );
    1.96 +
    1.97 +    return 1;
    1.98 +}
    1.99 +
   1.100 +sub _setDefaults {
   1.101 +    $defaultMode     = 'span';
   1.102 +    $defaultShowLink = '';
   1.103 +    $defaultHideLink = '';
   1.104 +    $defaultRemember =
   1.105 +      '';    # do not default to 'off' or all cookies will be cleared!
   1.106 +}
   1.107 +
   1.108 +sub _addHeader {
   1.109 +    return if $doneHeader;
   1.110 +    $doneHeader = 1;
   1.111 +
   1.112 +    my $header .= <<'EOF';
   1.113 +<style type="text/css" media="all">
   1.114 +@import url("%PUBURL%/%TWIKIWEB%/TwistyContrib/twist.css");
   1.115 +</style>
   1.116 +<script type='text/javascript' src='%PUBURL%/%TWIKIWEB%/BehaviourContrib/behaviour.compressed.js'></script>
   1.117 +<script type="text/javascript" src="%PUBURL%/%TWIKIWEB%/TWikiJavascripts/twikilib.js"></script>
   1.118 +<script type="text/javascript" src="%PUBURL%/%TWIKIWEB%/TWikiJavascripts/twikiPref.js"></script>
   1.119 +<script type="text/javascript" src="%PUBURL%/%TWIKIWEB%/TWikiJavascripts/twikiCSS.js"></script>
   1.120 +<script type="text/javascript" src="%PUBURL%/%TWIKIWEB%/TwistyContrib/twist.compressed.js"></script>
   1.121 +<script type="text/javascript">
   1.122 +// <![CDATA[
   1.123 +var styleText = '<style type="text/css" media="all">.twikiMakeVisible{display:inline;}.twikiMakeVisibleInline{display:inline;}.twikiMakeVisibleBlock{display:block;}.twikiMakeHidden{display:none;}</style>';
   1.124 +document.write(styleText);
   1.125 +// ]]>
   1.126 +</script>
   1.127 +EOF
   1.128 +
   1.129 +    TWiki::Func::addToHEAD( 'TWISTYPLUGIN_TWISTY', $header );
   1.130 +}
   1.131 +
   1.132 +sub _TWISTYSHOW {
   1.133 +    my ( $session, $params, $theTopic, $theWeb ) = @_;
   1.134 +    my $mode = $params->{'mode'} || $prefMode;
   1.135 +
   1.136 +    my $btn = _twistyBtn( 'show', @_ );
   1.137 +    return _decodeFormatTokens( _wrapInButtonHtml( $btn, $mode ) );
   1.138 +}
   1.139 +
   1.140 +sub _TWISTYHIDE {
   1.141 +    my ( $session, $params, $theTopic, $theWeb ) = @_;
   1.142 +    my $mode = $params->{'mode'} || $prefMode;
   1.143 +
   1.144 +    my $btn = _twistyBtn( 'hide', @_ );
   1.145 +    return _decodeFormatTokens( _wrapInButtonHtml( $btn, $mode ) );
   1.146 +}
   1.147 +
   1.148 +sub _TWISTYBUTTON {
   1.149 +    my ( $session, $params, $theTopic, $theWeb ) = @_;
   1.150 +    my $mode = $params->{'mode'} || $prefMode;
   1.151 +
   1.152 +    my $btnShow = _twistyBtn( 'show', @_ );
   1.153 +    my $btnHide = _twistyBtn( 'hide', @_ );
   1.154 +    my $prefix = $params->{'prefix'} || '';
   1.155 +    my $suffix = $params->{'suffix'} || '';
   1.156 +    my $btn = $prefix . ' ' . $btnShow . $btnHide . ' ' . $suffix;
   1.157 +    return _decodeFormatTokens( _wrapInButtonHtml( $btn, $mode ) );
   1.158 +}
   1.159 +
   1.160 +sub _TWISTY {
   1.161 +    my ( $session, $params, $theTopic, $theWeb ) = @_;
   1.162 +
   1.163 +    _addHeader();
   1.164 +    $twistyCount++;
   1.165 +    my $id = $params->{'id'};
   1.166 +    if ( !defined $id || $id eq '' ) {
   1.167 +        $params->{'id'} = 'twistyId' . $theWeb . $theTopic . $twistyCount;
   1.168 +    }
   1.169 +    return _TWISTYBUTTON(@_) . ' ' . _TWISTYTOGGLE(@_);
   1.170 +}
   1.171 +
   1.172 +sub _TWISTYTOGGLE {
   1.173 +    my ( $session, $params, $theTopic, $theWeb ) = @_;
   1.174 +    my $id = $params->{'id'};
   1.175 +    if ( !defined $id || $id eq '' ) {
   1.176 +        return '';
   1.177 +    }
   1.178 +    my $idTag = $id . 'toggle';
   1.179 +    my $mode  = 'div';            #$params->{'mode'} || $prefMode;
   1.180 +    unshift @modes, $mode;
   1.181 +
   1.182 +    my $isTrigger = 0;
   1.183 +    my $cookieState = _readCookie( $session, $idTag );
   1.184 +    my @propList =
   1.185 +      _createHtmlProperties( undef, $idTag, $mode, $params, $isTrigger,
   1.186 +        $cookieState );
   1.187 +    my $props = @propList ? " " . join( " ", @propList ) : '';
   1.188 +    my $modeTag = '<' . $mode . $props . '>';
   1.189 +    return _decodeFormatTokens( _wrapInContentHtmlOpen() . $modeTag );
   1.190 +}
   1.191 +
   1.192 +sub _ENDTWISTYTOGGLE {
   1.193 +    my ( $session, $params, $theTopic, $theWeb ) = @_;
   1.194 +    my $mode = shift @modes;
   1.195 +    my $modeTag = ($mode) ? '</' . $mode . '>' : '';
   1.196 +    return $modeTag . _wrapInContentHtmlClose();
   1.197 +}
   1.198 +
   1.199 +sub _twistyBtn {
   1.200 +    my ( $twistyControlState, $session, $params, $theTopic, $theWeb ) = @_;
   1.201 +
   1.202 +    _addHeader();
   1.203 +
   1.204 +    # not used yet:
   1.205 +    #my $triangle_right = '&#9658;';
   1.206 +    #my $triangle_down = '&#9660;';
   1.207 +
   1.208 +    my $id = $params->{'id'};
   1.209 +    if ( !defined $id || $id eq '' ) {
   1.210 +        return '';
   1.211 +    }
   1.212 +    my $idTag = $id . $twistyControlState if ($twistyControlState) || '';
   1.213 +
   1.214 +    my $defaultLink =
   1.215 +      ( $twistyControlState eq 'show' ) ? $prefShowLink : $prefHideLink;
   1.216 +
   1.217 +    # link="" takes precedence over showlink="" and hidelink=""
   1.218 +    my $link = $params->{'link'};
   1.219 +
   1.220 +    if ( !defined $link ) {
   1.221 +
   1.222 +        # if 'link' is not set, try 'showlink' / 'hidelink'
   1.223 +        $link = $params->{ $twistyControlState . 'link' };
   1.224 +    }
   1.225 +    if ( !defined $link ) {
   1.226 +        $link = $defaultLink || '';
   1.227 +    }
   1.228 +
   1.229 +    my $img = $params->{ $twistyControlState . 'img' }
   1.230 +      || $params->{'img'}
   1.231 +      || '';
   1.232 +    my $imgright = $params->{ $twistyControlState . 'imgright' }
   1.233 +      || $params->{'imgright'}
   1.234 +      || '';
   1.235 +    my $imgleft = $params->{ $twistyControlState . 'imgleft' }
   1.236 +      || $params->{'imgleft'}
   1.237 +      || '';
   1.238 +    $img      =~ s/['\"]//go;
   1.239 +    $imgright =~ s/['\"]//go;
   1.240 +    $imgleft  =~ s/['\"]//go;
   1.241 +    my $imgTag =
   1.242 +      ( $img ne '' ) ? '<img src="' . $img . '" border="0" alt="" />' : '';
   1.243 +    my $imgRightTag =
   1.244 +      ( $imgright ne '' )
   1.245 +      ? '<img src="' . $imgright . '" border="0" alt="" />'
   1.246 +      : '';
   1.247 +    my $imgLeftTag =
   1.248 +      ( $imgleft ne '' )
   1.249 +      ? '<img src="' . $imgleft . '" border="0" alt="" />'
   1.250 +      : '';
   1.251 +    my $imgLinkTag =
   1.252 +        '<a href="#">'
   1.253 +      . $imgLeftTag
   1.254 +      . '<span class="twikiLinkLabel twikiUnvisited">'
   1.255 +      . $link
   1.256 +      . '</span>'
   1.257 +      . $imgTag
   1.258 +      . $imgRightTag . '</a>' . ' ';
   1.259 +
   1.260 +    my $isTrigger = 1;
   1.261 +    my $props     = '';
   1.262 +
   1.263 +    if ( $idTag && $params ) {
   1.264 +        my $cookieState = _readCookie( $session, $idTag );
   1.265 +        my @propList =
   1.266 +          _createHtmlProperties( $twistyControlState, $idTag, undef, $params,
   1.267 +            $isTrigger, $cookieState );
   1.268 +        $props = @propList ? " " . join( " ", @propList ) : '';
   1.269 +    }
   1.270 +    my $triggerTag = '<span' . $props . '>' . $imgLinkTag . '</span>' . ' ';
   1.271 +    return $triggerTag;
   1.272 +}
   1.273 +
   1.274 +sub _createHtmlProperties {
   1.275 +    my ( $twistyControlState, $idTag, $mode, $params, $isTrigger, $cookie ) =
   1.276 +      @_;
   1.277 +    my $class      = $params->{'class'}      || '';
   1.278 +    my $firststart = $params->{'firststart'} || '';
   1.279 +    my $firstStartHidden;
   1.280 +    $firstStartHidden = 1 if ( $firststart eq 'hide' );
   1.281 +    my $firstStartShown;
   1.282 +    $firstStartShown = 1 if ( $firststart eq 'show' );
   1.283 +    my $cookieShow;
   1.284 +    $cookieShow = 1 if defined $cookie && $cookie == 1;
   1.285 +    my $cookieHide;
   1.286 +    $cookieHide = 1 if defined $cookie && $cookie == 0;
   1.287 +    my $start = $params->{start} || '';
   1.288 +    my $startHidden;
   1.289 +    $startHidden = 1 if ( $start eq 'hide' );
   1.290 +    my $startShown;
   1.291 +    $startShown = 1 if ( $start eq 'show' );
   1.292 +    my $remember = $params->{'remember'} || $prefRemember;
   1.293 +    my $noscript = $params->{'noscript'} || '';
   1.294 +    my $noscriptHide;
   1.295 +    $noscriptHide = 1 if ( $noscript eq 'hide' );
   1.296 +    $mode ||= $prefMode;
   1.297 +
   1.298 +    my @classList = ();
   1.299 +    push( @classList, $class ) if $class && !$isTrigger;
   1.300 +    push( @classList, 'twistyRememberSetting' ) if ( $remember eq 'on' );
   1.301 +    push( @classList, 'twistyForgetSetting' )   if ( $remember eq 'off' );
   1.302 +    push( @classList, 'twistyStartHide' )      if $startHidden;
   1.303 +    push( @classList, 'twistyStartShow' )      if $startShown;
   1.304 +    push( @classList, 'twistyFirstStartHide' ) if $firstStartHidden;
   1.305 +    push( @classList, 'twistyFirstStartShow' ) if $firstStartShown;
   1.306 +
   1.307 +    # Mimic the rules in twist.js, function _update()
   1.308 +    my $state = '';
   1.309 +    $state = $TWISTYPLUGIN_CONTENT_HIDDEN if $firstStartHidden;
   1.310 +    $state = $TWISTYPLUGIN_CONTENT_SHOWN  if $firstStartShown;
   1.311 +
   1.312 +    # cookie setting may override  firstStartHidden and firstStartShown
   1.313 +    $state = $TWISTYPLUGIN_CONTENT_HIDDEN if $cookieHide;
   1.314 +    $state = $TWISTYPLUGIN_CONTENT_SHOWN  if $cookieShow;
   1.315 +
   1.316 +    # startHidden and startShown may override cookie
   1.317 +    $state = $TWISTYPLUGIN_CONTENT_HIDDEN if $startHidden;
   1.318 +    $state = $TWISTYPLUGIN_CONTENT_SHOWN  if $startShown;
   1.319 +
   1.320 +    # assume trigger should be hidden
   1.321 +    # unless explicitly said otherwise
   1.322 +    my $shouldHideTrigger = 1;
   1.323 +    if ($isTrigger) {
   1.324 +        push( @classList, 'twistyTrigger twikiUnvisited' );
   1.325 +
   1.326 +        if (   $state eq $TWISTYPLUGIN_CONTENT_SHOWN
   1.327 +            && $twistyControlState eq 'hide' )
   1.328 +        {
   1.329 +            $shouldHideTrigger = 0;
   1.330 +        }
   1.331 +        if (   $state eq $TWISTYPLUGIN_CONTENT_HIDDEN
   1.332 +            && $twistyControlState eq 'show' )
   1.333 +        {
   1.334 +            $shouldHideTrigger = 0;
   1.335 +        }
   1.336 +        push( @classList, 'twistyHidden' ) if $shouldHideTrigger;
   1.337 +    }
   1.338 +
   1.339 +    # assume content should be hidden
   1.340 +    # unless explicitly said otherwise
   1.341 +    my $shouldHideContent = 1;
   1.342 +    if ( !$isTrigger ) {
   1.343 +        push( @classList, 'twistyContent' );
   1.344 +
   1.345 +        if ( $state eq $TWISTYPLUGIN_CONTENT_SHOWN ) {
   1.346 +            $shouldHideContent = 0;
   1.347 +        }
   1.348 +        push( @classList, 'twikiMakeHidden' ) if $shouldHideContent;
   1.349 +    }
   1.350 +
   1.351 +    # deprecated
   1.352 +    # should be done by twiki template scripts instead
   1.353 +    if ( !$isTrigger && $noscriptHide ) {
   1.354 +        if ( $mode eq 'div' ) {
   1.355 +            push( @classList, 'twikiMakeVisibleBlock' );
   1.356 +        }
   1.357 +        else {
   1.358 +            push( @classList, 'twikiMakeVisibleInline' );
   1.359 +        }
   1.360 +    }
   1.361 +
   1.362 +    # let javascript know we have set the state already
   1.363 +    push( @classList, 'twistyInited' . $state );
   1.364 +
   1.365 +    my @propList = ();
   1.366 +    push( @propList, 'id="' . $idTag . '"' );
   1.367 +    my $classListString = join( " ", @classList );
   1.368 +    push( @propList, 'class="' . $classListString . '"' );
   1.369 +    return @propList;
   1.370 +}
   1.371 +
   1.372 +=pod
   1.373 +
   1.374 +Reads a setting from the TWIKIPREF cookie.
   1.375 +Returns:
   1.376 +   * 1 if the cookie has been set (meaning: show content)
   1.377 +   * 0 if the cookie is '0' (meaning: hide content)
   1.378 +   * undef if no cookie has been set
   1.379 +
   1.380 +=cut
   1.381 +
   1.382 +sub _readCookie {
   1.383 +    my ( $session, $idTag ) = @_;
   1.384 +
   1.385 +    return '' if !$idTag;
   1.386 +
   1.387 +    # which state do we use?
   1.388 +    my $cgi    = new CGI;
   1.389 +    my $cookie = $cgi->cookie('TWIKIPREF');
   1.390 +    my $tag    = $idTag;
   1.391 +    $tag =~ s/^(.*)(hide|show|toggle)$/$1/go;
   1.392 +    my $key = $TWISTYPLUGIN_COOKIE_PREFIX . $tag;
   1.393 +
   1.394 +    return unless ( defined($key) && defined($cookie) );
   1.395 +
   1.396 +    my $value = '';
   1.397 +    if ( $cookie =~ m/\b$key\=(.+?)\b/gi ) {
   1.398 +        $value = $1;
   1.399 +    }
   1.400 +
   1.401 +    return if $value eq '';
   1.402 +    return ( $value eq '1' ) ? 1 : 0;
   1.403 +}
   1.404 +
   1.405 +sub _wrapInButtonHtml {
   1.406 +    my ( $text, $mode ) = @_;
   1.407 +    return _wrapInContainerHideIfNoJavascripOpen($mode) . "\n" . $text
   1.408 +      . _wrapInContainerDivIfNoJavascripClose($mode);
   1.409 +}
   1.410 +
   1.411 +sub _wrapInContentHtmlOpen {
   1.412 +    return '<div style="display:inline;" class="twistyPlugin">';
   1.413 +}
   1.414 +
   1.415 +sub _wrapInContentHtmlClose {
   1.416 +    return '</div><!--/twistyPlugin-->';
   1.417 +}
   1.418 +
   1.419 +sub _wrapInContainerHideIfNoJavascripOpen {
   1.420 +    my ($mode) = shift;
   1.421 +    return '<' . $mode . ' class="twistyPlugin twikiMakeVisibleInline">';
   1.422 +}
   1.423 +
   1.424 +sub _wrapInContainerDivIfNoJavascripClose {
   1.425 +    my ($mode) = shift;
   1.426 +    return '</' . $mode . '><!--/twistyPlugin twikiMakeVisibleInline-->';
   1.427 +}
   1.428 +
   1.429 +sub _decodeFormatTokens {
   1.430 +    my $text = shift;
   1.431 +    return
   1.432 +      defined(&TWiki::Func::decodeFormatTokens)
   1.433 +      ? TWiki::Func::decodeFormatTokens($text)
   1.434 +      : _expandStandardEscapes($text);
   1.435 +}
   1.436 +
   1.437 +=pod
   1.438 +
   1.439 +For TWiki versions that do not implement TWiki::Func::decodeFormatTokens.
   1.440 +
   1.441 +=cut
   1.442 +
   1.443 +sub _expandStandardEscapes {
   1.444 +    my $text = shift;
   1.445 +    $text =~ s/\$n\(\)/\n/gos;    # expand '$n()' to new line
   1.446 +    my $alpha = TWiki::Func::getRegularExpression('mixedAlpha');
   1.447 +    $text =~ s/\$n([^$alpha]|$)/\n$1/gos;    # expand '$n' to new line
   1.448 +    $text =~ s/\$nop(\(\))?//gos;      # remove filler, useful for nested search
   1.449 +    $text =~ s/\$quot(\(\))?/\"/gos;   # expand double quote
   1.450 +    $text =~ s/\$percnt(\(\))?/\%/gos; # expand percent
   1.451 +    $text =~ s/\$dollar(\(\))?/\$/gos; # expand dollar
   1.452 +    return $text;
   1.453 +}
   1.454 +
   1.455 +1;