~kosova/+junk/tuxfamily-twiki

« back to all changes in this revision

Viewing changes to foswiki/lib/Foswiki/UI/Preview.pm

  • Committer: James Michael DuPont
  • Date: 2009-07-18 19:58:49 UTC
  • Revision ID: jamesmikedupont@gmail.com-20090718195849-vgbmaht2ys791uo2
added foswiki

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# See bottom of file for license and copyright information
 
2
 
 
3
package Foswiki::UI::Preview;
 
4
 
 
5
use strict;
 
6
use Error qw( :try );
 
7
 
 
8
require Foswiki;
 
9
require Foswiki::UI::Save;
 
10
require Foswiki::OopsException;
 
11
 
 
12
require Assert;
 
13
 
 
14
sub preview {
 
15
    my $session = shift;
 
16
 
 
17
    my $query = $session->{request};
 
18
    my $web   = $session->{webName};
 
19
    my $topic = $session->{topicName};
 
20
    my $user  = $session->{user};
 
21
 
 
22
    my ( $meta, $text, $saveOpts, $merged ) =
 
23
      Foswiki::UI::Save::buildNewTopic( $session, 'preview' );
 
24
 
 
25
    # Note: param(formtemplate) has already been decoded by buildNewTopic
 
26
    # so the $meta entry reflects if it was used.
 
27
    # get form fields to pass on
 
28
    my $formFields = '';
 
29
    my $form = $meta->get('FORM') || '';
 
30
    my $formName;
 
31
    if ($form) {
 
32
        $formName = $form->{name};    # used later on as well
 
33
        require Foswiki::Form;
 
34
        my $formDef = new Foswiki::Form( $session, $web, $formName );
 
35
        unless ($formDef) {
 
36
            throw Foswiki::OopsException(
 
37
                'attention',
 
38
                def    => 'no_form_def',
 
39
                web    => $session->{webName},
 
40
                topic  => $session->{topicName},
 
41
                params => [ $web, $formName ]
 
42
            );
 
43
        }
 
44
        $formFields = $formDef->renderHidden( $meta, 0 );
 
45
    }
 
46
 
 
47
    $session->{plugins}->dispatch( 'afterEditHandler', $text, $topic, $web );
 
48
 
 
49
    my $skin     = $session->getSkin();
 
50
    my $template = $session->{prefs}->getPreferencesValue('VIEW_TEMPLATE')
 
51
      || 'preview';
 
52
    my $tmpl = $session->templates->readTemplate( $template, $skin );
 
53
 
 
54
    # if a VIEW_TEMPLATE is set, but does not exist or is not readable,
 
55
    # revert to 'preview' template (same code as View.pm)
 
56
    if ( !$tmpl && $template ne 'preview' ) {
 
57
        $tmpl = $session->templates->readTemplate( 'preview', $skin );
 
58
        $template = 'preview';
 
59
    }
 
60
 
 
61
    my $content = '';
 
62
    if ( $template eq 'preview' ) {
 
63
        $content = $text;
 
64
    }
 
65
    else {
 
66
 
 
67
        # only get the contents of TMPL:DEF{"content"}
 
68
        $content = $session->templates->expandTemplate('content');
 
69
 
 
70
        # put the text we have inside this template's content
 
71
        $content =~ s/%TEXT%/$text/go;
 
72
        
 
73
        # now we are ready to put the expanded and styled topic content in the
 
74
        # 'normal' preview template
 
75
    }
 
76
 
 
77
    $tmpl = $session->templates->readTemplate( 'preview', $skin );
 
78
 
 
79
    if ( $saveOpts->{minor} ) {
 
80
        $tmpl =~ s/%DONTNOTIFYCHECKBOX%/checked="checked"/go;
 
81
    }
 
82
    else {
 
83
        $tmpl =~ s/%DONTNOTIFYCHECKBOX%//go;
 
84
    }
 
85
    if ( $saveOpts->{forcenewrevision} ) {
 
86
        $tmpl =~ s/%FORCENEWREVISIONCHECKBOX%/checked="checked"/go;
 
87
    }
 
88
    else {
 
89
        $tmpl =~ s/%FORCENEWREVISIONCHECKBOX%//go;
 
90
    }
 
91
    my $saveCmd = $query->param('cmd') || '';
 
92
    $tmpl =~ s/%CMD%/$saveCmd/go;
 
93
 
 
94
    my $redirectTo = $query->param('redirectto') || '';
 
95
    $tmpl =~ s/%REDIRECTTO%/$redirectTo/go;
 
96
 
 
97
    $formName ||= '';
 
98
    $tmpl =~ s/%FORMTEMPLATE%/$formName/g;
 
99
 
 
100
    my $parent = $meta->get('TOPICPARENT');
 
101
    $parent = $parent->{name} if ($parent);
 
102
    $parent ||= '';
 
103
    $tmpl =~ s/%TOPICPARENT%/$parent/g;
 
104
 
 
105
    $session->enterContext( 'can_render_meta', $meta );
 
106
 
 
107
    my $displayText = $content;
 
108
    $displayText =
 
109
      $session->handleCommonTags( $displayText, $web, $topic, $meta );
 
110
    $displayText =
 
111
      $session->renderer->getRenderedVersion( $displayText, $web, $topic );
 
112
 
 
113
    # Disable links and inputs in the text
 
114
    $displayText =~
 
115
      s#<a\s[^>]*>(.*?)</a>#<span class="foswikiEmulatedLink">$1</span>#gis;
 
116
    $displayText =~ s/<(input|button|textarea) /<$1 disabled="disabled" /gis;
 
117
    $displayText =~ s(</?form(|\s.*?)>)()gis;
 
118
    $displayText =~ s/(<[^>]*\bon[A-Za-z]+=)('[^']*'|"[^"]*")/$1''/gis;
 
119
 
 
120
    # let templates know the context so they can act on it
 
121
    $session->enterContext( 'preview', 1 );
 
122
 
 
123
    # note: preventing linkage in rendered form can only happen in templates
 
124
    # see formtables.tmpl
 
125
 
 
126
    $tmpl = $session->handleCommonTags( $tmpl, $web, $topic, $meta );
 
127
    $tmpl = $session->renderer->getRenderedVersion( $tmpl, $web, $topic );
 
128
    $tmpl =~ s/%TEXT%/$displayText/go;
 
129
 
 
130
    # write the hidden form fields
 
131
    $tmpl =~ s/%FORMFIELDS%/$formFields/go;
 
132
 
 
133
    # SMELL: this should be done using CGI::hidden
 
134
    $text = Foswiki::entityEncode( $text, "\n" );
 
135
 
 
136
    $tmpl =~ s/%HIDDENTEXT%/$text/go;
 
137
 
 
138
    $tmpl =~ s/<\/?(nop|noautolink)\/?>//gis;
 
139
 
 
140
    # I don't know _where_ these should be done,
 
141
    # so I'll do them as late as possible
 
142
    my $originalrev = $query->param('originalrev');    # rev edit started on
 
143
         #ASSERT($originalrev ne '%ORIGINALREV%') if DEBUG;
 
144
    $tmpl =~ s/%ORIGINALREV%/$originalrev/go;
 
145
    my $templatetopic = $query->param('templatetopic');
 
146
 
 
147
    #ASSERT($templatetopic ne '%TEMPLATETOPIC%') if DEBUG;
 
148
    $tmpl =~ s/%TEMPLATETOPIC%/$templatetopic/go;
 
149
 
 
150
    #this one's worrying, its special, and not set much at all
 
151
    #$tmpl =~ s/%SETTINGSTOPIC%/$settingstopic/go;
 
152
    my $newtopic = $query->param('newtopic');
 
153
 
 
154
    #ASSERT($newtopic ne '%NEWTOPIC%') if DEBUG;
 
155
    $tmpl =~ s/%NEWTOPIC%/$newtopic/go;
 
156
 
 
157
    $session->writeCompletePage($tmpl);
 
158
}
 
159
 
 
160
1;
 
161
__DATA__
 
162
# Module of Foswiki - The Free and Open Source Wiki, http://foswiki.org/
 
163
#
 
164
# Copyright (C) 2008-2009 Foswiki Contributors. Foswiki Contributors
 
165
# are listed in the AUTHORS file in the root of this distribution.
 
166
# NOTE: Please extend that file, not this notice.
 
167
#
 
168
# Additional copyrights apply to some or all of the code in this
 
169
# file as follows:
 
170
#
 
171
# Copyright (C) 1999-2007 Peter Thoeny, peter@thoeny.org
 
172
# and TWiki Contributors. All Rights Reserved. TWiki Contributors
 
173
# are listed in the AUTHORS file in the root of this distribution.
 
174
#
 
175
# This program is free software; you can redistribute it and/or
 
176
# modify it under the terms of the GNU General Public License
 
177
# as published by the Free Software Foundation; either version 2
 
178
# of the License, or (at your option) any later version. For
 
179
# more details read LICENSE in the root of this distribution.
 
180
#
 
181
# This program is distributed in the hope that it will be useful,
 
182
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
183
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 
184
#
 
185
# As per the GPL, removal of this notice is prohibited.