| 1 | package TT2::Play::Area; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | # ABSTRACT: Simple site to allow playing with TT2 syntax and built in plugins. | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use strictures 2; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use Cpanel::JSON::XS; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use Dancer2; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 1 | ▶ | use File::ShareDir 'module_dir'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use Path::Tiny; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 1 | ▶ | use Template; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 1 | ▶ | use Template::Alloy; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 1 | our $VERSION = '0.001'; | |||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 1 | my $example_dir = path( module_dir('TT2::Play::Area') )->child('examples'); | |||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 1 | set appname => 'TT2::Play::Area'; | |||||||||||||||||||||||||||
| 19 | 1 | set charset => 'UTF-8'; | |||||||||||||||||||||||||||
| 20 | 1 | set engines => { template => { AUTO_FILTER => 'html' } }; | |||||||||||||||||||||||||||
| 21 | 1 | set layout => 'main'; | |||||||||||||||||||||||||||
| 22 | 1 | set public_dir => | |||||||||||||||||||||||||||
| 23 | path( module_dir('TT2::Play::Area') )->child('public')->stringify; | ||||||||||||||||||||||||||||
| 24 | 1 | set template => 'alloy'; | |||||||||||||||||||||||||||
| 25 | 1 | set views => path( module_dir('TT2::Play::Area') )->child('views')->stringify; | |||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 1 | my %engines = ( | |||||||||||||||||||||||||||
| 28 | tt2 => 'TT2', | ||||||||||||||||||||||||||||
| 29 | alloy => 'Template::Alloy', | ||||||||||||||||||||||||||||
| 30 | alloy_html => 'Template::Alloy + AUTO_FILTER = html', | ||||||||||||||||||||||||||||
| 31 | ); | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | get '/' => sub { | ||||||||||||||||||||||||||||
| 34 | 1 | ▶ | my $template = $example_dir->child('index.tt'); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 35 | 1 | my $vars_file = $example_dir->child('index.vars'); | |||||||||||||||||||||||||||
| 36 | 1 | template 'index', | |||||||||||||||||||||||||||
| 37 | { | ||||||||||||||||||||||||||||
| 38 | engine_list => engine_list_for_ui( selected => ['tt2'] ), | ||||||||||||||||||||||||||||
| 39 | examples => load_examples(), | ||||||||||||||||||||||||||||
| 40 | tt => $template->slurp_utf8, | ||||||||||||||||||||||||||||
| 41 | variables => $vars_file->slurp_utf8, | ||||||||||||||||||||||||||||
| 42 | }; | ||||||||||||||||||||||||||||
| 43 | 1 | }; | |||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | sub load_examples { | ||||||||||||||||||||||||||||
| 46 | 2 | ▶ | my @examples; | ||||||||||||||||||||||||||
|
Subroutine
load_examples: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 47 | 2 | my @files = $example_dir->children(qr/\.settings/); | |||||||||||||||||||||||||||
| 48 | 2 | for my $file (@files) { | |||||||||||||||||||||||||||
| 49 | 10 | my ( $name, $settings ) = load_settings($file); | |||||||||||||||||||||||||||
| 50 | 10 | ▶ | unless ( $name eq 'index' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 51 | 10 | push @examples, { name => $name, title => $settings->{title} }; | |||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||
| 54 | 2 | return [ sort { lc $a->{title} cmp lc $b->{title} } @examples ]; | |||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | sub engine_list_for_ui { | ||||||||||||||||||||||||||||
| 58 | 2 | ▶ | my %args = @_; | ||||||||||||||||||||||||||
|
Subroutine
engine_list_for_ui: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 59 | 2 | my %engine_list; | |||||||||||||||||||||||||||
| 60 | %engine_list = | ||||||||||||||||||||||||||||
| 61 | 2 | map { $_ => { name => $engines{$_}, selected => 0 } } keys %engines; | |||||||||||||||||||||||||||
| 62 | 2 | $engine_list{$_}{selected} = 1 for @{ $args{selected} }; | |||||||||||||||||||||||||||
| 63 | 2 | return \%engine_list; | |||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | post '/tt2' => sub { | ||||||||||||||||||||||||||||
| 67 | 3 | ▶ | my $tt = body_parameters->{template}; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=7 SCAR=19.5
| |||||||||||||||||||||||||||||
| 68 | 3 | my $vars; | |||||||||||||||||||||||||||
| 69 | 3 | my @engines = query_parameters->get_all('engine'); | |||||||||||||||||||||||||||
| 70 | 3 | eval { $vars = decode_json( body_parameters->{vars} ); }; | |||||||||||||||||||||||||||
| 71 | 3 | ▶ | if ($@) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 72 | 1 | send_as JSON => | |||||||||||||||||||||||||||
| 73 | { result => { 'Error' => 'Failed to parse variables: ' . $@, } }; | ||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 2 | my $config = {}; | |||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 2 | my %engine_output; | |||||||||||||||||||||||||||
| 79 | 2 | for my $engine (@engines) { | |||||||||||||||||||||||||||
| 80 | 4 | my $output; | |||||||||||||||||||||||||||
| 81 | 4 | ▶ | if ( $engine eq 'tt2' ) { | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | # create Template object | ||||||||||||||||||||||||||||
| 84 | 2 | my $template = Template->new($config); | |||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 2 | ▶ | if ( !$template->process( \$tt, $vars, \$output ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 87 | 1 | $output = 'Template error: ' . $template->error()->as_string; | |||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | elsif ( $engine eq 'alloy' ) { | ||||||||||||||||||||||||||||
| 91 | 1 | $output = process_alloy( {} ); | |||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | elsif ( $engine eq 'alloy_html' ) { | ||||||||||||||||||||||||||||
| 94 | 1 | $output = process_alloy( { AUTO_FILTER => 'html' } ); | |||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | 4 | $engine_output{ $engines{$engine} } = $output; | |||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 2 | send_as JSON => { result => \%engine_output, }; | |||||||||||||||||||||||||||
| 100 | 1 | }; | |||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | get '/example/:name' => sub { | ||||||||||||||||||||||||||||
| 103 | 1 | ▶ | my $name = route_parameters->get('name'); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=6 SCAR=18.9
| |||||||||||||||||||||||||||||
| 104 | 1 | ▶ | return status 404 unless $name =~ /^[-\w]+$/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | 1 | my $template = $example_dir->child( $name . '.tt' ); | |||||||||||||||||||||||||||
| 107 | 1 | my $vars_file = $example_dir->child( $name . '.vars' ); | |||||||||||||||||||||||||||
| 108 | 1 | my $settings = $example_dir->child( $name . '.settings' ); | |||||||||||||||||||||||||||
| 109 | 1 | ▶ | unless ( $vars_file->exists && $template->exists ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$vars_file->exists and $template->exists
MC/DC0%0/2
$vars_file->exists and $template->exists
$vars_file->exists
$template->exists
Truth table33%1/3
A and B
A= $vars_file->existsB= $template->exists
| |||||||||||||||||||||||||||||
| 110 | 0 | return status 404; | |||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||
| 112 | 1 | my $selected = ['tt2']; | |||||||||||||||||||||||||||
| 113 | 1 | my $tt_vars = { | |||||||||||||||||||||||||||
| 114 | examples => load_examples(), | ||||||||||||||||||||||||||||
| 115 | tt => $template->slurp_utf8, | ||||||||||||||||||||||||||||
| 116 | variables => $vars_file->slurp_utf8, | ||||||||||||||||||||||||||||
| 117 | }; | ||||||||||||||||||||||||||||
| 118 | 1 | ▶ | if ( $settings->exists ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 119 | 1 | my ( $name, $data ) = load_settings($settings); | |||||||||||||||||||||||||||
| 120 | 1 | $tt_vars->{example_data} = $data; | |||||||||||||||||||||||||||
| 121 | 1 | $tt_vars->{example_name} = $name; | |||||||||||||||||||||||||||
| 122 | 1 | ▶ | $selected = $data->{engines} if exists $data->{engines}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||
| 124 | 1 | $tt_vars->{engine_list} = engine_list_for_ui( selected => $selected ); | |||||||||||||||||||||||||||
| 125 | 1 | template 'index', $tt_vars; | |||||||||||||||||||||||||||
| 126 | 1 | }; | |||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | sub process_alloy { | ||||||||||||||||||||||||||||
| 129 | 2 | ▶ | my $config = shift; | ||||||||||||||||||||||||||
|
Subroutine
process_alloy: called CC=2 SCAR=7.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 130 | 2 | my $tt = body_parameters->{template}; | |||||||||||||||||||||||||||
| 131 | 2 | my $vars = decode_json( body_parameters->{vars} ); | |||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | # create Template object | ||||||||||||||||||||||||||||
| 134 | 2 | my $template = Template::Alloy->new($config); | |||||||||||||||||||||||||||
| 135 | 2 | my $output; | |||||||||||||||||||||||||||
| 136 | 2 | ▶ | unless ( $template->process( \$tt, $vars, \$output ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 137 | 0 | $output = 'Template error:' . $template->error()->as_string; | |||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | 2 | return $output; | |||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | sub load_settings { | ||||||||||||||||||||||||||||
| 144 | 11 | ▶ | my $file = shift; | ||||||||||||||||||||||||||
|
Subroutine
load_settings: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 145 | 11 | my ($name) = $file->stringify =~ /(\w+)\.settings$/; | |||||||||||||||||||||||||||
| 146 | 11 | my $data = decode_json( $file->slurp_utf8 ); | |||||||||||||||||||||||||||
| 147 | 11 | return ( $name, $data ); | |||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | 1 | 1; | |||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | __END__ | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | =pod | ||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | =encoding UTF-8 | ||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | =head1 NAME | ||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | TT2::Play::Area - Simple site to allow playing with TT2 syntax and built in plugins. | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | =head1 VERSION | ||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | version 0.001 | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | This is a mini site for testing L<Template> Toolkit 2 and L<Template::Alloy> | ||||||||||||||||||||||||||||
| 169 | rendering in a similar way to sites like jsFiddle. It provides a pane for | ||||||||||||||||||||||||||||
| 170 | editing the template, and a pane for providing the variables to pass it (in | ||||||||||||||||||||||||||||
| 171 | JSON). | ||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | Currently supported 'engines' are, | ||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | =over | ||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | =item * Template (TT2) | ||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | =item * Template::Alloy | ||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | =item * Template::Alloy (using AUTO_FILTER html) | ||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | =back | ||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | On the front end jQuery and CodeMirror are used to provide the UI. | ||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | =head1 RUNNING | ||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | The site is automatically built into a docker container on quay.io so if | ||||||||||||||||||||||||||||
| 190 | you simply want to spin it up the quickest way is to, | ||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | docker run -d -p5000:5000 quay.io/colinnewell/tt2-play-area:latest | ||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | This will expose it on port 5000 on localhost, so you should be able to | ||||||||||||||||||||||||||||
| 195 | browse to L<http://localhost:5000>. | ||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | Alternatively, if you've installed the module from CPAN then you can run it | ||||||||||||||||||||||||||||
| 198 | using plackup like this, | ||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | plackup `which tt2-play-area.psgi` | ||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||
| 202 | Note that if you run with starman you need to use the C<--preload-app> | ||||||||||||||||||||||||||||
| 203 | option otherwise you will get 403 errors when trying to process the | ||||||||||||||||||||||||||||
| 204 | templates because the CSRF prevention mechanism gets in the way. | ||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||
| 206 | Or if you checkout the github repo like this, | ||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||
| 208 | cpanm --installdeps . | ||||||||||||||||||||||||||||
| 209 | plackup -I lib bin/tt2-play-area.psgi | ||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||
| 211 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||
| 213 | Colin Newell <colin.newell@gmail.com> | ||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||
| 215 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||
| 217 | This software is Copyright (c) 2018 by Colin Newell. | ||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||
| 219 | This is free software, licensed under: | ||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||
| 221 | The MIT (X11) License | ||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||
| 223 | =cut | ||||||||||||||||||||||||||||