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

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

  ViewVC Help
Powered by ViewVC 1.1.26