lib/TWiki/UI/Preview.pm
changeset 0 414e01d06fd5
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/lib/TWiki/UI/Preview.pm	Sat Jan 26 15:50:53 2008 +0100
     1.3 @@ -0,0 +1,128 @@
     1.4 +# Module of TWiki Enterprise Collaboration Platform, http://TWiki.org/
     1.5 +#
     1.6 +# Copyright (C) 1999-2007 Peter Thoeny, peter@thoeny.org
     1.7 +# and TWiki Contributors. All Rights Reserved. TWiki Contributors
     1.8 +# are listed in the AUTHORS file in the root of this distribution.
     1.9 +# NOTE: Please extend that file, not this notice.
    1.10 +#
    1.11 +# This program is free software; you can redistribute it and/or
    1.12 +# modify it under the terms of the GNU General Public License
    1.13 +# as published by the Free Software Foundation; either version 2
    1.14 +# of the License, or (at your option) any later version. For
    1.15 +# more details read LICENSE in the root of this distribution.
    1.16 +#
    1.17 +# This program is distributed in the hope that it will be useful,
    1.18 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.19 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    1.20 +#
    1.21 +# As per the GPL, removal of this notice is prohibited.
    1.22 +
    1.23 +package TWiki::UI::Preview;
    1.24 +
    1.25 +use strict;
    1.26 +use Error qw( :try );
    1.27 +
    1.28 +require TWiki;
    1.29 +require TWiki::UI::Save;
    1.30 +require TWiki::OopsException;
    1.31 +
    1.32 +require Assert;
    1.33 +
    1.34 +sub preview {
    1.35 +    my $session = shift;
    1.36 +
    1.37 +    my $query = $session->{cgiQuery};
    1.38 +    my $web = $session->{webName};
    1.39 +    my $topic = $session->{topicName};
    1.40 +    my $user = $session->{user};
    1.41 +
    1.42 +    my( $meta, $text, $saveOpts, $merged ) =
    1.43 +      TWiki::UI::Save::buildNewTopic($session, 'preview');
    1.44 +
    1.45 +    # Note: param(formtemplate) has already been decoded by buildNewTopic
    1.46 +    # so the $meta entry reflects if it was used.
    1.47 +    my $formFields = '';
    1.48 +    my $form = $meta->get('FORM') || '';
    1.49 +    if( $form ) {
    1.50 +        $form = $form->{name}; # used later on as well
    1.51 +        require TWiki::Form;
    1.52 +        my $formDef = new TWiki::Form( $session, $web, $form );
    1.53 +        unless( $formDef ) {
    1.54 +            throw TWiki::OopsException( 'attention',
    1.55 +                                        def => 'no_form_def',
    1.56 +                                        web => $session->{webName},
    1.57 +                                        topic => $session->{topicName},
    1.58 +                                        params => [ $web, $form ] );
    1.59 +        }
    1.60 +        $formFields = $formDef->renderHidden( $meta, 0 );
    1.61 +    }
    1.62 +
    1.63 +    $session->{plugins}->afterEditHandler( $text, $topic, $web );
    1.64 +
    1.65 +    my $skin = $session->getSkin();
    1.66 +    my $tmpl = $session->templates->readTemplate( 'preview', $skin );
    1.67 +    if( $saveOpts->{minor} ) {
    1.68 +        $tmpl =~ s/%DONTNOTIFYCHECKBOX%/checked="checked"/go;
    1.69 +    } else {
    1.70 +        $tmpl =~ s/%DONTNOTIFYCHECKBOX%//go;
    1.71 +    }
    1.72 +    if( $saveOpts->{forcenewrevision} ) {
    1.73 +        $tmpl =~ s/%FORCENEWREVISIONCHECKBOX%/checked="checked"/go;
    1.74 +    } else {
    1.75 +        $tmpl =~ s/%FORCENEWREVISIONCHECKBOX%//go;
    1.76 +    }
    1.77 +    my $saveCmd = $query->param( 'cmd' ) || '';
    1.78 +    $tmpl =~ s/%CMD%/$saveCmd/go;
    1.79 +
    1.80 +    my $redirectTo = $query->param( 'redirectto' ) || '';
    1.81 +    $tmpl =~ s/%REDIRECTTO%/$redirectTo/go;
    1.82 +
    1.83 +    $tmpl =~ s/%FORMTEMPLATE%/$form/g;
    1.84 +
    1.85 +    my $parent = $meta->get('TOPICPARENT');
    1.86 +    $parent = $parent->{name} if( $parent );
    1.87 +    $parent ||= '';
    1.88 +    $tmpl =~ s/%TOPICPARENT%/$parent/g;
    1.89 +
    1.90 +    $session->enterContext( 'can_render_meta', $meta );
    1.91 +
    1.92 +    my $dispText = $text;
    1.93 +    $dispText = $session->handleCommonTags( $dispText, $web, $topic, $meta );
    1.94 +    $dispText = $session->renderer->getRenderedVersion( $dispText, $web, $topic );
    1.95 +
    1.96 +    # Disable links and inputs in the text
    1.97 +    $dispText =~ s#<a\s[^>]*>(.*?)</a>#<span class="twikiEmulatedLink">$1</span>#gis;
    1.98 +    $dispText =~ s/<(input|button|textarea) /<$1 disabled="disabled" /gis;
    1.99 +    $dispText =~ s(</?form(|\s.*?)>)()gis;
   1.100 +    $dispText =~ s/(<[^>]*\bon[A-Za-z]+=)('[^']*'|"[^"]*")/$1''/gis;
   1.101 +
   1.102 +    $tmpl = $session->handleCommonTags( $tmpl, $web, $topic, $meta );
   1.103 +    $tmpl = $session->renderer->getRenderedVersion( $tmpl, $web, $topic );
   1.104 +    $tmpl =~ s/%TEXT%/$dispText/go;
   1.105 +    $tmpl =~ s/%FORMFIELDS%/$formFields/go;
   1.106 +
   1.107 +    # SMELL: this should be done using CGI::hidden
   1.108 +    $text = TWiki::entityEncode( $text, "\n" );
   1.109 +
   1.110 +    $tmpl =~ s/%HIDDENTEXT%/$text/go;
   1.111 +
   1.112 +    $tmpl =~ s/<\/?(nop|noautolink)\/?>//gis;
   1.113 +    
   1.114 +    #I don't know _where_ these should be done, so I'll do them as late as possible
   1.115 +    my $originalrev = $query->param( 'originalrev' ); # rev edit started on
   1.116 +    #ASSERT($originalrev ne '%ORIGINALREV%') if DEBUG;
   1.117 +    $tmpl =~ s/%ORIGINALREV%/$originalrev/go;
   1.118 +    my $templatetopic = $query->param( 'templatetopic');
   1.119 +    #ASSERT($templatetopic ne '%TEMPLATETOPIC%') if DEBUG;
   1.120 +    $tmpl =~ s/%TEMPLATETOPIC%/$templatetopic/go;
   1.121 +    #this one's worrying, its special, and not set much at all
   1.122 +    #$tmpl =~ s/%SETTINGSTOPIC%/$settingstopic/go;
   1.123 +    my $newtopic = $query->param( 'newtopic' );
   1.124 +    #ASSERT($newtopic ne '%NEWTOPIC%') if DEBUG;
   1.125 +    $tmpl =~ s/%NEWTOPIC%/$newtopic/go;
   1.126 +
   1.127 +
   1.128 +    $session->writeCompletePage( $tmpl );
   1.129 +}
   1.130 +
   1.131 +1;