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 |
|
$c->stash->{databases} = $c->comp('Model::Databases')->list_inputs; |
45 |
|
|
46 |
$c->stash->{template} = 'editor.tt'; |
$c->stash->{template} = 'editor.tt'; |
47 |
} |
} |
48 |
|
|
68 |
my $template_full_path = "$template_path/$template_filename"; |
my $template_full_path = "$template_path/$template_filename"; |
69 |
|
|
70 |
if ($c->req->params->{save_template}) { |
if ($c->req->params->{save_template}) { |
71 |
|
|
72 |
|
$c->response->content_type('text/html; charset=utf-8'); |
73 |
|
|
74 |
my $t = $c->req->params->{template_content}; |
my $t = $c->req->params->{template_content}; |
75 |
my $size = length($t); |
my $size = length($t); |
76 |
$c->log->debug("saving $template_full_path, $size bytes"); |
$c->log->debug("saving $template_full_path, $size bytes"); |
77 |
if ($webpac->save_html( $template_full_path, $t )) { |
if ($webpac->save_html( $template_full_path, $t )) { |
78 |
$c->res->output("<span>saved $template_filename, $size bytes</span>"); |
$c->res->output("<span>saved $template_filename, $size bytes</span>"); |
79 |
} else { |
} else { |
80 |
$c->res->output("<span>error saving $template_filename!</span>"); |
$c->res->output("<span class=\"error\">error saving $template_filename!</span>"); |
81 |
} |
} |
82 |
return; |
return; |
83 |
} |
} |
86 |
|
|
87 |
if (! -r $template_full_path) { |
if (! -r $template_full_path) { |
88 |
$c->log->warn("can't find '$template_full_path': $!"); |
$c->log->warn("can't find '$template_full_path': $!"); |
89 |
|
$c->response->content_type('text/html; charset=utf-8'); |
90 |
$c->res->output("can't find template '$template_full_path'"); |
$c->res->output("can't find template '$template_full_path'"); |
91 |
return; |
return; |
92 |
} |
} |
124 |
my $css_full_path = "$css_path/$css_filename"; |
my $css_full_path = "$css_path/$css_filename"; |
125 |
|
|
126 |
if ($c->req->params->{save_css}) { |
if ($c->req->params->{save_css}) { |
127 |
|
|
128 |
|
$c->response->content_type('text/html; charset=utf-8'); |
129 |
|
|
130 |
my $t = $c->req->params->{css_content}; |
my $t = $c->req->params->{css_content}; |
131 |
my $size = length($t); |
my $size = length($t); |
132 |
$c->log->debug("saving $css_full_path, $size bytes"); |
$c->log->debug("saving $css_full_path, $size bytes"); |
133 |
if ($webpac->save_html( $css_full_path, $t )) { |
if ($webpac->save_html( $css_full_path, $t )) { |
134 |
$c->res->output("<span>saved $css_filename, $size bytes</span>"); |
$c->res->output("<span>saved $css_filename, $size bytes</span>"); |
135 |
} else { |
} else { |
136 |
$c->res->output("<span>error saving $css_filename!</span>"); |
$c->res->output("<span class=\"error\">error saving $css_filename!</span>"); |
137 |
} |
} |
138 |
return; |
return; |
139 |
} |
} |
142 |
|
|
143 |
$c->stash->{'css_content'} = $webpac->load_html( $css_full_path ); |
$c->stash->{'css_content'} = $webpac->load_html( $css_full_path ); |
144 |
|
|
145 |
|
$c->stash->{'css_list'} = [ qw/user.css foobar.css/ ]; |
146 |
|
|
147 |
$c->stash->{template} = 'editor/css.tt'; |
$c->stash->{template} = 'editor/css.tt'; |
148 |
} |
} |
149 |
|
|
158 |
|
|
159 |
$c->log->debug('record params '.Dumper($c->req->params)); |
$c->log->debug('record params '.Dumper($c->req->params)); |
160 |
|
|
161 |
my $mfn = $c->req->params->{mfn}; |
my $record_uri = $c->req->params->{record_uri}; |
162 |
|
$record_uri ||= $c->config->{editor}->{default_record_uri} and |
163 |
if (! $mfn) { |
$c->log->warn("using editor: default_record_uri"); |
164 |
$c->log->warn("no mfn, using 1"); |
|
165 |
$mfn = 1; |
if (! $record_uri) { |
166 |
|
$c->log->fatal("record called without record_uri and default_record_uri not found in config under editor"); |
167 |
|
$c->stash->{error} = 'record retrival failed'; |
168 |
|
$c->stash->{template} = 'error.tt'; |
169 |
|
return; |
170 |
} |
} |
171 |
|
|
172 |
my $template_filename = $c->req->params->{template_filename}; |
my $template_filename = $c->req->params->{template_filename}; |
177 |
|
|
178 |
my $webpac = $c->comp('Model::WebPAC'); |
my $webpac = $c->comp('Model::WebPAC'); |
179 |
|
|
180 |
$c->res->output( |
my $html = $webpac->record( record_uri => $record_uri, template => $template_filename ); |
181 |
$webpac->record( mfn => $mfn, template => $template_filename ) |
|
182 |
); |
if ($html) { |
183 |
|
$c->log->debug('check html with tidy'); |
184 |
|
my $tidy = new HTML::Tidy; |
185 |
|
$tidy->ignore( text => [ |
186 |
|
qr/DOCTYPE/, qr/unsupported/, qr/proprietary/i, |
187 |
|
qr/invalid character code/, |
188 |
|
qr/inserting missing 'title' element/, |
189 |
|
qr/lacks "summary" attribute/, |
190 |
|
] ); |
191 |
|
|
192 |
|
my @lines = split( "\n", $html ); |
193 |
|
$_ = "$_\n" for @lines; |
194 |
|
|
195 |
|
if ( $tidy->parse('tidy', @lines) ) { |
196 |
|
if ( $tidy->messages ) { |
197 |
|
$html .= <<__TIDY_CLOSE__; |
198 |
|
<div id="tidy_show" class="notice" style="display: none;" onclick="Element.hide('tidy_show'); Element.show('tidy');">?</div> |
199 |
|
<div id="tidy" class="tidy"> |
200 |
|
<a href="#" onclick="Element.hide('tidy'); Element.show('tidy_show'); return false;">close</a> |
201 |
|
HTML Tidy output: |
202 |
|
<br/> |
203 |
|
__TIDY_CLOSE__ |
204 |
|
# Escape <, >, & and ", and to produce valid XML |
205 |
|
my %escape = ('<'=>'<', '>'=>'>', '&'=>'&', '"'=>'"'); |
206 |
|
my $escape_re = join '|' => keys %escape; |
207 |
|
|
208 |
|
foreach my $m ( $tidy->messages ) { |
209 |
|
my $c = $lines[ ( $m->line - 1 ) ]; |
210 |
|
my $txt = $m->as_string; |
211 |
|
$c =~ s/($escape_re)/$escape{$1}/g; |
212 |
|
$txt =~ s/($escape_re)/$escape{$1}/g; |
213 |
|
my $class = ''; |
214 |
|
$class = ' class="tidy_'.lc($1).'"' if ($txt =~ /(Warning|Error):/s); |
215 |
|
$html .= 'line ' . $m->line . ":<span${class}>$txt<tt>$c</tt></span>"; |
216 |
|
} |
217 |
|
$html .= '</div>'; |
218 |
|
} |
219 |
|
} else { |
220 |
|
$html .= qq{<div class="tidy tidy_error">Can't parse this record with HTML Tidy!</div>}; |
221 |
|
} |
222 |
|
} else { |
223 |
|
$html .= qq{<div class="no_results">Can't find record</div>}; |
224 |
|
} |
225 |
|
|
226 |
|
$c->response->content_type('text/html; charset=utf-8'); |
227 |
|
$c->response->body( $html ); |
228 |
} |
} |
229 |
|
|
230 |
|
|