File Coverage

blib/lib/Bigtop/Backend/SiteLook/GantryDefault.pm
Criterion Covered Total %
statement 13 15 86.6
branch n/a
condition n/a
subroutine 5 5 100.0
pod n/a
total 18 20 90.0


line stmt bran cond sub pod time code
1             package Bigtop::Backend::SiteLook::GantryDefault;
2 1     1   1166 use strict; use warnings;
  1     1   1  
  1         30  
  1         4  
  1         1  
  1         22  
3              
4             # I would normally use Inline TT to control the appearance of the
5             # output, but I've been scared away before even trying to generate
6             # a valid template toolkit template with a tt template.
7             # So, I resort the bad old days of here docs.
8              
9 1     1   4 use File::Spec;
  1         2  
  1         14  
10 1     1   4 use Bigtop;
  1         1  
  1         19  
11 1     1   37 use Bigtop::Parser;
  0            
  0            
12              
13             BEGIN {
14             Bigtop::Parser->add_valid_keywords(
15             Bigtop::Keywords->get_docs_for(
16             'app', 'location',
17             )
18             );
19              
20             Bigtop::Parser->add_valid_keywords(
21             Bigtop::Keywords->get_docs_for(
22             'controller',
23             qw( location rel_location page_link_label )
24             )
25             );
26             }
27              
28             sub what_do_you_make {
29             return [
30             [ 'html/genwrapper.html' =>
31             'A sample template toolkit wrapper [please change it]' ],
32             ];
33             }
34              
35             sub backend_block_keywords {
36             return [
37             { keyword => 'no_gen',
38             label => 'No Gen',
39             descr => 'Skip everything for this backend',
40             type => 'boolean' },
41              
42             { keyword => 'gantry_wrapper',
43             label => 'Gantry Wrapper Path',
44             descr => 'Path to sample_wrapper.tt in the Gantry '
45             . 'distribution [defaults to ./html]',
46             type => 'text' },
47             ];
48             }
49              
50             sub gen_SiteLook {
51             my $class = shift;
52             my $base_dir = shift;
53             my $tree = shift;
54              
55             # make the html directory
56             my $html_dir = File::Spec->catdir( $base_dir, 'html' );
57             mkdir $html_dir;
58             $html_dir = File::Spec->catdir( $html_dir, 'templates' );
59             mkdir $html_dir;
60              
61             # find the root location
62             my $app_statements = $tree->{application}{lookup}{app_statements};
63             my $app_location = $app_statements->{location}[0]
64             if ( defined $app_statements->{location} );
65              
66             # make the wrapper...
67              
68             # ...get controllers
69             my $controllers
70             = $tree->walk_postorder( 'output_controller_name' );
71              
72             my $control_lookup = $tree->{application}{lookup}{controllers};
73             my %location_for;
74             my %title_for;
75              
76             CONTROLLER:
77             foreach my $controller ( @{ $controllers } ) {
78             my $statements = $control_lookup->{ $controller }{statements};
79             my $location_list = $statements->{rel_location}
80             || $statements->{location};
81             my $title_list = $statements->{page_link_label};
82              
83             next CONTROLLER unless $title_list;
84             next CONTROLLER if ( $controller eq 'base_controller' );
85              
86             unless ( $location_list ) {
87             die 'Error: no location or rel_location defined for '
88             . "controller $controller\n";
89             }
90              
91             $location_for{ $controller } = $location_list->[0];
92             $title_for { $controller } = $title_list ->[0];
93             }
94              
95             # ...make the content
96             my $links = build_links(
97             $controllers, $app_location, \%location_for, \%title_for
98             );
99             my $content;
100             eval {
101             $content = build_wrapper( $tree->get_config, $links );
102             };
103             if ( $@ ) {
104             warn $@;
105             return;
106             }
107              
108             # ...write it
109             my $wrapper = File::Spec->catfile( $html_dir, 'genwrapper.tt' );
110              
111             eval {
112             Bigtop::write_file( $wrapper, $content );
113             };
114             warn $@ if ( $@ );
115             }
116              
117             sub build_wrapper {
118             my $config = shift;
119             my $links = shift;
120              
121             # load the default wrapper
122             my $wrapper_file = $config->{SiteLook}{gantry_wrapper};
123              
124             unless ( defined $wrapper_file ) {
125             require Gantry::Init;
126             $wrapper_file = Gantry::Init::base_root() . '/sample_wrapper.tt';
127             }
128              
129             my $default_wrapper;
130             my $WRAPPER;
131              
132             open $WRAPPER, '<', $wrapper_file
133             or die "Couldn't read $wrapper_file: $!\n";
134              
135             while ( my $wrapper_line = <$WRAPPER> ) {
136             $wrapper_line =~ s/\s*\n/$links/ if $links;
137              
138             $default_wrapper .= $wrapper_line;
139             }
140              
141             close $WRAPPER;
142              
143             return $default_wrapper;
144             }
145              
146             sub build_links {
147             my $controllers = shift;
148             my $app_location = shift;
149             my $location_for = shift;
150             my $title_for = shift;
151              
152             # no leading or trailing slashes please
153             $app_location =~ s{^/+}{} if $app_location;
154             $app_location =~ s{/+$}{} if $app_location;
155              
156             # make the links
157             my $lead = '
  • 158             my $links;
    159              
    160             CONTROLLER:
    161             foreach my $controller ( @{ $controllers } ) {
    162             my $location = $location_for ->{ $controller };
    163             my $text = $title_for->{ $controller };
    164              
    165             next CONTROLLER unless $location and $text;
    166             if ( defined $app_location ) {
    167             $links .= "$lead/$app_location/$location'>$text\n";
    168             }
    169             else {
    170             $links .= "$lead/$location'>$text\n";
    171             }
    172             }
    173              
    174             return $links;
    175             }
    176              
    177             # controller_block
    178             package #
    179             controller_block;
    180             use strict; use warnings;
    181              
    182             sub output_controller_name {
    183             my $self = shift;
    184              
    185             return [ $self->{__NAME__} ];
    186             }
    187              
    188             1;
    189              
    190             =head1 NAME
    191              
    192             Bigtop::Backend::SiteLook::GantryDefault - Bigtop to generate site appearance files
    193              
    194             =head1 SYNOPSIS
    195              
    196             If your bigtop file looks like this:
    197              
    198             config {
    199             SiteLook GantryDefault {}
    200             }
    201             app App::Name { }
    202              
    203             and their are controllers in the app block, this backend will generate
    204             the templates for your app (mostly by copying them from Gantry's collection)
    205             when you type:
    206              
    207             bigtop your.bigtop SiteLook
    208              
    209             or
    210              
    211             bigtop your.bigtop all
    212              
    213             The templates and other files will live in the html subdirectory of the
    214             build directory. The files generated (or copied) include genwrapper.tt,
    215             index.html, and all the other templates that various bits of Gantry use
    216             (notably the AutoCRUD). It also makes a couple of css files in the
    217             html/css subdirectory of the build dir.
    218              
    219             =head1 DESCRIPTION
    220              
    221             This Bigtop backend generates templates and css files to make your app
    222             look like the Gantry samples.
    223              
    224             =head1 KEYWORDS
    225              
    226             This modules registers the location keyword at the app and controller level.
    227             It also registers page_link_label and rel_location at the controller level.
    228             Note that all of these except page_link_label are also registered by
    229             Control modules.
    230              
    231             =head1 METHODS
    232              
    233             To keep podcoverage tests happy.
    234              
    235             =over 4
    236              
    237             =item backend_block_keywords
    238              
    239             Tells tentmaker that I understand these config section backend block keywords:
    240              
    241             no_gen
    242             gantry_wrapper
    243              
    244             =item what_do_you_make
    245              
    246             Tells tentmaker what this module makes. Summary: genwrapper.tt a TT WRAPPER.
    247              
    248             =item gen_SiteLook
    249              
    250             Called by Bigtop::Parser to get me to do my thing.
    251              
    252             =item build_links
    253              
    254             What I call on the various AST packages to do my thing.
    255              
    256             =item build_wrapper
    257              
    258             What I call on the various AST packages to do my thing.
    259              
    260             =back
    261              
    262             =head1 AUTHOR
    263              
    264             Phil Crow
    265              
    266             =head1 COPYRIGHT and LICENSE
    267              
    268             Copyright (C) 2005 by Phil Crow
    269              
    270             This library is free software; you can redistribute it and/or modify
    271             it under the same terms as Perl itself, either Perl version 5.8.6 or,
    272             at your option, any later version of Perl 5 you may have available.
    273              
    274             =cut