lib/TWiki/UI/Preview.pm
author Colas Nahaboo <colas@nahaboo.net>
Sat, 26 Jan 2008 15:50:53 +0100
changeset 0 414e01d06fd5
permissions -rw-r--r--
RELEASE 4.2.0 freetown
colas@0
     1
# Module of TWiki Enterprise Collaboration Platform, http://TWiki.org/
colas@0
     2
#
colas@0
     3
# Copyright (C) 1999-2007 Peter Thoeny, peter@thoeny.org
colas@0
     4
# and TWiki Contributors. All Rights Reserved. TWiki Contributors
colas@0
     5
# are listed in the AUTHORS file in the root of this distribution.
colas@0
     6
# NOTE: Please extend that file, not this notice.
colas@0
     7
#
colas@0
     8
# This program is free software; you can redistribute it and/or
colas@0
     9
# modify it under the terms of the GNU General Public License
colas@0
    10
# as published by the Free Software Foundation; either version 2
colas@0
    11
# of the License, or (at your option) any later version. For
colas@0
    12
# more details read LICENSE in the root of this distribution.
colas@0
    13
#
colas@0
    14
# This program is distributed in the hope that it will be useful,
colas@0
    15
# but WITHOUT ANY WARRANTY; without even the implied warranty of
colas@0
    16
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
colas@0
    17
#
colas@0
    18
# As per the GPL, removal of this notice is prohibited.
colas@0
    19
colas@0
    20
package TWiki::UI::Preview;
colas@0
    21
colas@0
    22
use strict;
colas@0
    23
use Error qw( :try );
colas@0
    24
colas@0
    25
require TWiki;
colas@0
    26
require TWiki::UI::Save;
colas@0
    27
require TWiki::OopsException;
colas@0
    28
colas@0
    29
require Assert;
colas@0
    30
colas@0
    31
