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