/[webpac2]/Webpacus/lib/Webpacus/Controller/Editor.pm
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Contents of /Webpacus/lib/Webpacus/Controller/Editor.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 258 - (show annotations)
Fri Dec 16 13:20:51 2005 UTC (18 years, 5 months ago) by dpavlin
File size: 6876 byte(s)
 r11723@llin:  dpavlin | 2005-12-16 17:06:57 +0100
 skip databases without inputs defined

1 package Webpacus::Controller::Editor;
2
3 use strict;
4 use warnings;
5 use base 'Catalyst::Controller';
6
7 use HTML::Tidy;
8 use Data::Dumper;
9
10 =head1 NAME
11
12 Webpacus::Controller::Editor - AJAX template and CSS editor
13
14 =head1 SYNOPSIS
15
16 See L<Webpacus>, L<WebPAC>
17
18 =head1 DESCRIPTION
19
20 Catalyst Controller.
21
22 =head1 METHODS
23
24 =over 4
25
26 =item default
27
28 =cut
29
30 sub default : Private {
31 my ( $self, $c ) = @_;
32
33 my $webpac = $c->comp('Model::WebPAC');
34 my $params = $c->req->params;
35 my $log = $c->log;
36
37 my $template_filename =
38 $c->req->params->{template_filename} ||
39 $c->config->{webpac}->{default_template} ||
40 $c->log->fatal("need default_template");
41
42 $c->stash->{template_filename} = $template_filename;
43
44 # get databases from config
45 my @databases;
46 my $config_dbs = $c->config->{databases};
47
48 if (! $config_dbs) {
49 $c->log->fail("didn't find databases in config");
50 } else {
51 # convert to resonable format for TT
52 foreach my $db (keys %{ $config_dbs }) {
53 my $d = $config_dbs->{$db} || die;
54 my $inputs = $d->{input} || next;
55 $inputs = [ $inputs ] if (ref($inputs) ne 'ARRAY');
56
57 foreach my $i ( @{ $inputs } ) {
58 my $el = {
59 input => $i->{name},
60 name => $d->{name} || $d,
61 prefix => $db . '/' . $i->{name},
62 };
63 push @databases, $el;
64 }
65 }
66 }
67
68 $c->stash->{databases} = \@databases;
69
70 $c->stash->{template} = 'editor.tt';
71 }
72
73 =item template
74
75 fetch template using C<template_filename> in request or
76 C<< webpac->default_template >>.
77
78 =cut
79
80 sub template : Local {
81 my ( $self, $c ) = @_;
82
83 my $template_filename =
84 $c->req->params->{template_filename} ||
85 $c->config->{webpac}->{default_template} ||
86 $c->log->fatal("need default_template");
87
88 my $webpac = $c->comp('Model::WebPAC');
89
90 my $template_path = $webpac->{template_path} || $c->log->fatal("can't find template_path in Model::WebPAC");
91
92 my $template_full_path = "$template_path/$template_filename";
93
94 if ($c->req->params->{save_template}) {
95
96 $c->response->content_type('text/html; charset=utf-8');
97
98 my $t = $c->req->params->{template_content};
99 my $size = length($t);
100 $c->log->debug("saving $template_full_path, $size bytes");
101 if ($webpac->save_html( $template_full_path, $t )) {
102 $c->res->output("<span>saved $template_filename, $size bytes</span>");
103 } else {
104 $c->res->output("<span class=\"error\">error saving $template_filename!</span>");
105 }
106 return;
107 }
108
109 $c->log->debug("loading $template_full_path");
110
111 if (! -r $template_full_path) {
112 $c->log->warn("can't find '$template_full_path': $!");
113 $c->response->content_type('text/html; charset=utf-8');
114 $c->res->output("can't find template '$template_full_path'");
115 return;
116 }
117
118 # load template html
119 $c->stash->{'template_content'} =
120 $webpac->load_html( $template_full_path );
121
122 # load template list
123
124 opendir(my $d, $template_path) || $c->log->fatal("can't opendir '$template_path': $!");
125 my @templates = grep { /\.tt$/i } readdir($d);
126
127 $c->stash->{template_list} = \@templates;
128 $c->stash->{template_filename} = $template_filename;
129
130 $c->stash->{template} = 'editor/template.tt';
131 }
132
133 =item css
134
135 Return css.
136
137 B<Not finished>
138
139 =cut
140
141 sub css : Local {
142 my ( $self, $c ) = @_;
143
144 my $css_filename = $c->config->{webpac}->{default_css} || die("need default_css");
145 my $css_path = $c->config->{webpac}->{css_path} || die("need css_path");
146 my $webpac = $c->comp('Model::WebPAC');
147
148 my $css_full_path = "$css_path/$css_filename";
149
150 if ($c->req->params->{save_css}) {
151
152 $c->response->content_type('text/html; charset=utf-8');
153
154 my $t = $c->req->params->{css_content};
155 my $size = length($t);
156 $c->log->debug("saving $css_full_path, $size bytes");
157 if ($webpac->save_html( $css_full_path, $t )) {
158 $c->res->output("<span>saved $css_filename, $size bytes</span>");
159 } else {
160 $c->res->output("<span class=\"error\">error saving $css_filename!</span>");
161 }
162 return;
163 }
164
165 $c->log->debug("loading $css_full_path");
166
167 $c->stash->{'css_content'} = $webpac->load_html( $css_full_path );
168
169 $c->stash->{'css_list'} = [ qw/user.css foobar.css/ ];
170
171 $c->stash->{template} = 'editor/css.tt';
172 }
173
174 =item record
175
176 Return html of record taking C<mfn> and C<template_filename> from request.
177
178 =cut
179
180 sub record : Local {
181 my ( $self, $c ) = @_;
182
183 $c->log->debug('record params '.Dumper($c->req->params));
184
185 my $record_uri = $c->req->params->{record_uri};
186 $record_uri ||= $c->config->{editor}->{default_record_uri} and
187 $c->log->warn("using editor: default_record_uri");
188
189 if (! $record_uri) {
190 $c->log->fatal("record called without record_uri and default_record_uri not found in config under editor");
191 $c->stash->{error} = 'record retrival failed';
192 $c->stash->{template} = 'error.tt';
193 return;
194 }
195
196 my $template_filename = $c->req->params->{template_filename};
197 if (! $template_filename) {
198 $template_filename = $c->config->{webpac}->{default_template} || $c->log->fatal("no webpac->default_template in configuration!");
199 $c->log->warn("no template_filename using $template_filename");
200 }
201
202 my $webpac = $c->comp('Model::WebPAC');
203
204 my $html = $webpac->record( record_uri => $record_uri, template => $template_filename );
205
206 if ($html) {
207 $c->log->debug('check html with tidy');
208 my $tidy = new HTML::Tidy;
209 $tidy->ignore( text => [
210 qr/DOCTYPE/, qr/unsupported/, qr/proprietary/i,
211 qr/invalid character code/,
212 qr/inserting missing 'title' element/,
213 qr/lacks "summary" attribute/,
214 ] );
215
216 my @lines = split( "\n", $html );
217 $_ = "$_\n" for @lines;
218
219 if ( $tidy->parse('tidy', @lines) ) {
220 if ( $tidy->messages ) {
221 $html .= <<__TIDY_CLOSE__;
222 <div id="tidy_show" class="notice" style="display: none;" onclick="Element.hide('tidy_show'); Element.show('tidy');">?</div>
223 <div id="tidy" class="tidy">
224 <a href="#" onclick="Element.hide('tidy'); Element.show('tidy_show'); return false;">close</a>
225 HTML Tidy output:
226 <br/>
227 __TIDY_CLOSE__
228 # Escape <, >, & and ", and to produce valid XML
229 my %escape = ('<'=>'&lt;', '>'=>'&gt;', '&'=>'&amp;', '"'=>'&quot;');
230 my $escape_re = join '|' => keys %escape;
231
232 foreach my $m ( $tidy->messages ) {
233 my $c = $lines[ ( $m->line - 1 ) ];
234 my $txt = $m->as_string;
235 $c =~ s/($escape_re)/$escape{$1}/g;
236 $txt =~ s/($escape_re)/$escape{$1}/g;
237 my $class = '';
238 $class = ' class="tidy_'.lc($1).'"' if ($txt =~ /(Warning|Error):/s);
239 $html .= 'line ' . $m->line . ":<span${class}>$txt<tt>$c</tt></span>";
240 }
241 $html .= '</div>';
242 }
243 } else {
244 $html .= qq{<div class="tidy tidy_error">Can't parse this record with HTML Tidy!</div>};
245 }
246 } else {
247 $html .= qq{<div class="no_results">Can't find record</div>};
248 }
249
250 $c->response->content_type('text/html; charset=utf-8');
251 $c->response->body( $html );
252 }
253
254
255
256 =back
257
258 =head1 AUTHOR
259
260 Dobrica Pavlinusic, C<< <dpavlin@rot13.org> >>
261
262 =head1 LICENSE
263
264 This library is free software, you can redistribute it and/or modify
265 it under the same terms as Perl itself.
266
267 =cut
268
269 1;

  ViewVC Help
Powered by ViewVC 1.1.26