--- Webpacus/lib/Webpacus/Controller/Editor.pm 2005/11/26 21:11:41 167 +++ Webpacus/lib/Webpacus/Controller/Editor.pm 2005/12/17 03:20:14 274 @@ -4,6 +4,7 @@ use warnings; use base 'Catalyst::Controller'; +use HTML::Tidy; use Data::Dumper; =head1 NAME @@ -64,10 +65,26 @@ my $template_full_path = "$template_path/$template_filename"; + if ($c->req->params->{save_template}) { + + $c->response->content_type('text/html; charset=utf-8'); + + my $t = $c->req->params->{template_content}; + my $size = length($t); + $c->log->debug("saving $template_full_path, $size bytes"); + if ($webpac->save_html( $template_full_path, $t )) { + $c->res->output("saved $template_filename, $size bytes"); + } else { + $c->res->output("error saving $template_filename!"); + } + return; + } + $c->log->debug("loading $template_full_path"); if (! -r $template_full_path) { $c->log->warn("can't find '$template_full_path': $!"); + $c->response->content_type('text/html; charset=utf-8'); $c->res->output("can't find template '$template_full_path'"); return; } @@ -98,16 +115,33 @@ sub css : Local { my ( $self, $c ) = @_; - my $css_file = $c->config->{webpac}->{default_css} || die("need default_css"); + my $css_filename = $c->config->{webpac}->{default_css} || die("need default_css"); my $css_path = $c->config->{webpac}->{css_path} || die("need css_path"); my $webpac = $c->comp('Model::WebPAC'); - my $css_full_path = "$css_path/$css_file"; + my $css_full_path = "$css_path/$css_filename"; + + if ($c->req->params->{save_css}) { + + $c->response->content_type('text/html; charset=utf-8'); + + my $t = $c->req->params->{css_content}; + my $size = length($t); + $c->log->debug("saving $css_full_path, $size bytes"); + if ($webpac->save_html( $css_full_path, $t )) { + $c->res->output("saved $css_filename, $size bytes"); + } else { + $c->res->output("error saving $css_filename!"); + } + return; + } $c->log->debug("loading $css_full_path"); $c->stash->{'css_content'} = $webpac->load_html( $css_full_path ); + $c->stash->{'css_list'} = [ qw/user.css foobar.css/ ]; + $c->stash->{template} = 'editor/css.tt'; } @@ -122,11 +156,15 @@ $c->log->debug('record params '.Dumper($c->req->params)); - my $mfn = $c->req->params->{mfn}; - - if (! $mfn) { - $c->log->warn("no mfn, using 1"); - $mfn = 1; + my $record_uri = $c->req->params->{record_uri}; + $record_uri ||= $c->config->{editor}->{default_record_uri} and + $c->log->warn("using editor: default_record_uri"); + + if (! $record_uri) { + $c->log->fatal("record called without record_uri and default_record_uri not found in config under editor"); + $c->stash->{error} = 'record retrival failed'; + $c->stash->{template} = 'error.tt'; + return; } my $template_filename = $c->req->params->{template_filename}; @@ -137,9 +175,54 @@ my $webpac = $c->comp('Model::WebPAC'); - $c->res->output( - $webpac->record( mfn => $mfn, template => $template_filename ) - ); + my $html = $webpac->record( record_uri => $record_uri, template => $template_filename ); + + if ($html) { + $c->log->debug('check html with tidy'); + my $tidy = new HTML::Tidy; + $tidy->ignore( text => [ + qr/DOCTYPE/, qr/unsupported/, qr/proprietary/i, + qr/invalid character code/, + qr/inserting missing 'title' element/, + qr/lacks "summary" attribute/, + ] ); + + my @lines = split( "\n", $html ); + $_ = "$_\n" for @lines; + + if ( $tidy->parse('tidy', @lines) ) { + if ( $tidy->messages ) { + $html .= <<__TIDY_CLOSE__; + +
+close +HTML Tidy output: +
+__TIDY_CLOSE__ + # Escape <, >, & and ", and to produce valid XML + my %escape = ('<'=>'<', '>'=>'>', '&'=>'&', '"'=>'"'); + my $escape_re = join '|' => keys %escape; + + foreach my $m ( $tidy->messages ) { + my $c = $lines[ ( $m->line - 1 ) ]; + my $txt = $m->as_string; + $c =~ s/($escape_re)/$escape{$1}/g; + $txt =~ s/($escape_re)/$escape{$1}/g; + my $class = ''; + $class = ' class="tidy_'.lc($1).'"' if ($txt =~ /(Warning|Error):/s); + $html .= 'line ' . $m->line . ":$txt$c"; + } + $html .= '
'; + } + } else { + $html .= qq{
Can't parse this record with HTML Tidy!
}; + } + } else { + $html .= qq{
Can't find record
}; + } + + $c->response->content_type('text/html; charset=utf-8'); + $c->response->body( $html ); }