File Coverage

blib/lib/App/revealup/cli/serve.pm
Criterion Covered Total %
statement 24 60 40.0
branch 0 16 0.0
condition 0 15 0.0
subroutine 8 11 72.7
pod 0 2 0.0
total 32 104 30.7


line stmt bran cond sub pod time code
1             package App::revealup::cli::serve;
2 1     1   895 use App::revealup::base;
  1         16  
  1         5  
3 1     1   737 use Getopt::Long qw/GetOptionsFromArray/;
  1         9951  
  1         4  
4 1     1   703 use File::ShareDir qw/dist_dir/;
  1         23415  
  1         59  
5 1     1   721 use Path::Tiny qw/path/;
  1         10185  
  1         68  
6 1     1   538 use Plack::Runner;
  1         13033  
  1         33  
7 1     1   613 use Pod::Usage;
  1         50119  
  1         102  
8 1     1   480 use App::revealup::util;
  1         3  
  1         73  
9 1     1   390 use App::revealup::builder;
  1         2  
  1         502  
10              
11             has 'plack_port' => 5000;
12             has 'dry_run' => 0;
13             has 'theme';
14             has 'template';
15             has 'transition';
16             has 'width';
17             has 'height';
18             has 'theme_path';
19              
20             sub run {
21 0     0 0   my ($self, @args) = @_;
22 0           my $opt;
23             parse_options(
24             \@args,
25             'p|port=s' => \$opt->{plack_port},
26             'theme=s' => \$opt->{theme},
27             'template=s' => \$opt->{template},
28             'transition=s' => \$opt->{transition},
29             'width=i' => \$opt->{width},
30             'height=i' => \$opt->{height},
31             '_dry-run' => \$opt->{dry_run},
32 0           );
33              
34 0           for my $key (keys %$opt) {
35 0           $self->$key( $opt->{$key} );
36             }
37            
38 0           my $filename = shift @args;
39 0   0       my $builder = App::revealup::builder->new(
      0        
      0        
      0        
      0        
      0        
40             filename => $filename || '',
41             theme => $self->theme || '',
42             template => $self->template || '',
43             transition => $self->transition || '',
44             width => $self->width || 0,
45             height => $self->height || 0,
46             );
47 0           my $html = $builder->build_html();
48 0 0         if( !$html ) {
49 0           system "perldoc App::revealup::cli::serve";
50 0           exit;
51             }
52              
53 0           my $app = $self->app($html);
54 0           my $runner = Plack::Runner->new();
55 0           $runner->parse_options("--no-default-middleware");
56 0           $runner->set_options(port => $self->plack_port);
57 0 0         $runner->run($app) if !$self->dry_run
58             }
59              
60             sub app {
61 0     0 0   my ($self, $html) = @_;
62             return sub {
63 0     0     my $env = shift;
64 0 0         if ($env->{PATH_INFO} eq '/') {
65             return [
66 0           200,
67             ['Content-Type' => 'text/html', 'Content-Length' => length $html],
68             [$html]
69             ];
70             };
71              
72 0           my $path;
73             # theme
74 0 0 0       if($self->theme_path && $env->{PATH_INFO} =~ m!${self->theme_path}$!){
  0            
75 0 0         if($self->theme_path->exists) {
76 0           $path = path('.', $self->theme_path);
77             }else{
78 0           my $reveal_theme_path = App::revealup::util::share_path([qw/share revealjs dist theme/]);
79 0           $path = $reveal_theme_path->child($self->theme_path->basename);
80             }
81 0 0         return App::revealup::util::path_to_res($path) if $path->exists;
82             }
83            
84 0           $path = path('.', $env->{PATH_INFO});
85 0 0         return App::revealup::util::path_to_res($path) if $path->exists;
86              
87 0           my $reveal_dir = App::revealup::util::share_path([qw/share/]);
88 0           $path = $reveal_dir->child($env->{PATH_INFO});
89 0 0         return App::revealup::util::path_to_res($path) if $path->exists;
90 0           App::revealup::util::warn("$path does not exist");
91             return [
92 0           404,
93             ['Content-Type' => 'text/plain'],
94             ['Not Found']
95             ];
96 0           };
97             }
98              
99             1;
100              
101             __END__