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