1# Module of TWiki Enterprise Collaboration Platform, http://TWiki.org/ 2# 3# Copyright (C) 1999-2018 Peter Thoeny, peter[at]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 3 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 20package TWiki::UI::Preview; 21 22use strict; 23use Error qw( :try ); 24 25require TWiki; 26require TWiki::UI::Save; 27require TWiki::OopsException; 28 29require Assert; 30 31sub preview { 32 my $session = shift; 33 34 my $query = $session->{request}; 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}->dispatch( 61 'afterEditHandler', $text, $topic, $web ); 62 63 my $skin = $session->getSkin(); 64 my $tmpl = $session->templates->readTemplate( 'preview', $skin ); 65 if( $saveOpts->{minor} ) { 66 $tmpl =~ s/%DONTNOTIFYCHECKBOX%/checked="checked"/go; 67 } else { 68 $tmpl =~ s/%DONTNOTIFYCHECKBOX%//go; 69 } 70 if( $saveOpts->{forcenewrevision} ) { 71 $tmpl =~ s/%FORCENEWREVISIONCHECKBOX%/checked="checked"/go; 72 } else { 73 $tmpl =~ s/%FORCENEWREVISIONCHECKBOX%//go; 74 } 75 my $saveCmd = $query->param( 'cmd' ) || ''; 76 $tmpl =~ s/%CMD%/$saveCmd/go; 77 78 my $redirectTo = $query->param( 'redirectto' ) || ''; 79 $tmpl =~ s/%REDIRECTTO%/$redirectTo/go; 80 81 my $metaPreferences = $query->param( 'metapreferences' ) || ''; 82 $metaPreferences = TWiki::entityEncode( $metaPreferences, "\n" ); 83 $tmpl =~ s/%METAPREFERENCES%/$metaPreferences/go; 84 85 $tmpl =~ s/%FORMTEMPLATE%/$form/g; 86 87 my $parent = $meta->get('TOPICPARENT'); 88 $parent = $parent->{name} if( $parent ); 89 $parent ||= ''; 90 $tmpl =~ s/%TOPICPARENT%/$parent/g; 91 92 $session->enterContext( 'can_render_meta', $meta ); 93 94 my $dispText = $text; 95 $dispText = $session->handleCommonTags( $dispText, $web, $topic, $meta ); 96 $dispText = $session->renderer->getRenderedVersion( $dispText, $web, $topic ); 97 98 # Disable links and inputs in the text 99 $dispText =~ s#<a\s[^>]*>(.*?)</a>#<span class="twikiEmulatedLink">$1</span>#gis; 100 $dispText =~ s/<(input|button|textarea) /<$1 disabled="disabled" /gis; 101 $dispText =~ s(</?form(|\s.*?)>)()gis; 102 $dispText =~ s/(<[^>]*\bon[A-Za-z]+=)('[^']*'|"[^"]*")/$1''/gis; 103 104 $tmpl = $session->handleCommonTags( $tmpl, $web, $topic, $meta ); 105 $tmpl = $session->renderer->getRenderedVersion( $tmpl, $web, $topic ); 106 $tmpl =~ s/%TEXT%/$dispText/go; 107 $tmpl =~ s/%FORMFIELDS%/$formFields/go; 108 109 # SMELL: this should be done using CGI::hidden 110 $text = TWiki::entityEncode( $text, "\n" ); 111 112 $tmpl =~ s/%HIDDENTEXT%/$text/go; 113 114 $tmpl =~ s/<\/?(nop|noautolink)\/?>//gis; 115 116 #I don't know _where_ these should be done, so I'll do them as late as possible 117 my $originalrev = $query->param( 'originalrev' ); # rev edit started on 118 #ASSERT($originalrev ne '%ORIGINALREV%') if DEBUG; 119 $tmpl =~ s/%ORIGINALREV%/$originalrev/go; 120 my $templatetopic = $query->param( 'templatetopic'); 121 #ASSERT($templatetopic ne '%TEMPLATETOPIC%') if DEBUG; 122 $tmpl =~ s/%TEMPLATETOPIC%/$templatetopic/go; 123 my $newtopic = $query->param( 'newtopic' ); 124 #ASSERT($newtopic ne '%NEWTOPIC%') if DEBUG; 125 $tmpl =~ s/%NEWTOPIC%/$newtopic/go; 126 127 128 $session->writeCompletePage( $tmpl ); 129} 130 1311; 132