-
Notifications
You must be signed in to change notification settings - Fork 0
/
polka.pl
executable file
·58 lines (50 loc) · 1.58 KB
/
polka.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#!/usr/bin/perl
use Dancer;
use File::Find;
use Text::Markdown 'markdown';
use utf8;
my %cache;
my $dirtree = dirtree( 'data' );
sub slurp { local $/; open(my $fh, '<:utf8', shift); <$fh> }
sub cache { my $f = shift; $cache{$f} ||= slurp $f }
sub dirtree {
my $dir = shift;
my %tree;
opendir(DIR, $dir);
my @files = grep {!/^\.\.?$/} readdir(DIR);
closedir(DIR);
$tree{"$dir/$_"} = -d "$dir/$_" ? dirtree("$dir/$_") : '' for @files;
\%tree;
}
sub menu {
my ($tree, $path, $unfolded) = @_;
my $menu = $unfolded ? "<ul class=\"active\">\n" : "<ul>\n";
while ( my ($link, $child) = each %$tree ) {
$link =~ s/^data\///;
my $label = pop @{[ split '/', $link ]};
if ( $child ) {
$menu .= "<li class=\"dir\">$label\n".menu($child, $path, $path =~ /$link/)."</li>\n";
} else {
my $class = $link eq $path ? ' class="active"' : '';
$menu .= "<li$class><a href=\"/$link\">$label</a></li>\n";
}
}
$menu .= "</ul>\n";
}
get qr{/(?<path>.*)} => sub {
captures->{path} ||= 'Home';
"<html>
<head>
<link href=\"/style.css\" rel=\"stylesheet\" type=\"text/css\" />
</head>
<body>
<div>
<h1><a href=\"/\"><span id=\"main-title\">Polka~</a></h1>
<nav>" . menu( $dirtree, captures->{path} ) . "</nav>
<div id=\"page\">" . markdown( cache 'data/'.captures->{path} ) . "</div>
</div>
<footer>Powered by <a href=\"#\">Polka</a>.</footer>
</body>
</html>";
};
dance;