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