lib/TWiki/Plugins/EditTablePlugin.pm
author Colas Nahaboo <colas@nahaboo.net>
Sat, 26 Jan 2008 15:50:53 +0100
changeset 0 414e01d06fd5
child 1 e2915a7cbdfa
permissions -rw-r--r--
RELEASE 4.2.0 freetown
     1 # Plugin for TWiki Enterprise Collaboration Platform, http://TWiki.org/
     2 #
     3 # Copyright (C) 2002-2007 Peter Thoeny, peter@thoeny.org and TWiki
     4 # 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.
    15 #
    16 # As per the GPL, removal of this notice is prohibited.
    17 #
    18 # This is the EditTablePlugin used to edit tables in place.
    19 
    20 package TWiki::Plugins::EditTablePlugin;
    21 
    22 use strict;
    23 
    24 use vars qw(
    25   $web $topic $user $VERSION $RELEASE $debug
    26   $query $renderingWeb $usesJavascriptInterface $viewModeHeaderDone $editModeHeaderDone $encodeStart $encodeEnd $prefsInitialized $table
    27   %editMode %saveMode
    28 );
    29 
    30 # This should always be $Rev: 16239 (22 Jan 2008) $ so that TWiki can determine the checked-in
    31 # status of the plugin. It is used by the build automation tools, so
    32 # you should leave it alone.
    33 $VERSION = '$Rev: 16239 (22 Jan 2008) $';
    34 
    35 # This is a free-form string you can use to "name" your own plugin version.
    36 # It is *not* used by the build automation tools, but is reported as part
    37 # of the version number in PLUGINDESCRIPTIONS.
    38 $RELEASE = '4.7.10';
    39 
    40 $encodeStart = '--EditTableEncodeStart--';
    41 $encodeEnd   = '--EditTableEncodeEnd--';
    42 %editMode    = ( 'NONE', 0, 'EDIT', 1 );
    43 %saveMode    = ( 'NONE', 0, 'SAVE', 1, 'SAVEQUIET', 2 );
    44 
    45 sub initPlugin {
    46     ( $topic, $web, $user ) = @_;
    47 
    48     # check for Plugins.pm versions
    49     if ( $TWiki::Plugins::VERSION < 1.026 ) {
    50         TWiki::Func::writeWarning(
    51             "Version mismatch between EditTablePlugin and Plugins.pm");
    52         return 0;
    53     }
    54 
    55     $query = TWiki::Func::getCgiQuery();
    56     if ( !$query ) {
    57         return 0;
    58     }
    59 
    60     # Get plugin debug flag
    61     $debug = TWiki::Func::getPreferencesFlag('EDITTABLEPLUGIN_DEBUG');
    62     $usesJavascriptInterface =
    63       TWiki::Func::getPreferencesFlag('EDITTABLEPLUGIN_JAVASCRIPTINTERFACE');
    64     $viewModeHeaderDone = 0;
    65     $editModeHeaderDone = 0;
    66     $prefsInitialized   = 0;
    67     $renderingWeb       = $web;
    68 
    69     # Plugin correctly initialized
    70     TWiki::Func::writeDebug(
    71         "- TWiki::Plugins::EditTablePlugin::initPlugin( $web.$topic ) is OK")
    72       if $debug;
    73 
    74     # Initialize $table such that the code will correctly detect when to
    75     # read in a topic.
    76     undef $table;
    77 
    78     return 1;
    79 }
    80 
    81 sub commonTagsHandler {
    82     _process(@_);
    83 }
    84 
    85 sub _process {
    86     my ( $theText, $theTopic, $theWeb ) = @_;
    87 
    88     return unless $_[0] =~ /%EDIT(TABLE|CELL){(.*)}%/os;
    89     addViewModeHeadersToHead();
    90 
    91     require TWiki::Plugins::EditTablePlugin::Core;
    92     TWiki::Plugins::EditTablePlugin::Core::process( $_[0], $theTopic, $theWeb,
    93         $topic, $web );
    94 }
    95 
    96 sub postRenderingHandler {
    97 ### my ( $text ) = @_;   # do not uncomment, use $_[0] instead
    98     $_[0] =~ s/$encodeStart(.*?)$encodeEnd/decodeValue($1)/geos;
    99 }
   100 
   101 sub encodeValue {
   102     my ($theText) = @_;
   103 
   104     # FIXME: *very* crude encoding to escape Wiki rendering inside form fields
   105     # also prevents urls to get expanded to links
   106     $theText =~ s/\./%dot%/gos;
   107     $theText =~ s/(.)/\.$1/gos;
   108 
   109     # convert <br /> markup to unicode linebreak character for text areas
   110     $theText =~ s/.<.b.r. .\/.>. /&#10;/gos;
   111     return $encodeStart . $theText . $encodeEnd;
   112 }
   113 
   114 sub decodeValue {
   115     my ($theText) = @_;
   116 
   117     $theText =~ s/\.(.)/$1/gos;
   118     $theText =~ s/%dot%/\./gos;
   119     $theText =~ s/\&([^#a-z])/&amp;$1/go;    # escape non-entities
   120     $theText =~ s/</\&lt;/go;                # change < to entity
   121     $theText =~ s/>/\&gt;/go;                # change > to entity
   122     $theText =~ s/\"/\&quot;/go;             # change " to entity
   123     return $theText;
   124 }
   125 
   126 sub decodeFormatTokens {
   127     return if ( !$_[0] );
   128     $_[0] =~ s/\$n\(\)/\n/gos;    # expand '$n()' to new line
   129     my $alpha = TWiki::Func::getRegularExpression('mixedAlpha');
   130     $_[0] =~ s/\$n([^$alpha]|$)/\n$1/gos;    # expand '$n' to new line
   131     $_[0] =~ s/\$nop(\(\))?//gos;      # remove filler, useful for nested search
   132     $_[0] =~ s/\$quot(\(\))?/\"/gos;   # expand double quote
   133     $_[0] =~ s/\$percnt(\(\))?/\%/gos; # expand percent
   134     $_[0] =~ s/\$dollar(\(\))?/\$/gos; # expand dollar
   135 }
   136 
   137 =pod
   138 
   139 Style sheet for table in view mode
   140 
   141 =cut
   142 
   143 sub addViewModeHeadersToHead {
   144     return if $viewModeHeaderDone;
   145 
   146     $viewModeHeaderDone = 1;
   147 
   148     my $header = <<'EOF';
   149 <style type="text/css" media="all">
   150 @import url("%PUBURL%/%TWIKIWEB%/EditTablePlugin/edittable.css");
   151 </style>
   152 EOF
   153     TWiki::Func::addToHEAD( 'EDITTABLEPLUGIN', $header );
   154 }
   155 
   156 =pod
   157 
   158 Style sheet and javascript for table in edit mode
   159 
   160 =cut
   161 
   162 sub addEditModeHeadersToHead {
   163     my ( $tableNr, $assetUrl ) = @_;
   164 
   165     return if $editModeHeaderDone;
   166     return if !$usesJavascriptInterface;
   167 
   168     require TWiki::Contrib::BehaviourContrib;
   169     TWiki::Contrib::BehaviourContrib::addHEAD();
   170 
   171     $editModeHeaderDone = 1;
   172 
   173     my $tableId = "edittable$tableNr";
   174     my $header  = "";
   175     $header .=
   176       '<meta name="EDITTABLEPLUGIN_EditTableId" content="' . $tableId . '" />';
   177     $header .= "\n"
   178       . '<meta name="EDITTABLEPLUGIN_EditTableUrl" content="'
   179       . $assetUrl . '" />';
   180     $header .= <<'EOF';
   181 <style type="text/css" media="all">
   182 @import url("%PUBURL%/%TWIKIWEB%/EditTablePlugin/edittable.css");
   183 </style>
   184 <script type="text/javascript" src="%PUBURL%/%TWIKIWEB%/EditTablePlugin/edittable.js"></script>
   185 EOF
   186 
   187     TWiki::Func::addToHEAD( 'EDITTABLEPLUGIN', $header );
   188 }
   189 
   190 1;