/[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

Annotation of /Webpacus/lib/Webpacus/Controller/Editor.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 243 - (hide annotations)
Wed Dec 14 23:04:05 2005 UTC (18 years, 5 months ago) by dpavlin
File size: 6064 byte(s)
 r11694@llin:  dpavlin | 2005-12-14 23:39:15 +0100
 nicer errors, editor in progress

1 dpavlin 134 package Webpacus::Controller::Editor;
2    
3     use strict;
4     use warnings;
5     use base 'Catalyst::Controller';
6    
7 dpavlin 180 use HTML::Tidy;
8 dpavlin 165 use Data::Dumper;
9    
10 dpavlin 134 =head1 NAME
11    
12 dpavlin 165 Webpacus::Controller::Editor - AJAX template and CSS editor
13 dpavlin 134
14     =head1 SYNOPSIS
15    
16 dpavlin 165 See L<Webpacus>, L<WebPAC>
17 dpavlin 134
18     =head1 DESCRIPTION
19    
20     Catalyst Controller.
21    
22     =head1 METHODS
23    
24     =over 4
25    
26     =item default
27    
28     =cut
29    
30     sub default : Private {
31 dpavlin 165 my ( $self, $c ) = @_;
32 dpavlin 134
33 dpavlin 165 my $webpac = $c->comp('Model::WebPAC');
34     my $params = $c->req->params;
35     my $log = $c->log;
36 dpavlin 134
37 dpavlin 165 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     $c->stash->{template} = 'editor.tt';
45 dpavlin 134 }
46    
47 dpavlin 165 =item template
48    
49     fetch template using C<template_filename> in request or
50     C<< webpac->default_template >>.
51    
52     =cut
53    
54 dpavlin 134 sub template : Local {
55 dpavlin 135 my ( $self, $c ) = @_;
56 dpavlin 134
57 dpavlin 167 my $template_filename =
58 dpavlin 165 $c->req->params->{template_filename} ||
59     $c->config->{webpac}->{default_template} ||
60     $c->log->fatal("need default_template");
61 dpavlin 135
62     my $webpac = $c->comp('Model::WebPAC');
63    
64 dpavlin 167 my $template_path = $webpac->{template_path} || $c->log->fatal("can't find template_path in Model::WebPAC");
65 dpavlin 135
66 dpavlin 167 my $template_full_path = "$template_path/$template_filename";
67    
68 dpavlin 172 if ($c->req->params->{save_template}) {
69 dpavlin 179
70     $c->response->content_type('text/html; charset=utf-8');
71    
72 dpavlin 172 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 dpavlin 243 $c->res->output("<span class=\"error\">error saving $template_filename!</span>");
79 dpavlin 172 }
80     return;
81     }
82    
83 dpavlin 165 $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 dpavlin 179 $c->response->content_type('text/html; charset=utf-8');
88 dpavlin 165 $c->res->output("can't find template '$template_full_path'");
89     return;
90     }
91    
92 dpavlin 167 # load template html
93 dpavlin 135 $c->stash->{'template_content'} =
94     $webpac->load_html( $template_full_path );
95    
96 dpavlin 167 # 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 dpavlin 135 $c->stash->{template} = 'editor/template.tt';
105 dpavlin 134 }
106    
107 dpavlin 165 =item css
108    
109 dpavlin 167 Return css.
110    
111     B<Not finished>
112    
113 dpavlin 165 =cut
114    
115 dpavlin 134 sub css : Local {
116 dpavlin 135 my ( $self, $c ) = @_;
117 dpavlin 134
118 dpavlin 172 my $css_filename = $c->config->{webpac}->{default_css} || die("need default_css");
119 dpavlin 135 my $css_path = $c->config->{webpac}->{css_path} || die("need css_path");
120     my $webpac = $c->comp('Model::WebPAC');
121 dpavlin 165
122 dpavlin 172 my $css_full_path = "$css_path/$css_filename";
123 dpavlin 165
124 dpavlin 172 if ($c->req->params->{save_css}) {
125 dpavlin 179
126     $c->response->content_type('text/html; charset=utf-8');
127    
128 dpavlin 172 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 dpavlin 243 $c->res->output("<span class=\"error\">error saving $css_filename!</span>");
135 dpavlin 172 }
136     return;
137     }
138    
139 dpavlin 165 $c->log->debug("loading $css_full_path");
140    
141     $c->stash->{'css_content'} = $webpac->load_html( $css_full_path );
142    
143 dpavlin 189 $c->stash->{'css_list'} = [ qw/user.css foobar.css/ ];
144    
145 dpavlin 135 $c->stash->{template} = 'editor/css.tt';
146 dpavlin 134 }
147    
148 dpavlin 165 =item record
149    
150 dpavlin 167 Return html of record taking C<mfn> and C<template_filename> from request.
151    
152 dpavlin 165 =cut
153    
154     sub record : Local {
155     my ( $self, $c ) = @_;
156    
157     $c->log->debug('record params '.Dumper($c->req->params));
158    
159 dpavlin 242 my $record_uri = $c->req->params->{record_uri};
160 dpavlin 165
161 dpavlin 242 if (! $record_uri) {
162     $c->log->fatal("no record_uri");
163 dpavlin 243 $c->stash->{error} = 'record retrival failed';
164     $c->stash->{template} = 'error.tt';
165 dpavlin 242 return;
166 dpavlin 165 }
167    
168     my $template_filename = $c->req->params->{template_filename};
169     if (! $template_filename) {
170     $template_filename = $c->config->{webpac}->{default_template} || $c->log->fatal("no webpac->default_template in configuration!");
171     $c->log->warn("no template_filename using $template_filename");
172     }
173    
174     my $webpac = $c->comp('Model::WebPAC');
175    
176 dpavlin 242 my $html = $webpac->record( record_uri => $record_uri, template => $template_filename );
177 dpavlin 180
178     if ($html) {
179     $c->log->debug('check html with tidy');
180     my $tidy = new HTML::Tidy;
181     $tidy->ignore( text => [
182     qr/DOCTYPE/, qr/unsupported/, qr/proprietary/i,
183     qr/invalid character code/,
184     qr/inserting missing 'title' element/,
185     qr/lacks "summary" attribute/,
186     ] );
187    
188 dpavlin 181 my @lines = split( "\n", $html );
189     $_ = "$_\n" for @lines;
190 dpavlin 180
191 dpavlin 181 if ( $tidy->parse('tidy', @lines) ) {
192     if ( $tidy->messages ) {
193     $html .= <<__TIDY_CLOSE__;
194     <div id="tidy_show" class="notice" style="display: none;" onclick="Element.hide('tidy_show'); Element.show('tidy');">?</div>
195     <div id="tidy" class="tidy">
196     <a href="#" onclick="Element.hide('tidy'); Element.show('tidy_show'); return false;">close</a>
197     HTML Tidy output:
198     <br/>
199     __TIDY_CLOSE__
200 dpavlin 180 # Escape <, >, & and ", and to produce valid XML
201     my %escape = ('<'=>'&lt;', '>'=>'&gt;', '&'=>'&amp;', '"'=>'&quot;');
202     my $escape_re = join '|' => keys %escape;
203    
204     foreach my $m ( $tidy->messages ) {
205 dpavlin 181 my $c = $lines[ ( $m->line - 1 ) ];
206 dpavlin 180 my $txt = $m->as_string;
207     $c =~ s/($escape_re)/$escape{$1}/g;
208     $txt =~ s/($escape_re)/$escape{$1}/g;
209     my $class = '';
210     $class = ' class="tidy_'.lc($1).'"' if ($txt =~ /(Warning|Error):/s);
211 dpavlin 181 $html .= 'line ' . $m->line . ":<span${class}>$txt<tt>$c</tt></span>";
212 dpavlin 180 }
213     $html .= '</div>';
214     }
215     } else {
216     $html .= qq{<div class="tidy tidy_error">Can't parse this record with HTML Tidy!</div>};
217     }
218 dpavlin 241 } else {
219     $html .= qq{<div class="no_results">Can't find record</div>};
220 dpavlin 180 }
221 dpavlin 242
222 dpavlin 179 $c->response->content_type('text/html; charset=utf-8');
223 dpavlin 237 $c->response->body( $html );
224 dpavlin 165 }
225    
226 dpavlin 167
227    
228 dpavlin 134 =back
229    
230     =head1 AUTHOR
231    
232 dpavlin 165 Dobrica Pavlinusic, C<< <dpavlin@rot13.org> >>
233 dpavlin 134
234     =head1 LICENSE
235    
236     This library is free software, you can redistribute it and/or modify
237     it under the same terms as Perl itself.
238    
239     =cut
240    
241     1;

  ViewVC Help
Powered by ViewVC 1.1.26