Skip to content

Commit

Permalink
Xslate
Browse files Browse the repository at this point in the history
  • Loading branch information
Arthur Axel 'fREW' Schmidt committed Sep 24, 2015
1 parent 778920c commit e38f6b4
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 28 deletions.
3 changes: 2 additions & 1 deletion cpanfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
requires 'IO::All';
requires 'JSON::XS';
requires 'Web::Simple';
requires 'namespace::clean';
requires 'Web::Simple';
requires 'Text::Xslate';

on test => sub {
requires 'Test::More';
Expand Down
68 changes: 41 additions & 27 deletions lib/WWW/RetroPie/Game/Picker.pm
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use warnings NONFATAL => 'all';
use autodie;

use IO::All;
use Text::Xslate;

use WWW::RetroPie::Game::Picker::ConfigLoader;

Expand All @@ -26,6 +27,26 @@ sub _err { [ 500, [ content_type => 'text/html' ], [ $_[1] ]] }
sub _forbidden { [ 403, [ content_type => 'text/html' ], [ $_[1] ]] }
sub _conflict { [ 409, [ content_type => 'text/html' ], [ $_[1] ]] }

has _xslate => (
is => 'ro',
lazy => 1,
default => sub {
Text::Xslate->new(
path => ['var/tx'],
type => 'text',
)
},
handles => {
_tx => 'render',
},
);

sub _html_200 {
my ($self, $t, $vars) = @_;

[ 200, [ content_type => 'text/html' ], [ $self->_tx("$t.tx.html", $vars) ] ]
}

sub dispatch_request {
my $self = shift;
'/all/...' => sub {
Expand All @@ -40,18 +61,16 @@ sub dispatch_request {
grep -l $_,
map "$_",
io->dir($self->_config->retropie_roms_dir, $system)->all;
my $html =
join "\n",
map {
my $fn = $_->filename;
"<li>$fn [" . (
$links{$_->name}
? '<strike>Pick</strike>'
: qq(<a href="/all/$system/$fn/pick">Pick</a>)
) . ']</li>'
} io->dir($self->_config->real_roms_dir, $system)->all;

return [ 200, [ content_type => 'text/html' ], [ "<html><ul>$html</ul></html>" ] ]

my @games = map +{
name => $_->filename,
linked => $links{$_->name}
}, io->dir($self->_config->real_roms_dir, $system)->all;

$self->_html_200('all_games', {
system => $system,
games => \@games,
})
},
'/*/...' => sub {
my ($game) = $_[1];
Expand Down Expand Up @@ -80,13 +99,11 @@ sub dispatch_request {
},
},
'/' => sub {
my $html =
join "\n",
map qq(<li><a href="/all/$_/">$_</a></li>),
my @systems =
map $_->filename,
io->dir($self->_config->real_roms_dir)->all;

return [ 200, [ content_type => 'text/html' ], [ "<html><ul>$html</ul></html>" ] ]
$self->_html_200('all_systems', { systems => \@systems })
},
},
'/selected/...' => sub {
Expand All @@ -98,13 +115,14 @@ sub dispatch_request {
return $self->_forbidden unless $system =~ m/^[\w]+$/;

'/' => sub {
my $html =
join "\n",
map qq(<li>$_ [<a href="/selected/$system/$_/unpick">Unpick</a>]</li>),
my @games =
map $_->filename,
io->dir($dir, $system)->all;

return [ 200, [ content_type => 'text/html' ], [ "<html><ul>$html</ul></html>" ] ]
$self->_html_200('selected_games', {
games => \@games,
system => $system,
});
},
'/*/...' => sub {
my ($game) = $_[1];
Expand Down Expand Up @@ -133,18 +151,14 @@ sub dispatch_request {
},
},
'/' => sub {
my $html =
join "\n",
map qq(<li><a href="/selected/$_/">$_</a></li>),
my @systems =
map $_->filename,
io->dir($self->_config->retropie_roms_dir)->all;

return [ 200, [ content_type => 'text/html' ], [ "<html><ul>$html</ul></html>" ] ]
$self->_html_200('selected_systems', { systems => \@systems })
},
},
'/' => sub {
return [ 200, [ content_type => 'text/html' ], [ q(<html><a href="/all/">all roms</a><br /><br /><a href="/selected/">selected roms</a></html>) ] ]
},
'/' => sub { $self->_html_200('index') },
}

1;
17 changes: 17 additions & 0 deletions var/tx/all_games.tx.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<html>
<body>
<ul>
: for $games -> $game {
<li>
<: $game.name :> [
: if $game.linked {
<strike>Unpick</strike>
: } else {
<a href="/all/<: $system :>/<: $game.name :>/unpick">Unpick</a>
: }
]
</li>
: }
</ul>
</body>
</html>
9 changes: 9 additions & 0 deletions var/tx/all_systems.tx.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<html>
<body>
<ul>
: for $systems -> $system {
<li><a href="/all/<: $system :>/"><: $system :></a></li>
: }
</ul>
</body>
</html>
7 changes: 7 additions & 0 deletions var/tx/index.tx.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<html>
<body>
<a href="/all/">all roms</a><br />
<br />
<a href="/selected/">selected roms</a>
</body>
</html>
13 changes: 13 additions & 0 deletions var/tx/selected_games.tx.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<html>
<body>
<ul>
: for $games -> $game {
<li>
<: $game :> [
<a href="/selected/<: $system :>/<: $game :>/unpick">Unpick</a>
]
</li>
: }
</ul>
</body>
</html>
9 changes: 9 additions & 0 deletions var/tx/selected_systems.tx.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<html>
<body>
<ul>
: for $systems -> $system {
<li><a href="/selected/<: $system :>/"><: $system :></a></li>
: }
</ul>
</body>
</html>

0 comments on commit e38f6b4

Please sign in to comment.