sub preview {
colas@0
    32
    my $session = shift;
colas@0
    33
colas@0
    34
    my $query = $session->{cgiQuery};
colas@0
    35
    my $web = $session->{webName};
colas@0
    36
    my $topic = $session->{topicName};
colas@0
    37
    my $user = $session->{user};
colas@0
    38
colas@0
    39
    my( $meta, $text, $saveOpts, $merged ) =
colas@0
    40
      TWiki::UI::Save::buildNewTopic($session, 'preview');
colas@0
    41
colas@0
    42
    # Note: param(formtemplate) has already been decoded by buildNewTopic
colas@0
    43
    # so the $meta entry reflects if it was used.
colas@0
    44
    my $formFields = '';
colas@0
    45
    my $form = $meta->get('FORM') || '';
colas@0
    46
    if( $form ) {
colas@0
    47
        $form = $form->{name}; # used later on as well
colas@0
    48
        require TWiki::Form;
colas@0
    49
        my $formDef = new TWiki::Form( $session, $web, $form );
colas@0
    50
        unless( $formDef ) {
colas@0
    51
            throw TWiki::OopsException( 'attention',
colas@0
    52
                                        def => 'no_form_def',
colas@0
    53
                                        web => $session->{webName},
colas@0
    54
                                        topic => $session->{topicName},
colas@0
    55
                                        params => [ $web, $form ] );
colas@0
    56
        }
colas@0
    57
        $formFields = $formDef->renderHidden( $meta, 0 );
colas@0
    58
    }
colas@0
    59
colas@0
    60
    $session->{plugins}->afterEditHandler( $text, $topic, $web );
colas@0
    61
colas@0
    62
    my $skin = $session->getSkin();
colas@0
    63
    my $tmpl = $session->templates->readTemplate( 'preview', $skin );
colas@0
    64
    if( $saveOpts->{minor} ) {
colas@0
    65
        $tmpl =~ s/%DONTNOTIFYCHECKBOX%/checked="checked"/go;
colas@0
    66
    } else {
colas@0
    67
        $tmpl =~ s/%DONTNOTIFYCHECKBOX%//go;
colas@0
    68
    }
colas@0
    69
    if( $saveOpts->{forcenewrevision} ) {
colas@0
    70
        $tmpl =~ s/%FORCENEWREVISIONCHECKBOX%/checked="checked"/go;
colas@0
    71
    } else {
colas@0
    72
        $tmpl =~ s/%FORCENEWREVISIONCHECKBOX%//go;
colas@0
    73
    }
colas@0
    74
    my $saveCmd = $query->param( 'cmd' ) || '';
colas@0
    75
    $tmpl =~ s/%CMD%/$saveCmd/go;
colas@0
    76
colas@0
    77
    my $redirectTo = $query->param( 'redirectto' ) || '';
colas@0
    78
    $tmpl =~ s/%REDIRECTTO%/$redirectTo/go;
colas@0
    79
colas@0
    80
    $tmpl =~ s/%FORMTEMPLATE%/$form/g;
colas@0
    81
colas@0
    82
    my $parent = $meta->get('TOPICPARENT');
colas@0
    83
    $parent = $parent->{name} if( $parent );
colas@0
    84
    $parent ||= '';
colas@0
    85
    $tmpl =~ s/%TOPICPARENT%/$parent/g;
colas@0
    86
colas@0
    87
    $session->enterContext( 'can_render_meta', $meta );
colas@0
    88
colas@0
    89
    my $dispText = $text;
colas@0
    90
    $dispText = $session->handleCommonTags( $dispText, $web, $topic, $meta );
colas@0
    91
    $dispText = $session->renderer->getRenderedVersion( $dispText, $web, $topic );
colas@0
    92
colas@0
    93
    # Disable links and inputs in the text
colas@0
    94
    $dispText =~ s#<a\s[^>]*>(.*?)</a>#<span class="twikiEmulatedLink">$1</span>#gis;
colas@0
    95
    $dispText =~ s/<(input|button|textarea) /<$1 disabled="disabled" /gis;
colas@0
    96
    $dispText =~ s(</?form(|\s.*?)>)()gis;
colas@0
    97
    $dispText =~ s/(<[^>]*\bon[A-Za-z]+=)('[^']*'|"[^"]*")/$1''/gis;
colas@0
    98
colas@0
    99
    $tmpl = $session->handleCommonTags( $tmpl, $web, $topic, $meta );
colas@0
   100
    $tmpl = $session->renderer->getRenderedVersion( $tmpl, $web, $topic );
colas@0
   101
    $tmpl =~ s/%TEXT%/$dispText/go;
colas@0
   102
    $tmpl =~ s/%FORMFIELDS%/$formFields/go;
colas@0
   103
colas@0
   104
    # SMELL: this should be done using CGI::hidden
colas@0
   105
    $text = TWiki::entityEncode( $text, "\n" );
colas@0
   106
colas@0
   107
    $tmpl =~ s/%HIDDENTEXT%/$text/go;
colas@0
   108
colas@0
   109
    $tmpl =~ s/<\/?(nop|noautolink)\/?>//gis;
colas@0
   110
    
colas@0
   111
    #I don't know _where_ these should be done, so I'll do them as late as possible
colas@0
   112
    my $originalrev = $query->param( 'originalrev' ); # rev edit started on
colas@0
   113
    #ASSERT($originalrev ne '%ORIGINALREV%') if DEBUG;
colas@0
   114
    $tmpl =~ s/%ORIGINALREV%/$originalrev/go;
colas@0
   115
    my $templatetopic = $query->param( 'templatetopic');
colas@0
   116
    #ASSERT($templatetopic ne '%TEMPLATETOPIC%') if DEBUG;
colas@0
   117
    $tmpl =~ s/%TEMPLATETOPIC%/$templatetopic/go;
colas@0
   118
    #this one's worrying, its special, and not set much at all
colas@0
   119
    #$tmpl =~ s/%SETTINGSTOPIC%/$settingstopic/go;
colas@0
   120
    my $newtopic = $query->param( 'newtopic' );
colas@0
   121
    #ASSERT($newtopic ne '%NEWTOPIC%') if DEBUG;
colas@0
   122
    $tmpl =~ s/%NEWTOPIC%/$newtopic/go;
colas@0
   123
colas@0
   124
colas@0
   125
    $session->writeCompletePage( $tmpl );
colas@0
   126
}
colas@0
   127
colas@0
   128
1;