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

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

  ViewVC Help
Powered by ViewVC 1.1.26