/[webpac2]/Webpacus/lib/Webpacus/Controller/Editor.pm
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Diff of /Webpacus/lib/Webpacus/Controller/Editor.pm

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

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

Legend:
Removed from v.165  
changed lines
  Added in v.400

  ViewVC Help
Powered by ViewVC 1.1.26