App-mookview
view release on metacpan or search on metacpan
lib/App/mookview.pm view on Meta::CPAN
package App::mookview;
use 5.008005;
use strict;
use warnings;
use Plack::Request;
use Path::Tiny qw/path/;
use Text::Markdown::Discount qw/markdown/;
use Text::Xslate qw/mark_raw/;
use Number::Format qw/format_number/;
use File::ShareDir qw/dist_dir/;
use Plack::App::Directory;
use Try::Tiny;
use Encode;
our $VERSION = "0.03";
sub new {
my $class = shift;
my $self = bless {}, $class;
my $name = shift or return;
my $file_path = path($name);
return unless $file_path->is_file();
$self->{file_path} = $file_path;
$self->{xslate} = Text::Xslate->new(
path => $self->local_or_share_path( [qw/share templates/] )
);
return $self;
}
sub local_or_share_path {
my ($self, $p) = @_;
my $path = path(@$p);
return $path if $path->exists;
try {
shift @$p;
$path = path(dist_dir('App-mookview'), @$p);
};
return $path;
}
sub psgi_app {
my $self = shift;
return sub {
my $req = Plack::Request->new(shift);
my $path = $req->path_info;
return $self->return_markdown($path) if $path eq '/';
return $self->return_css($path) if $path =~ m!^/css/.+!;
Plack::App::Directory->new->to_app->($req->env);
};
}
sub return_404 {
return [404, [ 'Content-Type' => 'text/plain' ], ['Not Found'] ];
}
sub return_css {
my ($self, $path) = @_;
return $self->return_404 unless $path;
my ($name) = $path =~ m!/([^/]+?)$!;
my $local_path = $self->local_or_share_path([qw/share static css/, $name]);
return $self->return_404 unless $local_path;
my $css = $local_path->slurp();
return [200, [ 'Content-Type' => 'text/css', 'Content-Length' => length $css ], [ encode_utf8($css) ] ];
}
sub return_markdown {
my ($self, $path) = @_;
my $text = $self->{file_path}->slurp_utf8();
my $length = format_number(length $text);
$text = $self->filter_markdown($text);
my $stock = ''; my $page = 1; my $content = '';
my $limit = 1100;
( run in 3.117 seconds using v1.01-cache-2.11-cpan-39bf76dae61 )