--- Webpacus/lib/Webpacus/Controller/Editor.pm 2005/11/24 16:30:16 134 +++ Webpacus/lib/Webpacus/Controller/Editor.pm 2005/11/29 13:02:53 189 @@ -4,13 +4,16 @@ use warnings; use base 'Catalyst::Controller'; +use HTML::Tidy; +use Data::Dumper; + =head1 NAME -Webpacus::Controller::Editor - Catalyst Controller +Webpacus::Controller::Editor - AJAX template and CSS editor =head1 SYNOPSIS -See L +See L, L =head1 DESCRIPTION @@ -25,33 +28,205 @@ =cut sub default : Private { - my ( $self, $c ) = @_; + my ( $self, $c ) = @_; + + my $webpac = $c->comp('Model::WebPAC'); + my $params = $c->req->params; + my $log = $c->log; + + my $template_filename = + $c->req->params->{template_filename} || + $c->config->{webpac}->{default_template} || + $c->log->fatal("need default_template"); - my $webpac = $c->comp('Model::WebPAC'); - my $params = $c->req->params; - my $log = $c->log; + $c->stash->{template_filename} = $template_filename; - $c->stash->{template} = 'editor.tt'; + $c->stash->{template} = 'editor.tt'; } +=item template + +fetch template using C in request or +C<< webpac->default_template >>. + +=cut + sub template : Local { - my ( $self, $c ) = @_; + my ( $self, $c ) = @_; + + my $template_filename = + $c->req->params->{template_filename} || + $c->config->{webpac}->{default_template} || + $c->log->fatal("need default_template"); + + my $webpac = $c->comp('Model::WebPAC'); + + my $template_path = $webpac->{template_path} || $c->log->fatal("can't find template_path in Model::WebPAC"); + + my $template_full_path = "$template_path/$template_filename"; + + if ($c->req->params->{save_template}) { - $c->stash->{template} = 'editor/template.tt'; + $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; + } + + # load template html + $c->stash->{'template_content'} = + $webpac->load_html( $template_full_path ); + + # load template list + + opendir(my $d, $template_path) || $c->log->fatal("can't opendir '$template_path': $!"); + my @templates = grep { /\.tt$/i } readdir($d); + + $c->stash->{template_list} = \@templates; + $c->stash->{template_filename} = $template_filename; + + $c->stash->{template} = 'editor/template.tt'; } +=item css + +Return css. + +B + +=cut + sub css : Local { - my ( $self, $c ) = @_; + my ( $self, $c ) = @_; + + 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_filename"; + + if ($c->req->params->{save_css}) { + + $c->response->content_type('text/html; charset=utf-8'); - $c->stash->{template} = 'editor/css.tt'; + 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'; } -=back +=item record +Return html of record taking C and C from request. + +=cut + +sub record : Local { + my ( $self, $c ) = @_; + + $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 $template_filename = $c->req->params->{template_filename}; + if (! $template_filename) { + $template_filename = $c->config->{webpac}->{default_template} || $c->log->fatal("no webpac->default_template in configuration!"); + $c->log->warn("no template_filename using $template_filename"); + } + + my $webpac = $c->comp('Model::WebPAC'); + + my $html = $webpac->record( mfn => $mfn, 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!
}; + } + } + + + $c->response->content_type('text/html; charset=utf-8'); + $c->res->output( $html ); +} + + + +=back =head1 AUTHOR -Dobrica Pavlinusic,,, +Dobrica Pavlinusic, C<< >> =head1 LICENSE