/[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 135 by dpavlin, Thu Nov 24 22:29:29 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;
9    
10  =head1 NAME  =head1 NAME
11    
12  Webpacus::Controller::Editor - Catalyst Controller  Webpacus::Controller::Editor - AJAX template and CSS editor
13    
14  =head1 SYNOPSIS  =head1 SYNOPSIS
15    
16  See L<Webpacus>  See L<Webpacus>, L<WebPAC>
17    
18  =head1 DESCRIPTION  =head1 DESCRIPTION
19    
# Line 25  Catalyst Controller. Line 28  Catalyst Controller.
28  =cut  =cut
29    
30  sub default : Private {  sub default : Private {
31      my ( $self, $c ) = @_;          my ( $self, $c ) = @_;
32    
33            my $webpac = $c->comp('Model::WebPAC');
34            my $params = $c->req->params;
35            my $log = $c->log;
36    
37            my $template_filename =
38                    $c->req->params->{template_filename} ||
39                    $c->config->{webpac}->{default_template} ||
40                    $c->log->fatal("need default_template");
41    
42            $c->stash->{template_filename} = $template_filename;
43    
44      my $webpac = $c->comp('Model::WebPAC');          $c->stash->{databases} = $c->comp('Model::Databases')->list_inputs;
     my $params = $c->req->params;  
     my $log = $c->log;  
45    
46      $c->stash->{template} = 'editor.tt';          $c->stash->{template} = 'editor.tt';
47  }  }
48    
49    =item template
50    
51    fetch template using C<template_filename> in request or
52    C<< webpac->default_template >>.
53    
54    =cut
55    
56  sub template : Local {  sub template : Local {
57          my ( $self, $c ) = @_;          my ( $self, $c ) = @_;
58    
59          my $template_file = $c->config->{webpac}->{default_template} || die("need default_template");          my $template_filename =
60                    $c->req->params->{template_filename} ||
61                    $c->config->{webpac}->{default_template} ||
62                    $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");
86    
87            if (! -r $template_full_path) {
88                    $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'");
91                    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
110    
111    Return css.
112    
113    B<Not finished>
114    
115    =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          $c->stash->{'css_content'} = $webpac->load_html( "$css_path/$css_file" );  
124            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");
142    
143            $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  =back  =item record
151    
152    Return html of record taking C<mfn> and C<template_filename> from request.
153    
154    =cut
155    
156    sub record : Local {
157            my ( $self, $c ) = @_;
158    
159            $c->log->debug('record params '.Dumper($c->req->params));
160    
161            my $record_uri = $c->req->params->{record_uri};
162            $record_uri ||= $c->config->{editor}->{default_record_uri} and
163                    $c->log->warn("using editor: default_record_uri");
164    
165            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};
173            if (! $template_filename) {
174                    $template_filename = $c->config->{webpac}->{default_template} || $c->log->fatal("no webpac->default_template in configuration!");
175                    $c->log->warn("no template_filename using $template_filename");
176            }
177    
178            my $webpac = $c->comp('Model::WebPAC');
179    
180            my $html = $webpac->record( record_uri => $record_uri, template => $template_filename );
181    
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    
230    
231    
232    =back
233    
234  =head1 AUTHOR  =head1 AUTHOR
235    
236  Dobrica Pavlinusic,,,  Dobrica Pavlinusic, C<< <dpavlin@rot13.org> >>
237    
238  =head1 LICENSE  =head1 LICENSE
239    

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

  ViewVC Help
Powered by ViewVC 1.1.26