# Copyright (C) 2005 ILOG http://www.ilog.fr # and TWiki Contributors. All Rights Reserved. TWiki Contributors # are listed in the AUTHORS file in the root of this distribution. # NOTE: Please extend that file, not this notice. # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License # as published by the Free Software Foundation; either version 2 # of the License, or (at your option) any later version. For # more details read LICENSE in the root of the TWiki distribution. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. # # As per the GPL, removal of this notice is prohibited. =pod ---+ package WysiwygPlugin This plugin is responsible for translating TML to HTML before an edit starts and translating the resultant HTML back into TML. The flow of control is as follows: 1 User hits "edit" 2 if the skin is WYWIWYGPLUGIN_WYWIWYGSKIN, the beforeEditHandler filters the edit 3 The 'edit' template is instantiated with all the js and css 4 editor invokes view URL with the 'wysiwyg_edit=1' parameter to obtain the clean document * The earliest possible handler is implemented by the plugin in this mode. This handler formats the text and then saves it so the rest of twiki rendering can't do anything to it. At the end of rendering it drops the saved text back in. 5 User edits 6 editor saves by posting to 'save' with the 'wysiwyg_edit=1' parameter 7 the beforeSaveHandler sees this and converts the HTML back to tml Note: In the case of a new topic, you might expect to see the "create topic" screen in the editor when it goesback to twiki for the topic content. This doesn't happen because the earliest possible handler is called on the topic content and not the template. The template is effectively ignored and a blank document is sent to the editor. Attachment uploads can be handled by URL requests from the editor to the TWiki upload script. If these uploads are done in an IFRAME, then the redirect at the end of the upload is done in the IFRAME and the user doesn't see the upload screens. This avoids the need to add any scripts to the bin dir. =cut package TWiki::Plugins::WysiwygPlugin; use CGI qw( -any ); use strict; use TWiki::Func; use vars qw( $VERSION $RELEASE $MODERN $SKIN $SHORTDESCRIPTION ); use vars qw( $html2tml $tml2html $recursionBlock $imgMap $cairoCalled ); use vars qw( %TWikiCompatibility @refs ); $SHORTDESCRIPTION = 'Translator framework for Wysiwyg editors'; $VERSION = '$Rev: 12422 $'; $RELEASE = 'Dakar'; sub initPlugin { my( $topic, $web, $user, $installWeb ) = @_; if( defined( &TWiki::Func::normalizeWebTopicName )) { $MODERN = 1; } else { # SMELL: nasty global var needed for Cairo $cairoCalled = 0; } $SKIN = TWiki::Func::getPreferencesValue( 'WYSIWYGPLUGIN_WYSIWYGSKIN' ); # %OWEB%.%OTOPIC% is the topic where the initial content should be # grabbed from, as defined in templates/edit.skin.tmpl TWiki::Func::registerTagHandler('OWEB',\&_OWEBTAG); TWiki::Func::registerTagHandler('OTOPIC',\&_OTOPICTAG); TWiki::Func::registerTagHandler('WYSIWYG_TEXT',\&_WYSIWYG_TEXT); TWiki::Func::registerTagHandler('JAVASCRIPT_TEXT',\&_JAVASCRIPT_TEXT); # Plugin correctly initialized return 1; } sub _OWEBTAG { my($session, $params, $theTopic, $theWeb) = @_; my $query = TWiki::Func::getCgiQuery(); return "$theWeb" unless $query; if(defined($query->param('templatetopic'))) { my @split=split(/\./,$query->param('templatetopic')); if($#split==0) { return $theWeb; } else { return $split[0]; } } return $theWeb; } sub _OTOPICTAG { my($session, $params, $theTopic, $theWeb) = @_; my $query = TWiki::Func::getCgiQuery(); return "$theTopic" unless $query; if(defined($query->param('templatetopic'))) { my @split=split(/\./,$query->param('templatetopic')); return $split[$#split]; } return $theTopic; } # This handler is used to determine whether the topic is editable by # Wysiwyg or not. The only thing it does is to redirect to a normal edit # url if the skin is set to $SKIN and nasty content is found. sub beforeEditHandler { #my( $text, $topic, $web, $meta ) = @_; return unless $SKIN; if( TWiki::Func::getSkin() =~ /\b$SKIN\b/o ) { my $exclusions = TWiki::Func::getPreferencesValue( 'WYSIWYG_EXCLUDE' ); my $calls_ok = TWiki::Func::getPreferencesValue( 'WYSIWYG_EDITABLE_CALLS' ); return unless $exclusions; my $not_ok = 0; if( $exclusions =~ /calls/ && $_[0] =~ /%((?!($calls_ok){)[A-Z_]+{.*?})%/s ) { #print STDERR "WYSIWYG_DEBUG: has calls $1\n"; $not_ok = 1; } if( $exclusions =~ /variables/ && $_[0] =~ /%([A-Z_]+)%/s ) { #print STDERR "$exclusions WYSIWYG_DEBUG: has variables $1\n"; $not_ok = 1; } if( $exclusions =~ /html/ && $_[0] =~ /<\/?((?!literal|verbatim|noautolink|nop|br)\w+)/ ) { #print STDERR "WYSIWYG_DEBUG: has html: $1\n"; $not_ok = 1; } if( $exclusions =~ /comments/ && $_[0] =~ /<[!]--/ ) { #print STDERR "WYSIWYG_DEBUG: has comments\n"; $not_ok = 1; } if( $exclusions =~ /pre/ && $_[0] =~ /
param( $p ); if( $arg && $arg =~ s/\b$SKIN\b//o ) { if( $arg =~ /^[\s,]*$/ ) { $query->delete( $p ); } else { $query->param( -name=>$p, -value=>$arg ); } } } my $url = $query->url( -full=>1, -path=>1, -query=>1 ); TWiki::Func::redirectCgiQuery( $query, $url ); # Bring this session to an untimely end exit 0; } } } # Invoked when the selected skin is in use to convert HTML to # TML (best offorts) sub beforeSaveHandler { #my( $text, $topic, $web ) = @_; my $query = TWiki::Func::getCgiQuery(); return unless $query; return unless defined( $query->param( 'wysiwyg_edit' )); unless( $html2tml ) { require TWiki::Plugins::WysiwygPlugin::HTML2TML; $html2tml = new TWiki::Plugins::WysiwygPlugin::HTML2TML(); } my @rescue; # SMELL: really, really bad smell; bloody core should NOT pass text # with embedded meta to plugins! It is VERY BAD DESIGN!!! $_[0] =~ s/^(%META:[A-Z]+{.*?}%)\s*$/push(@rescue,$1);''/gem; unless( $MODERN ) { # undo the munging that has already been done (grrrrrrrrrr!!!!) $_[0] =~ s/\t/ /g; } my $opts = { web => $_[2], topic => $_[1], convertImage => \&convertImage, rewriteURL => \&postConvertURL, very_clean => 1, # aggressively polish saved HTML }; # Let's just set this and see what happens.... $opts->{very_clean} = 1; $_[0] = $html2tml->convert( $_[0], $opts ); unless( $MODERN ) { # redo the munging $_[0] =~ s/ /\t/g; } $_[0] =~ s/\n/\n$rescue[$1-1]/gs; # Add a newline if one has been eaten $_[0] =~ s//\n$rescue[$1-1]/g; } # Handler used to process text in a =view= URL to generate text/html # containing the HTML of the topic to be edited. # # Invoked when the selected skin is in use to convert the text to HTML # We can't use the beforeEditHandler, because the editor loads up and then # uses a URL to fetch the text to be edited. This handler is designed to # provide the text for that request. It's a real struggle, because the # commonTagsHandler is called so many times that getting the right # call is hard, and then preventing a repeat call is harder! sub beforeCommonTagsHandler { #my ( $text, $topic, $web ) return if $recursionBlock; if( $MODERN ) { return unless TWiki::Func::getContext()->{body_text}; } else { # DANGEROUS SMELL: only way to tell what we are processing is # the order of the calls to commonTagsHandler - the first call after # initPlugin is the body text in Cairo. We only want to process the # body text. return if( $cairoCalled ); $cairoCalled = 1; } my $query = TWiki::Func::getCgiQuery(); return unless $query; return unless defined( $query->param( 'wysiwyg_edit' )); # stop it from processing the template without expanded # %TEXT% (grr; we need a better way to tell where we # are in the processing pipeline) return if( $_[0] =~ /^