lib/TWiki/Plugins/EditTablePlugin.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/EditTablePlugin.pm	Sat Jan 26 15:50:53 2008 +0100
     1.3 @@ -0,0 +1,190 @@
     1.4 +# Plugin for TWiki Enterprise Collaboration Platform, http://TWiki.org/
     1.5 +#
     1.6 +# Copyright (C) 2002-2007 Peter Thoeny, peter@thoeny.org and TWiki
     1.7 +# Contributors.
     1.8 +#
     1.9 +# This program is free software; you can redistribute it and/or
    1.10 +# modify it under the terms of the GNU General Public License
    1.11 +# as published by the Free Software Foundation; either version 2
    1.12 +# of the License, or (at your option) any later version. For
    1.13 +# more details read LICENSE in the root of this distribution.
    1.14 +#
    1.15 +# This program is distributed in the hope that it will be useful,
    1.16 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.17 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    1.18 +#
    1.19 +# As per the GPL, removal of this notice is prohibited.
    1.20 +#
    1.21 +# This is the EditTablePlugin used to edit tables in place.
    1.22 +
    1.23 +package TWiki::Plugins::EditTablePlugin;
    1.24 +
    1.25 +use strict;
    1.26 +
    1.27 +use vars qw(
    1.28 +  $web $topic $user $VERSION $RELEASE $debug
    1.29 +  $query $renderingWeb $usesJavascriptInterface $viewModeHeaderDone $editModeHeaderDone $encodeStart $encodeEnd $prefsInitialized $table
    1.30 +  %editMode %saveMode
    1.31 +);
    1.32 +
    1.33 +# This should always be $Rev: 16239 (22 Jan 2008) $ so that TWiki can determine the checked-in
    1.34 +# status of the plugin. It is used by the build automation tools, so
    1.35 +# you should leave it alone.
    1.36 +$VERSION = '$Rev: 16239 (22 Jan 2008) $';
    1.37 +
    1.38 +# This is a free-form string you can use to "name" your own plugin version.
    1.39 +# It is *not* used by the build automation tools, but is reported as part
    1.40 +# of the version number in PLUGINDESCRIPTIONS.
    1.41 +$RELEASE = '4.7.10';
    1.42 +
    1.43 +$encodeStart = '--EditTableEncodeStart--';
    1.44 +$encodeEnd   = '--EditTableEncodeEnd--';
    1.45 +%editMode    = ( 'NONE', 0, 'EDIT', 1 );
    1.46 +%saveMode    = ( 'NONE', 0, 'SAVE', 1, 'SAVEQUIET', 2 );
    1.47 +
    1.48 +sub initPlugin {
    1.49 +    ( $topic, $web, $user ) = @_;
    1.50 +
    1.51 +    # check for Plugins.pm versions
    1.52 +    if ( $TWiki::Plugins::VERSION < 1.026 ) {
    1.53 +        TWiki::Func::writeWarning(
    1.54 +            "Version mismatch between EditTablePlugin and Plugins.pm");
    1.55 +        return 0;
    1.56 +    }
    1.57 +
    1.58 +    $query = TWiki::Func::getCgiQuery();
    1.59 +    if ( !$query ) {
    1.60 +        return 0;
    1.61 +    }
    1.62 +
    1.63 +    # Get plugin debug flag
    1.64 +    $debug = TWiki::Func::getPreferencesFlag('EDITTABLEPLUGIN_DEBUG');
    1.65 +    $usesJavascriptInterface =
    1.66 +      TWiki::Func::getPreferencesFlag('EDITTABLEPLUGIN_JAVASCRIPTINTERFACE');
    1.67 +    $viewModeHeaderDone = 0;
    1.68 +    $editModeHeaderDone = 0;
    1.69 +    $prefsInitialized   = 0;
    1.70 +    $renderingWeb       = $web;
    1.71 +
    1.72 +    # Plugin correctly initialized
    1.73 +    TWiki::Func::writeDebug(
    1.74 +        "- TWiki::Plugins::EditTablePlugin::initPlugin( $web.$topic ) is OK")
    1.75 +      if $debug;
    1.76 +
    1.77 +    # Initialize $table such that the code will correctly detect when to
    1.78 +    # read in a topic.
    1.79 +    undef $table;
    1.80 +
    1.81 +    return 1;
    1.82 +}
    1.83 +
    1.84 +sub commonTagsHandler {
    1.85 +    _process(@_);
    1.86 +}
    1.87 +
    1.88 +sub _process {
    1.89 +    my ( $theText, $theTopic, $theWeb ) = @_;
    1.90 +
    1.91 +    return unless $_[0] =~ /%EDIT(TABLE|CELL){(.*)}%/os;
    1.92 +    addViewModeHeadersToHead();
    1.93 +
    1.94 +    require TWiki::Plugins::EditTablePlugin::Core;
    1.95 +    TWiki::Plugins::EditTablePlugin::Core::process( $_[0], $theTopic, $theWeb,
    1.96 +        $topic, $web );
    1.97 +}
    1.98 +
    1.99 +sub postRenderingHandler {
   1.100 +### my ( $text ) = @_;   # do not uncomment, use $_[0] instead
   1.101 +    $_[0] =~ s/$encodeStart(.*?)$encodeEnd/decodeValue($1)/geos;
   1.102 +}
   1.103 +
   1.104 +sub encodeValue {
   1.105 +    my ($theText) = @_;
   1.106 +
   1.107 +    # FIXME: *very* crude encoding to escape Wiki rendering inside form fields
   1.108 +    # also prevents urls to get expanded to links
   1.109 +    $theText =~ s/\./%dot%/gos;
   1.110 +    $theText =~ s/(.)/\.$1/gos;
   1.111 +
   1.112 +    # convert <br /> markup to unicode linebreak character for text areas
   1.113 +    $theText =~ s/.<.b.r. .\/.>. /&#10;/gos;
   1.114 +    return $encodeStart . $theText . $encodeEnd;
   1.115 +}
   1.116 +
   1.117 +sub decodeValue {
   1.118 +    my ($theText) = @_;
   1.119 +
   1.120 +    $theText =~ s/\.(.)/$1/gos;
   1.121 +    $theText =~ s/%dot%/\./gos;
   1.122 +    $theText =~ s/\&([^#a-z])/&amp;$1/go;    # escape non-entities
   1.123 +    $theText =~ s/</\&lt;/go;                # change < to entity
   1.124 +    $theText =~ s/>/\&gt;/go;                # change > to entity
   1.125 +    $theText =~ s/\"/\&quot;/go;             # change " to entity
   1.126 +    return $theText;
   1.127 +}
   1.128 +
   1.129 +sub decodeFormatTokens {
   1.130 +    return if ( !$_[0] );
   1.131 +    $_[0] =~ s/\$n\(\)/\n/gos;    # expand '$n()' to new line
   1.132 +    my $alpha = TWiki::Func::getRegularExpression('mixedAlpha');
   1.133 +    $_[0] =~ s/\$n([^$alpha]|$)/\n$1/gos;    # expand '$n' to new line
   1.134 +    $_[0] =~ s/\$nop(\(\))?//gos;      # remove filler, useful for nested search
   1.135 +    $_[0] =~ s/\$quot(\(\))?/\"/gos;   # expand double quote
   1.136 +    $_[0] =~ s/\$percnt(\(\))?/\%/gos; # expand percent
   1.137 +    $_[0] =~ s/\$dollar(\(\))?/\$/gos; # expand dollar
   1.138 +}
   1.139 +
   1.140 +=pod
   1.141 +
   1.142 +Style sheet for table in view mode
   1.143 +
   1.144 +=cut
   1.145 +
   1.146 +sub addViewModeHeadersToHead {
   1.147 +    return if $viewModeHeaderDone;
   1.148 +
   1.149 +    $viewModeHeaderDone = 1;
   1.150 +
   1.151 +    my $header = <<'EOF';
   1.152 +<style type="text/css" media="all">
   1.153 +@import url("%PUBURL%/%TWIKIWEB%/EditTablePlugin/edittable.css");
   1.154 +</style>
   1.155 +EOF
   1.156 +    TWiki::Func::addToHEAD( 'EDITTABLEPLUGIN', $header );
   1.157 +}
   1.158 +
   1.159 +=pod
   1.160 +
   1.161 +Style sheet and javascript for table in edit mode
   1.162 +
   1.163 +=cut
   1.164 +
   1.165 +sub addEditModeHeadersToHead {
   1.166 +    my ( $tableNr, $assetUrl ) = @_;
   1.167 +
   1.168 +    return if $editModeHeaderDone;
   1.169 +    return if !$usesJavascriptInterface;
   1.170 +
   1.171 +    require TWiki::Contrib::BehaviourContrib;
   1.172 +    TWiki::Contrib::BehaviourContrib::addHEAD();
   1.173 +
   1.174 +    $editModeHeaderDone = 1;
   1.175 +
   1.176 +    my $tableId = "edittable$tableNr";
   1.177 +    my $header  = "";
   1.178 +    $header .=
   1.179 +      '<meta name="EDITTABLEPLUGIN_EditTableId" content="' . $tableId . '" />';
   1.180 +    $header .= "\n"
   1.181 +      . '<meta name="EDITTABLEPLUGIN_EditTableUrl" content="'
   1.182 +      . $assetUrl . '" />';
   1.183 +    $header .= <<'EOF';
   1.184 +<style type="text/css" media="all">
   1.185 +@import url("%PUBURL%/%TWIKIWEB%/EditTablePlugin/edittable.css");
   1.186 +</style>
   1.187 +<script type="text/javascript" src="%PUBURL%/%TWIKIWEB%/EditTablePlugin/edittable.js"></script>
   1.188 +EOF
   1.189 +
   1.190 +    TWiki::Func::addToHEAD( 'EDITTABLEPLUGIN', $header );
   1.191 +}
   1.192 +
   1.193 +1;