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

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

  ViewVC Help
Powered by ViewVC 1.1.26