4 |
use warnings; |
use warnings; |
5 |
use base 'Catalyst::Controller'; |
use base 'Catalyst::Controller'; |
6 |
|
|
7 |
|
use HTML::Tidy; |
8 |
use Data::Dumper; |
use Data::Dumper; |
9 |
|
|
10 |
=head1 NAME |
=head1 NAME |
41 |
|
|
42 |
$c->stash->{template_filename} = $template_filename; |
$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'; |
$c->stash->{template} = 'editor.tt'; |
71 |
} |
} |
72 |
|
|
92 |
my $template_full_path = "$template_path/$template_filename"; |
my $template_full_path = "$template_path/$template_filename"; |
93 |
|
|
94 |
if ($c->req->params->{save_template}) { |
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}; |
my $t = $c->req->params->{template_content}; |
99 |
my $size = length($t); |
my $size = length($t); |
100 |
$c->log->debug("saving $template_full_path, $size bytes"); |
$c->log->debug("saving $template_full_path, $size bytes"); |
101 |
if ($webpac->save_html( $template_full_path, $t )) { |
if ($webpac->save_html( $template_full_path, $t )) { |
102 |
$c->res->output("<span>saved $template_filename, $size bytes</span>"); |
$c->res->output("<span>saved $template_filename, $size bytes</span>"); |
103 |
} else { |
} else { |
104 |
$c->res->output("<span>error saving $template_filename!</span>"); |
$c->res->output("<span class=\"error\">error saving $template_filename!</span>"); |
105 |
} |
} |
106 |
return; |
return; |
107 |
} |
} |
110 |
|
|
111 |
if (! -r $template_full_path) { |
if (! -r $template_full_path) { |
112 |
$c->log->warn("can't find '$template_full_path': $!"); |
$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'"); |
$c->res->output("can't find template '$template_full_path'"); |
115 |
return; |
return; |
116 |
} |
} |
148 |
my $css_full_path = "$css_path/$css_filename"; |
my $css_full_path = "$css_path/$css_filename"; |
149 |
|
|
150 |
if ($c->req->params->{save_css}) { |
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}; |
my $t = $c->req->params->{css_content}; |
155 |
my $size = length($t); |
my $size = length($t); |
156 |
$c->log->debug("saving $css_full_path, $size bytes"); |
$c->log->debug("saving $css_full_path, $size bytes"); |
157 |
if ($webpac->save_html( $css_full_path, $t )) { |
if ($webpac->save_html( $css_full_path, $t )) { |
158 |
$c->res->output("<span>saved $css_filename, $size bytes</span>"); |
$c->res->output("<span>saved $css_filename, $size bytes</span>"); |
159 |
} else { |
} else { |
160 |
$c->res->output("<span>error saving $css_filename!</span>"); |
$c->res->output("<span class=\"error\">error saving $css_filename!</span>"); |
161 |
} |
} |
162 |
return; |
return; |
163 |
} |
} |
166 |
|
|
167 |
$c->stash->{'css_content'} = $webpac->load_html( $css_full_path ); |
$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'; |
$c->stash->{template} = 'editor/css.tt'; |
172 |
} |
} |
173 |
|
|
182 |
|
|
183 |
$c->log->debug('record params '.Dumper($c->req->params)); |
$c->log->debug('record params '.Dumper($c->req->params)); |
184 |
|
|
185 |
my $mfn = $c->req->params->{mfn}; |
my $record_uri = $c->req->params->{record_uri}; |
186 |
|
$record_uri ||= $c->config->{editor}->{default_record_uri} and |
187 |
if (! $mfn) { |
$c->log->warn("using editor: default_record_uri"); |
188 |
$c->log->warn("no mfn, using 1"); |
|
189 |
$mfn = 1; |
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}; |
my $template_filename = $c->req->params->{template_filename}; |
201 |
|
|
202 |
my $webpac = $c->comp('Model::WebPAC'); |
my $webpac = $c->comp('Model::WebPAC'); |
203 |
|
|
204 |
$c->res->output( |
my $html = $webpac->record( record_uri => $record_uri, template => $template_filename ); |
205 |
$webpac->record( mfn => $mfn, template => $template_filename ) |
|
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 = ('<'=>'<', '>'=>'>', '&'=>'&', '"'=>'"'); |
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 |
|
|