Skip to content

Instantly share code, notes, and snippets.

@s-aska s-aska/app.psgi
Created Feb 18, 2011

Embed
What would you like to do?
Markdown Reader
use strict;
use Data::Section::Simple;
use Encode;
use JSON;
use Path::Class;
use Plack::Builder;
use Plack::Request;
use Text::Markdown;
use Text::Xslate;
my $doc_dir = dir('./doc/')->absolute;
my $app = sub {
my $req = Plack::Request->new(shift);
return [ 200, [ 'Content-Type' => 'text/plain' ], [ '' ] ]
if $req->path eq '/favicon.ico';
my $tx = Text::Xslate->new(
path => Data::Section::Simple->new()->get_data_section(),
module => ['Text::Xslate::Bridge::TT2Like'],
syntax => 'TTerse'
);
my $res = $req->new_response(200);
if ($req->path eq '/') {
my @dirs;
my @files;
$doc_dir->recurse(
depthfirst => 1,
callback => sub {
my $file = shift;
my $path = decode('utf8', $file);
$path=~s|^$doc_dir||;
return unless length $path;
$path = -f $file ? file($path) : dir($path);
push @files, $path;
}
);
@files = sort @files;
my $content = $tx->render('index.html', { files => \@files });
$res->content_type('text/html; charset=UTF-8');
$res->body(encode('utf8', $content));
}
else {
my $path = file($doc_dir, substr($req->path, 1))->absolute;
my $base_path = quotemeta $doc_dir;
die 'rel attach' unless $path=~/^$base_path/;
my $text = $path->slurp;
my $html = Text::Markdown->new->markdown($text);
$res->content_type('text/html; charset=UTF-8');
$res->body($html);
}
return $res->finalize;
};
builder {
enable 'Static',
path => qr!^/static!, root => './htdocs/';
$app;
};
__DATA__
@@ index.html
<html>
<head>
<title>Pickles</title>
<link rel="stylesheet" type="text/css" href="/static/css/style.css" media="all" />
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.0/jquery.min.js"></script>
<script>
function load_file(file) {
$.ajax({
url: file,
success: function(html){
$("#content").html(html);
}
});
}
function load_dir(dir) {
$.ajax({
url: 'dir/' + dir,
cache: false,
dataType: 'json',
success: function(data){
var html = '';
jQuery.each(data, function(i, file){
html = html + '<div><a onclick="load_file($(this).data().file)" data-file="' + dir + '/' + file + '" href="javascript:void(0)">' + file + '</a></div>';
});
$("#sidebar").html(html);
}
});
}
$(document).ready(function() {
$("#sidebar li.dir").hover(function(){
$(this).css("cursor","pointer");
},function(){
$(this).css("cursor","default");
});
$("#sidebar li.dir").click(function(){
$(this).next().slideToggle("fast");
});
$("#sidebar li.dir").each(function(){
// $(this).next().hide();
});
});
</script>
</head>
<body>
<h1>Pickles</h1>
<div id="sidebar">
[% SET dep = 0 %]
[% SET cur_dep = 0 %]
[% FOREACH file IN files %]
[% SET cur_dep = file.is_dir ? file.dir_list - 1 : file.dir.dir_list %]
[% IF dep < cur_dep %]
[% '<ul>'.repeat(cur_dep - dep) | mark_raw %][% SET dep = cur_dep %]
[% ELSIF dep > cur_dep %]
[% '</ul>'.repeat(dep - cur_dep) | mark_raw %][% SET dep = cur_dep %]
[% END %]
[% IF file.is_dir %]
<li class="dir">[% file.basename %]</li>
[% ELSE %]
<li><a onclick="load_file($(this).data().file)" data-file="[% file %]" href="javascript:void(0)">[% file.basename.replace('.txt$', '') %]</a></li>
[% END %]
[% END %]
[% '</ul>'.repeat(dep) | mark_raw %]
</div>
<div id="content"></div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.