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

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

  ViewVC Help
Powered by ViewVC 1.1.26