-
Notifications
You must be signed in to change notification settings - Fork 2
/
gen.p6
91 lines (71 loc) · 1.94 KB
/
gen.p6
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
use v6;
use BreakDancer;
use Template::Mojo;
class Link {
has $.location is rw;
has $.title is rw;
}
class Post {
has $.title is rw;
has $.location is rw;
has $.content is rw;
has $.date is rw;
}
my $path = 'www';
my $index_tmpl = slurp "tmpls/index.mojo";
my $post_tmpl = slurp "tmpls/blog/post.mojo";
my $menu = menu(get_items('sites'), 'site');
my @conts;
for get_items('sites') {
@conts.push: slurp ~$_;
}
my %sites = get_items('sites')>>.basename Z @conts;
@conts = ();
for get_items('blog') {
@conts.push: slurp ~$_;
}
my %posts = get_items('blog')>>.basename Z @conts;
gen '/', sub {
my $posts = blog(get_items('blog'));
$posts = Template::Mojo.new($post_tmpl).render($posts);
return Template::Mojo.new($index_tmpl).render($menu, $posts);
}
gen '/site', %sites, sub ($site, $content) {
return Template::Mojo.new($index_tmpl).render($menu, $content);
}
gen '/blog', %posts, sub ($post, $content) {
return Template::Mojo.new($index_tmpl).render($menu, $content);
}
sub get_items(Str $location) {
my @items = dir "data/$location";
return @items;
}
sub split(Str $path is copy) {
my $file = $path.substr($path.rindex('/')+1);
$path = $path.substr($path.index('/')+1, $path.rindex('/')-5);
return ($path, $file);
}
sub menu(@items, Str $p) {
my @ret;
@ret.push: Link.new(location => '', title => 'Strona główna');
for @items -> $i {
my ($l, $t) = split(~$i);
@ret.push: Link.new(location => "$p/$t", title => $t.tclc);
}
return @ret.item;
}
sub blog(@items) {
my @ret;
for @items -> $i {
my ($l, $t) = split(~$i);
my $post = Post.new(location => "$l/$t", title => $t);
my ($date, @content) = $i.IO.lines;
$post.date = $date;
$post.content = @content.join: "\n";
@ret.push: $post;
}
return @ret.item;
}
sub get_link($title) {
return $menu.list.grep({.title == $title})[0].location;
}