1 # Module of TWiki Enterprise Collaboration Platform, http://TWiki.org/
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.
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.
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.
18 # As per the GPL, removal of this notice is prohibited.
20 package TWiki::UI::Preview;
26 require TWiki::UI::Save;
27 require TWiki::OopsException;
34 my $query = $session->{cgiQuery};
35 my $web = $session->{webName};
36 my $topic = $session->{topicName};
37 my $user = $session->{user};
39 my( $meta, $text, $saveOpts, $merged ) =
40 TWiki::UI::Save::buildNewTopic($session, 'preview');
42 # Note: param(formtemplate) has already been decoded by buildNewTopic
43 # so the $meta entry reflects if it was used.
45 my $form = $meta->get('FORM') || '';
47 $form = $form->{name}; # used later on as well
49 my $formDef = new TWiki::Form( $session, $web, $form );
51 throw TWiki::OopsException( 'attention',
53 web => $session->{webName},
54 topic => $session->{topicName},
55 params => [ $web, $form ] );
57 $formFields = $formDef->renderHidden( $meta, 0 );
60 $session->{plugins}->afterEditHandler( $text, $topic, $web );
62 my $skin = $session->getSkin();
63 my $tmpl = $session->templates->readTemplate( 'preview', $skin );
64 if( $saveOpts->{minor} ) {
65 $tmpl =~ s/%DONTNOTIFYCHECKBOX%/checked="checked"/go;
67 $tmpl =~ s/%DONTNOTIFYCHECKBOX%//go;
69 if( $saveOpts->{forcenewrevision} ) {
70 $tmpl =~ s/%FORCENEWREVISIONCHECKBOX%/checked="checked"/go;
72 $tmpl =~ s/%FORCENEWREVISIONCHECKBOX%//go;
74 my $saveCmd = $query->param( 'cmd' ) || '';
75 $tmpl =~ s/%CMD%/$saveCmd/go;
77 my $redirectTo = $query->param( 'redirectto' ) || '';
78 $tmpl =~ s/%REDIRECTTO%/$redirectTo/go;
80 $tmpl =~ s/%FORMTEMPLATE%/$form/g;
82 my $parent = $meta->get('TOPICPARENT');
83 $parent = $parent->{name} if( $parent );
85 $tmpl =~ s/%TOPICPARENT%/$parent/g;
87 $session->enterContext( 'can_render_meta', $meta );
90 $dispText = $session->handleCommonTags( $dispText, $web, $topic, $meta );
91 $dispText = $session->renderer->getRenderedVersion( $dispText, $web, $topic );
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;
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;
104 # SMELL: this should be done using CGI::hidden
105 $text = TWiki::entityEncode( $text, "\n" );
107 $tmpl =~ s/%HIDDENTEXT%/$text/go;
109 $tmpl =~ s/<\/?(nop|noautolink)\/?>//gis;
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;
125 $session->writeCompletePage( $tmpl );