package Bigtop::Backend::SiteLook::GantryDefault; use strict; use warnings; # I would normally use Inline TT to control the appearance of the # output, but I've been scared away before even trying to generate # a valid template toolkit template with a tt template. # So, I resort the bad old days of here docs. use File::Spec; use Bigtop; use Bigtop::Parser; BEGIN { Bigtop::Parser->add_valid_keywords( Bigtop::Keywords->get_docs_for( 'app', 'location', ) ); Bigtop::Parser->add_valid_keywords( Bigtop::Keywords->get_docs_for( 'controller', qw( location rel_location page_link_label ) ) ); } sub what_do_you_make { return [ [ 'html/genwrapper.html' => 'A sample template toolkit wrapper [please change it]' ], ]; } sub backend_block_keywords { return [ { keyword => 'no_gen', label => 'No Gen', descr => 'Skip everything for this backend', type => 'boolean' }, { keyword => 'gantry_wrapper', label => 'Gantry Wrapper Path', descr => 'Path to sample_wrapper.tt in the Gantry ' . 'distribution [defaults to ./html]', type => 'text' }, ]; } sub gen_SiteLook { my $class = shift; my $base_dir = shift; my $tree = shift; # make the html directory my $html_dir = File::Spec->catdir( $base_dir, 'html' ); mkdir $html_dir; $html_dir = File::Spec->catdir( $html_dir, 'templates' ); mkdir $html_dir; # find the root location my $app_statements = $tree->{application}{lookup}{app_statements}; my $app_location = $app_statements->{location}[0] if ( defined $app_statements->{location} ); # make the wrapper... # ...get controllers my $controllers = $tree->walk_postorder( 'output_controller_name' ); my $control_lookup = $tree->{application}{lookup}{controllers}; my %location_for; my %title_for; CONTROLLER: foreach my $controller ( @{ $controllers } ) { my $statements = $control_lookup->{ $controller }{statements}; my $location_list = $statements->{rel_location} || $statements->{location}; my $title_list = $statements->{page_link_label}; next CONTROLLER unless $title_list; next CONTROLLER if ( $controller eq 'base_controller' ); unless ( $location_list ) { die 'Error: no location or rel_location defined for ' . "controller $controller\n"; } $location_for{ $controller } = $location_list->[0]; $title_for { $controller } = $title_list ->[0]; } # ...make the content my $links = build_links( $controllers, $app_location, \%location_for, \%title_for ); my $content; eval { $content = build_wrapper( $tree->get_config, $links ); }; if ( $@ ) { warn $@; return; } # ...write it my $wrapper = File::Spec->catfile( $html_dir, 'genwrapper.tt' ); eval { Bigtop::write_file( $wrapper, $content ); }; warn $@ if ( $@ ); } sub build_wrapper { my $config = shift; my $links = shift; # load the default wrapper my $wrapper_file = $config->{SiteLook}{gantry_wrapper}; unless ( defined $wrapper_file ) { require Gantry::Init; $wrapper_file = Gantry::Init::base_root() . '/sample_wrapper.tt'; } my $default_wrapper; my $WRAPPER; open $WRAPPER, '<', $wrapper_file or die "Couldn't read $wrapper_file: $!\n"; while ( my $wrapper_line = <$WRAPPER> ) { $wrapper_line =~ s/\s*\n/$links/ if $links; $default_wrapper .= $wrapper_line; } close $WRAPPER; return $default_wrapper; } sub build_links { my $controllers = shift; my $app_location = shift; my $location_for = shift; my $title_for = shift; # no leading or trailing slashes please $app_location =~ s{^/+}{} if $app_location; $app_location =~ s{/+$}{} if $app_location; # make the links my $lead = '