X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=urisagit%2FCMS-Simple.git;a=blobdiff_plain;f=CMS%2FSimple.pm;fp=CMS%2FSimple.pm;h=05184e1dadd21c567ee9c3a7fc64caad7458679d;hp=f47fb3f01a997f5638ddded49dc66472466304a3;hb=0db1d6261b28678ab1ba3f99b1885a55e5d8083d;hpb=9e609156353bacdda67eb190212f4d67181bbe83 diff --git a/CMS/Simple.pm b/CMS/Simple.pm index f47fb3f..05184e1 100644 --- a/CMS/Simple.pm +++ b/CMS/Simple.pm @@ -4,6 +4,7 @@ use warnings ; use strict ; use Carp ; +use File::Path; use Data::Dumper ; use CMS::Simple::Parse ; @@ -62,6 +63,22 @@ sub new { return $self ; } +sub _find_content_file { + + my( $self, $file ) = @_ ; + + my @dirs = @{$self->{content_paths}} ; + + foreach my $dir ( @dirs ) { + + my $file_path = "$dir/$file" ; + + return $file_path if -e $file_path ; + } + + croak "can't file content file '$file' in [@dirs]" ; +} + sub _load_content { @@ -77,15 +94,17 @@ sub _load_content { # ADD BETTER DIRECTORY STUFF ###### - my $file_path = "$self->{content_paths}[0]/$file" ; + my $file_path = $self->_find_content_file( $file ) ; my $content_text = read_file( $file_path ) ; +#print "FILE $file_path\n$content_text" if $file_path =~ /location/; + my ($suffix) = $file_path =~ /\.(\w+)$/ ; my $parser = $parsers{ $suffix } ; - $parser or die "unknown suffix '$suffix'" ; + $parser or croak "unknown suffix '$suffix'" ; my $parsed = $parser->( $content_text ) ; @@ -129,6 +148,9 @@ sub build_page { $self->_filter_page_content( $page ) ; + if( my $filter = $page->{filter} ) { + $filter->( $page ) ; + } $self->_render_page( $page ) ; #print ${$page->{rendered}} ; @@ -149,29 +171,56 @@ sub _get_page_content { my $page_contents = $page->{contents} || {} ; + $page->{contents_map} ||= { $page->{name} => '' } ; + # loop over the default (common) and page specific content maps foreach my $contents_map ( $self->{default_contents_map}, $page->{contents_map} ) { -#print "MAP ", Dumper $contents_map ; +print "MAP ", Dumper $contents_map if $page->{dump_content} ; + + if ( ref $contents_map eq 'ARRAY' ) { - while( my( $name, $location ) = each %{$contents_map} ) { + for( my $i = 0 ; $i < @$contents_map ; $i += 2 ) { # get the contents for this content name + my( $name, $location ) = + @{$contents_map}[$i, $i+1] ; + + my $contents = $all_contents->{$name}{parsed} ; + + $self->_add_page_contents( + $page_contents, + $location, + $contents + ) ; + +#print "CONT ", Dumper $page_contents if $page->{dump_content} ; - my $contents = $all_contents->{$name}{parsed} ; + } + } + else { + while( my( $name, $location ) = + each %{$contents_map} ) { + + # get the contents for this content name + + my $contents = $all_contents->{$name}{parsed} ; - $self->_add_page_contents( - $page_contents, - $location, - $contents - ) ; + #print "CONT ", Dumper $contents if $page->{dump_content} ; + + $self->_add_page_contents( + $page_contents, + $location, + $contents + ) ; + } } } - print Dumper $page_contents if $page->{dump} ; + print "ALL CONT ", Dumper $page_contents if $page->{dump_content} ; $page->{contents} = $page_contents ; } @@ -218,7 +267,7 @@ sub _filter_page_content { $self->_filter_content_hash( $page->{contents} ) ; -#print Dumper $page->{contents} ; + print "DONE\n", Dumper $page->{contents} if $page->{dump_filtered} ; } sub _filter_content_hash { @@ -232,6 +281,9 @@ sub _filter_content_hash { next unless @new_val ; $href->{$tag} = $new_val[0] ; + +#print "NEW\n", Dumper \@new_val if $tag =~ /location/ ; + } } @@ -259,10 +311,27 @@ sub _filter_content_tag { my( $self, $tag, $val, $path ) = @_ ; +#print "TAG1 $tag\n" ; +#print "TAG $tag\n" if $tag =~ /location/ ; + my $ref_type = ref $val ; + + if( my @new_val = + $self->_filter_content_value( $tag, $val, $path ) ) { + + $val = $new_val[0] ; + +#print "VAL $tag\n" if $tag =~ /location/ ; + +# handle case where the filter changed the type of the value + + $ref_type = ref $val ; + } if ( $ref_type eq 'HASH' ) { +#print "HASH $tag\n" if $tag =~ /location/ ; + $self->_filter_content_hash( $val, $path ) ; return $val ; } @@ -273,14 +342,6 @@ sub _filter_content_tag { return $val ; } - my @new_val = $self->_filter_content_value( $tag, $val, $path ) ; - - return unless @new_val ; - - $val = $new_val[0] ; - - $self->_filter_content_tag( $tag, $val, $path ) if ref $val ; - return $val ; } @@ -288,6 +349,8 @@ sub _filter_content_value { my( $self, $tag, $val, $path ) = @_ ; +#print "VAL TAG $tag\n" if $tag =~ /location/ ; + my $filters = $self->{tag_to_filters}{$tag} ; return unless $filters ; @@ -347,8 +410,16 @@ sub _output_page { # use file::path stuff to make this portable ########## - my $output_path = - "$self->{'output_dir'}/$page->{name}$self->{'output_suffix'}" ; + my $sub_dir = $page->{sub_dir} || '' ; + $sub_dir .= '/' if $sub_dir ; + + my $suffix = $page->{'output_suffix'} || $self->{'output_suffix'} ; + + my $output_dir = "$self->{'output_dir'}/$sub_dir" ; + + mkpath( $output_dir ) unless -d $output_dir ; + + my $output_path = "$output_dir$page->{name}$suffix" ; $page->{'output_path'} = $output_path ;