lib/CatalystX/CMS/File.pm

filter: Statementstmt 76.3% 126 / 165 Branchbran 46.1% 36 / 78 Conditioncond 39.2% 20 / 51 MC/DCmcdc 3.3% 1 / 30 Subroutinesub 89.4% 17 / 19 Podpod 100% 8 / 8 totaltotal 59.2% 208 / 351 CC 63 SCAR 36.3
CC
63
Cov
62%
save
114.3
new
12.2
committed
11.2
CRAP 37.7
« blib/lib/CatalystX/CMS.pm ↑ Summary lib/CatalystX/CMS/Model.pm »
1 package CatalystX::CMS::File;
2 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use base qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
5 SVN::Class::File
6 Path::Class::File::Lockable
7 Class::Accessor::Fast
8 2 );
9 2 ▶use MRO::Compat;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use mro 'c3';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use Data::Dump qw( dump );
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 2 our $VERSION = '0.011';
15  
16 2 __PACKAGE__->mk_accessors(qw( attrs content ext has_unsaved_changes ));
17  
18 =head1 NAME
19  
20 CatalystX::CMS::File - template file class
21  
22 =head1 SYNOPSIS
23  
24 use CatalystX::CMS::File;
25  
26 # longhand
27 my $file = CatalystX::CMS::File->new( $path_to_file );
28 $file->fetch if $page->size;
29 $file->content('hello world');
30 $file->title('super foo');
31 $file->write;
32 $file->commit or die $file->errstr;
33
34 # shorthand
35 my $file = CatalystX::CMS::File->new(
36 path => $path_to_file,
37 title => 'super foo',
38 content => 'hello world',
39 );
40 eval { $file->save } or die $file->errstr;
41 print "$file was saved\n";
42
43 =head1 DESCRIPTION
44  
45 CatalystX::CMS::File is the object model for the Template Toolkit files that
46 make up the content system of your application.
47 Each object represents a C<.tt>
48 file on the filesystem in a Subversion working directory.
49 An object can be read, written, locked and unlocked on the filesystem.
50 As a subclass of SVN::Class::File, the object can be interrogated
51 for its history, added, deleted, committed or updated.
52  
53 =head2 new( I<path> )
54  
55 =head2 new( path => I<path> [, %param ] )
56  
57 Create a new object.
58  
59 Either form of new() may be used, but I<path> is always
60 required.
61  
62 =cut
63  
64 23 ▶sub new {
Subroutine new: called CC=11 SCAR=25.0
Pod documented
65 23 my $class = shift;
66 23 my @param = @_;
67 23 my ( $self, %arg );
68  
69 23 ▶ if ( !@param ) {
Branch: 4 branches
BranchTrueFalseTotal
if (not @param) { } 0 23 23
elsif (@param == 1) { } 5 18 23
elsif (@param % 2) { } 0 18 18
elsif (@param == 2 and $param[0] ne 'path') { } 11 7 18
70 0 croak "path required";
71 }
72 elsif ( @param == 1 ) {
73 5 $self = $class->next::method(@param);
74 }
75 elsif ( @param % 2 ) {
76 0 croak "new() requires either a path or a hash of key/value pairs";
77 }
78 ▶ elsif ( @param == 2 && $param[0] ne 'path' ) {
Condition66%2/3
@param == 2 and $param[0] ne "path"
!ll&&!rl&&r
0711
MC/DC50%1/2
@param == 2 and $param[0] ne "path"
@param == 2 $param[0] ne "path"
Truth table66%2/3
A and B
A= @param == 2B= $param[0] ne "path"
ABresult
0X0
100
111
79 11 $self = $class->next::method(@param);
80 }
81 else {
82 7 %arg = @param;
83 7 ▶ my $path = delete $arg{path} or croak "path required";
Branch: 1 branch
BranchTrueFalseTotal
my $path = delete $arg{"path"} or croak("path required") 7 0 7
84 7 $self = $class->next::method($path);
85 }
86  
87 # add other attributes
88 23 $self->{$_} = $arg{$_} for keys %arg;
89  
90 23 ▶ $self->{attrs} ||= {};
Condition50%1/2
$self->{"attrs"} ||= {}
l!l
023
MC/DC0%0/1
$self->{"attrs"} ||= {}
$self->{"attrs"}
Truth table50%1/2
A
A= $self->{"attrs"}
Aresult
11
00
91  
92 # path cleanup
93 23 ▶ $self->{ext} ||= '.tt';
Condition50%1/2
$self->{"ext"} ||= ".tt"
l!l
023
MC/DC0%0/1
$self->{"ext"} ||= ".tt"
$self->{"ext"}
Truth table50%1/2
A
A= $self->{"ext"}
Aresult
11
00
94  
95 # re-create the object with extension
96 23 my $ext = $self->{ext};
97 23 ▶ unless ( $self =~ m/$ext$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self =~ /$ext$/) 5 18 23
98 5 $self = $self->new( $self . $self->{ext} );
99 }
100  
101 #carp dump $self;
102  
103 23 return $self;
104 }
105  
106 =head2 read
107  
108 Reads the file from disk and parses the metadata attributes and content,
109 thereafter accessible via the attrs() and content() methods.
110  
111 Will croak if path() is false, empty or non-existent.
112  
113 =cut
114  
115 3 ▶sub read {
Subroutine read: called CC=6 SCAR=21.4
Pod documented
116 3 my $self = shift;
117 3 ▶ croak("invalid path: $self") unless ( -f $self && -s $self );
Branch: 1 branch
BranchTrueFalseTotal
unless -f $self and -s $self 0 3 3
Condition33%1/3
-f $self and -s $self
!ll&&!rl&&r
003
MC/DC0%0/2
-f $self and -s $self
-f $self -s $self
Truth table33%1/3
A and B
A= -f $selfB= -s $self
ABresult
0X0
100
111
118  
119 # TODO does ->slurp correctly handle utf8?
120 3 $self->_parse_page( scalar $self->slurp );
121  
122 #$self->debug(1);
123  
124 # cache whether we have local mods
125 3 my $committed = $self->committed;
126 3 my $diff = $self->diff;
127 3 chomp( my $outstr = $self->outstr );
128  
129 #warn "committed = $committed\ndiff = $diff\noutstr = $outstr\n";
130  
131 3 ▶ if ( $committed && $diff && length $outstr ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($committed and $diff and length $outstr) 0 3 3
Condition33%2/6
$committed and $diff and length $outstr
!ll&&!rl&&r
300
$committed and $diff
!ll&&!rl&&r
300
MC/DC0%0/3
$committed and $diff and length $outstr
$committed $diff length $outstr
Truth table25%1/4
A and B and C
A= $committedB= $diffC= length $outstr
ABCresult
0XX0
10X0
1100
1111
132 0 $self->has_unsaved_changes(1);
133 }
134  
135 #$self->debug(0);
136  
137 3 return $self;
138 }
139  
140 =head2 committed
141  
142 Returns true if the delegate file has ever been committed to the repository.
143  
144 =cut
145  
146 4 ▶sub committed {
Subroutine committed: called CC=6 SCAR=24.1
Pod documented
147 4 my $self = shift;
148 4 my $stat = $self->status;
149  
150 #warn "status = $stat";
151 #warn "error = " . $self->errstr;
152  
153 0 ▶ if ( $stat == 0 && grep {m/is not a working copy/} @{ $self->stderr } ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($stat == 0 and grep {/is not a working copy/;} @{$self->stderr;}) { } 0 4 4
elsif ($stat) { } 0 4 4
elsif ($stat == 0 and not $self->error) { } 0 4 4
Condition33%1/3
$stat == 0 and grep {/is not a working copy/;} @{$self->stderr;}
!ll&&!rl&&r
040
MC/DC0%0/2
$stat == 0 and grep {/is not a working copy/;} @{$self->stderr;}
$stat == 0 grep {/is not a working copy/;} @{$self->stderr;}
Truth table33%1/3
A and B
A= $stat == 0B= grep {/is not a working copy/;} @{$self->stderr;}
ABresult
0X0
100
111
154  
155 #warn "committed == 0";
156 0 return 0;
157 }
158 elsif ($stat) {
159  
160 # some status means svn knows about it or its parent dir.
161 0 return 1;
162 }
163 ▶ elsif ( $stat == 0
Condition33%1/3
$stat == 0 and not $self->error
!ll&&!rl&&r
040
MC/DC0%0/2
$stat == 0 and not $self->error
$stat == 0 not $self->error
Truth table33%1/3
A and not(B)
A= $stat == 0B= not $self->error
ABresult
0X0
100
111
164 && !$self->error )
165 {
166 0 return 1;
167 }
168  
169 4 return 0;
170 }
171  
172 3 ▶sub _parse_page {
Subroutine _parse_page: called CC=8 SCAR=22.0
173 3 my ( $self, $buf ) = @_;
174  
175 # example:
176 #
177 # [% # CMS
178 # cmspage.attrs.title = 'foo'
179 # %]
180 #
181 # this is a page.
182 #
183  
184 3 $self->_escape( \$buf );
185  
186 3 my ( $attrs, $content )
187 = ( $buf =~ m/^\s*\[\%\s+\#\s*CMS\s+(.+?)\%\]\s*(.*)$/s );
188 3 ▶ if ( $attrs && $content ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attrs and $content) { } 3 0 3
Condition33%1/3
$attrs and $content
!ll&&!rl&&r
003
MC/DC0%0/2
$attrs and $content
$attrs $content
Truth table33%1/3
A and B
A= $attrsB= $content
ABresult
0X0
100
111
189 3 my $depth = 100;
190 3 my $count = 0;
191 3 ▶ while ( $attrs =~ m/\bcmspage\.attrs\.(\w+)\s*=\s*(['"])(.+?)\2/sg ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attrs =~ /\bcmspage\.attrs\.(\w+)\s*=\s*(['"])(.+?)\2/gs) 4 3 7
192  
193 4 ▶ last if $count++ > $depth;
Branch: 1 branch
BranchTrueFalseTotal
if $count++ > $depth 0 4 4
194  
195 4 my $key = $1;
196 4 my $val = $3;
197  
198 # reserved words
199 4 ▶ next if $key eq 'attrs';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "attrs" 0 4 4
200 4 ▶ next if $key eq 'content';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "content" 0 4 4
201 4 ▶ next if $key eq 'page';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "page" 0 4 4
202  
203 4 $self->{attrs}->{$key} = $val;
204 }
205  
206 3 $self->content($content);
207 }
208 else {
209 0 $self->content($buf);
210 }
211 3 return $self;
212 }
213  
214 =head2 write( [I<ignore_lock>] )
215  
216 Writes attrs() and content() to file location in TT-compatible format.
217  
218 Will croak on any error.
219  
220 If the I<ignore_lock> flag is true, write() will ignore any true
221 value of locked(). Otherwise, will croak() if locked() is true.
222  
223 Returns the size of the resulting file.
224  
225 =cut
226  
227 2 ▶sub write {
Subroutine write: called CC=6 SCAR=19.3
Pod documented
228 2 my $self = shift;
229 2 ▶ my $force = shift || 0;
Condition50%1/2
shift() || 0
l!l
20
MC/DC0%0/1
shift() || 0
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
230  
231 2 ▶ if ( $self->locked && !$force ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->locked and not $force) 0 2 2
Condition33%1/3
$self->locked and not $force
!ll&&!rl&&r
020
MC/DC0%0/2
$self->locked and not $force
$self->locked not $force
Truth table33%1/3
A and not(B)
A= $self->lockedB= not $force
ABresult
0X0
100
111
232 0 croak "write failed. $self is locked";
233 }
234  
235 2 my $fh = $self->openw();
236  
237 # make sure we have at least one newline at end of file.
238 2 ▶ my $content = $self->content || '';
Condition50%1/2
$self->content || ''
l!l
20
MC/DC0%0/1
$self->content || ''
$self->content
Truth table50%1/2
A
A= $self->content
Aresult
11
00
239 2 chomp $content;
240 2 $content .= "\n";
241 2 $self->_unescape( \$content );
242  
243 2 ▶ print {$fh} join( "\n", $self->_ttify_attrs, $content )
Branch: 1 branch
BranchTrueFalseTotal
print {$fh;} join("\n", $self->_ttify_attrs, $content) or croak("write failed for ${self}: $!") 2 0 2
244 or croak "write failed for $self: $!";
245 2 $fh->close;
246  
247 2 return -s $self;
248 }
249  
250 2 my $COMMENT = 'CXCMS COMMENT: DO NOT REMOVE';
251 2 my @special_tags = qw( textarea );
252  
253 3 ▶sub _escape {
Subroutine _escape: called CC=3 SCAR=12.0
254 3 my ( $self, $buf_ref ) = @_;
255  
256 3 for my $tag (@special_tags) {
257 3 ▶ if ( $$buf_ref =~ m!</?$tag!i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$buf_ref =~ m[</?$tag]i) 0 3 3
258 0 $$buf_ref =~ s,<(/?${tag}.*?)>,<!-- $COMMENT $1 -->,sgi;
259 }
260 }
261 }
262  
263 2 ▶sub _unescape {
Subroutine _unescape: called CC=2 SCAR=8.1
264 2 my ( $self, $buf_ref ) = @_;
265  
266 2 ▶ if ( $$buf_ref =~ m!$COMMENT!i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$buf_ref =~ /$COMMENT/i) 0 2 2
267 0 $$buf_ref =~ s,<!-- $COMMENT (.+?) -->,<$1>,sgi;
268 }
269 }
270  
271 =head2 create( I<user> )
272  
273 Acquires lock for I<user> and writes files as a new page.
274  
275 =cut
276  
277 1 ▶sub create {
Subroutine create: called CC=4 SCAR=15.0
Pod documented
278 1 my $self = shift;
279 1 ▶ my $user = shift or croak "user required";
Branch: 1 branch
BranchTrueFalseTotal
my $user = shift() or croak("user required") 1 0 1
280  
281 # create any required parent directories
282 1 $self->dir->mkpath();
283  
284 1 ▶ if ( $self->locked or -s $self ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->locked or -s $self) 0 1 1
Condition33%1/3
$self->locked or -s $self
l!l&&r!l&&!r
001
MC/DC0%0/2
$self->locked or -s $self
$self->locked -s $self
Truth table33%1/3
A or B
A= $self->lockedB= -s $self
ABresult
1X1
011
000
285 0 croak "cannot create $self : locked or already exists";
286 }
287  
288 1 $self->lock($user);
289 1 $self->attrs->{owner} = $user;
290 1 $self->content('[ this is a new page ]');
291 1 return $self->write(1);
292 }
293  
294 =head2 update
295  
296 Calls write().
297  
298 B<NOTE:> This is not the same as the SVN::Class->update method!
299 If you want that method, use the up() alias instead.
300  
301 =cut
302  
303 0 ▶sub update {
Subroutine update: not called CC=2 SCAR=17.9
Pod documented
304 0 my $self = shift;
305 0 ▶ if ( !-s $self ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-s $self) 0 0 0
306 0 croak "cannot update an empty file";
307 }
308 0 $self->write(@_);
309 }
310  
311 2 ▶sub _ttify_attrs {
Subroutine _ttify_attrs: called CC=2 SCAR=6.9
312 2 my $self = shift;
313  
314 # make a TT-hash out of a Perl hash
315 2 my $attrs = $self->attrs;
316 2 my $buf = "[% # CMS\n\n";
317 2 for my $key ( sort keys %$attrs ) {
318 3 $buf .= " cmspage.attrs.$key = '" . qq($attrs->{$key}) . "';\n";
319 }
320 2 return $buf . "\n%]";
321 }
322  
323 =head2 save( I<message> [, I<leave_lock>, I<username> ] )
324  
325 Will write() file, add() to the SVN workspace if necessary,
326 and then call commit( I<message> ).
327  
328 Returns -1 if status() of file indicates no modification.
329 Otherwise, returns commit() return value.
330  
331 Pass a true for I<leave_lock> to leave the lock file intact
332 after the commit().
333  
334 Pass an authorized I<username> to have the commit() made with that
335 credential.
336  
337 =cut
338  
339 1 ▶sub save {
Subroutine save: called CC=23 SCAR=47.4
Pod documented
340 1 my $self = shift;
341 1 ▶ my $message = shift || '[no log message]';
Condition50%1/2
shift() || '[no log message]'
l!l
10
MC/DC0%0/1
shift() || '[no log message]'
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
342 1 ▶ my $leave_lock = shift || 0;
Condition50%1/2
shift() || 0
l!l
01
MC/DC0%0/1
shift() || 0
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
343 1 my $username = shift;
344  
345 # pass force flag to write() since we want lock
346 # to persist till we've committed.
347  
348 1 ▶ $self->write(1) or croak "$self failed to write(): $!";
Branch: 1 branch
BranchTrueFalseTotal
$self->write(1) or croak("$self failed to write(): $!") 1 0 1
349  
350 # might be that the parent dir hasn't been added yet
351 # so will get err rather than status.
352 # track what level we need to commit at.
353 1 my @to_add;
354  
355 1 my $dir = $self->dir;
356 1 $dir->debug( $self->debug );
357  
358 1 my $self_status = $self->status;
359 1 my $dir_status = $dir->status;
360  
361 1 ▶ if ( $self->debug ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->debug) 0 1 1
362 0 warn "self = $self status = $self_status";
363 0 warn "dir = $dir status = $dir_status";
364 }
365  
366 # walk up the tree
367 1 ▶ while (
Branch: 1 branch
BranchTrueFalseTotal
if ($dir_status eq 0 and $dir->errstr || grep({/is not a working copy/;} @{$dir->stderr;})) 0 1 1
Condition33%2/6
$dir_status eq 0 and $dir->errstr || grep({/is not a working copy/;} @{$dir->stderr;})
!ll&&!rl&&r
010
$dir->errstr or grep {/is not a working copy/;} @{$dir->stderr;}
l!l&&r!l&&!r
001
MC/DC0%0/3
$dir_status eq 0 and $dir->errstr || grep({/is not a working copy/;} @{$dir->stderr;})
$dir_status eq 0 $dir->errstr grep {/is not a working copy/;} @{$dir->stderr;}
Truth table25%1/4
A and B or C
A= $dir_status eq 0B= $dir->errstrC= grep {/is not a working copy/;} @{$dir->stderr;}
ABCresult
0XX0
1000
11X1
1011
368 $dir_status eq '0'
369 && ( $dir->errstr
370 0 || grep {/is not a working copy/} @{ $dir->stderr } )
371 )
372 {
373  
374 0 ▶ $self->debug and warn "no status for $dir";
Branch: 1 branch
BranchTrueFalseTotal
$self->debug and warn "no status for $dir" 0 0 0
375 0 unshift( @to_add, $dir );
376 0 $dir = $dir->parent;
377 0 $dir_status = $dir->status;
378 0 ▶ $self->debug and warn "dir $dir status = $dir_status";
Branch: 1 branch
BranchTrueFalseTotal
$self->debug and warn "dir $dir status = $dir_status" 0 0 0
379  
380 # avoid infinite loops
381 0 ▶ if ( $dir eq '/' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dir eq "/") 0 0 0
382 0 croak "infinite loop looking up svn workdir tree for $self";
383 }
384 }
385  
386 # found one that svn knows about
387 1 ▶ if ( $dir_status eq '?' or $dir_status eq 'A' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dir_status eq "?" or $dir_status eq "A") 0 1 1
Condition33%1/3
$dir_status eq "?" or $dir_status eq "A"
l!l&&r!l&&!r
001
MC/DC0%0/2
$dir_status eq "?" or $dir_status eq "A"
$dir_status eq "?" $dir_status eq "A"
Truth table33%1/3
A or B
A= $dir_status eq "?"B= $dir_status eq "A"
ABresult
1X1
011
000
388 0 unshift( @to_add, $dir );
389 }
390  
391 1 ▶ $self->debug and warn "to_add: " . dump \@to_add;
Branch: 1 branch
BranchTrueFalseTotal
$self->debug and warn 'to_add: ' . CatalystX::CMS::File::dump(\@to_add) 0 1 1
392  
393 # we 'add' each dir non-recursively
394 # so that we do not add unaffected files (like lock files) by mistake.
395 1 for my $d (@to_add) {
396 0 ▶ $d->add( ['-N'] ) unless $d->status eq 'A';
Branch: 1 branch
BranchTrueFalseTotal
unless $d->status eq "A" 0 0 0
397 0 my @opts = '-N';
398 0 ▶ if ($username) {
Branch: 1 branch
BranchTrueFalseTotal
if ($username) 0 0 0
399 0 push( @opts, "--username $username" );
400 }
401 0 ▶ $d->commit( "new directory", \@opts )
Branch: 1 branch
BranchTrueFalseTotal
$d->commit("new directory", \@opts) or croak("add directory $d failed: " . $d->errstr) 0 0 0
402 or croak "add directory $d failed: " . $d->errstr;
403 }
404  
405 # now check the stat of the file itself
406 1 my $stat = $self->status;
407 1 ▶ if ( $stat && $stat eq '?' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($stat and $stat eq "?") 0 1 1
Condition33%1/3
$stat and $stat eq "?"
!ll&&!rl&&r
100
MC/DC0%0/2
$stat and $stat eq "?"
$stat $stat eq "?"
Truth table33%1/3
A and B
A= $statB= $stat eq "?"
ABresult
0X0
100
111
408 0 $self->add;
409 }
410  
411 1 my $ret;
412  
413 # is file modified at all?
414 1 ▶ if ( $self->modified ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->modified) { } 0 1 1
415 0 $ret = $self->commit($message);
416 }
417 else {
418  
419 # no attempt made to commit unmodified file
420 1 $ret = -1;
421 }
422  
423 1 ▶ unless ($leave_lock) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($leave_lock) 1 0 1
424 1 ▶ $self->unlock or croak "can't unlock $self: $!";
Branch: 1 branch
BranchTrueFalseTotal
$self->unlock or croak("can't unlock ${self}: $!") 1 0 1
425 }
426 1 return $ret;
427 }
428  
429 =head2 escape_tt
430  
431 Returns content() with all TT code wrapped in special XHTML tagset
432  
433 <code class="tt">
434  
435 This is to allow for such code to be escaped online or otherwise parsed
436 with a XML parser.
437  
438 =cut
439  
440 0 ▶sub escape_tt {
Subroutine escape_tt: not called CC=1 SCAR=6.9
Pod documented
441 0 my $self = shift;
442 0 my $content = $self->content;
443  
444 0 $content =~ s/\[\%/<code class="tt">\[\%/g;
445 0 $content =~ s/\%\]/\%\]<\/code>/g;
446  
447 0 return $content;
448 }
449  
450 # this isn't currently used but could be.
451 #sub as_chunks {
452 # my $self = shift;
453 # my $escape = shift || 0;
454 #
455 # # NOTE the regex keeps the surrounding whitespace WITH the PROCESS
456 # # line so that it is preserved in the web interaction.
457 # my @chunks = split( m!(\s*\[\%\ +PROCESS tt/share/\S+\ +\%\]\s*)!s,
458 # $self->content );
459 #
460 # # turn into array of hashrefs
461 # my @hashes;
462 #
463 # #carp "raw chunks: " . dump \@chunks;
464 #
465 # for my $chunk (@chunks) {
466 # my $h = { content => $chunk };
467 #
468 # # editable text
469 # unless ( $chunk =~ m/^\s*\[\%\s+PROCESS/ ) {
470 # $h->{editable} = 1;
471 #
472 # # wrap TT instructions in <pre> tags so they show verbatim
473 # if ($escape) {
474 # $h->{content} =~ s,\[\%,<pre class="tt">\[\%,g;
475 # $h->{content} =~ s,\%\],\%\]</pre>,g;
476 # }
477 #
478 # }
479 #
480 # # shared text -- render it as TT would but just that chunk.
481 # else {
482 # $h->{editable} = 0;
483 # }
484 #
485 # push( @hashes, $h );
486 # }
487 #
488 # return \@hashes;
489 #}
490 #
491 #=head2 save_chunk( I<chunk_id>, I<new_text>, I<message> )
492 #
493 #I<chunk_id> is element of the array returned by as_chunks().
494 #
495 #I<new_text> is the text to be saved at that element.
496 #
497 #Calls save() with I<message>. Returns save() return value.
498 #
499 #=cut
500 #
501 #sub save_chunk {
502 # my $self = shift;
503 # my $id = shift || 0;
504 # my $text = shift || '';
505 # my $message = shift or croak "need save message";
506 #
507 # my $chunks = $self->as_chunks; # NO escape
508 #
509 # $chunks->[$id]->{content} = $text;
510 #
511 # carp dump $chunks;
512 #
513 # $self->content( join( '', map { $_->{content} } @$chunks ) );
514 #
515 # return $self->save( $message, 1 );
516 #}
517  
518 2 1;
519  
520 __END__
« blib/lib/CatalystX/CMS.pm ↑ Summary lib/CatalystX/CMS/Model.pm »