/
Rhetoric.pm
311 lines (241 loc) · 6.16 KB
/
Rhetoric.pm
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
package Rhetoric;
use common::sense;
use aliased 'Squatting::H';
use Squatting;
use Try::Tiny;
use Memoize;
use Rhetoric::Helpers ':all';
our $VERSION = '0.01';
# global config for our blogging app
our %CONFIG = (
'theme' => 'BrownStone', # Rhetoric::Theme::____
'time_format' => '%b %e, %Y %I:%M%P',
'posts_per_page' => 8,
'storage' => 'File', # Rhetoric::Storage::____
'storage.file.path' => '/tmp/rhetoric',
);
# TODO - divorce Continuity
# TODO - marry Plack
sub continue {
my $app = shift;
$app->next::method(
docroot => "./share/theme/brownstone",
staticp => sub { $_[0]->url =~ m/\.(jpg|jpeg|gif|png|css|ico|js|swf)$/ },
@_
);
}
# service() is run on every request (just like in Camping).
sub service {
my ($class, $c, @args) = @_;
my $v = $c->v;
my $s = $v->{storage} = storage();
H->bless($v);
H->bless($c->input);
$v->{title} = $s->meta('title');
$v->{subtitle} = $s->meta('subtitle');
$v->{description} = $s->meta('description');
$v->{menu} = $s->menu;
$v->{request_path} = $c->env->{REQUEST_PATH};
$v->{time_format} = $CONFIG{time_format};
if (exists $CONFIG{relocated}) {
for (@{ $v->menu }) {
$_->url($CONFIG{relocated} . $_->url);
}
}
$class->next::method($c, @args);
}
sub init {
my ($class) = @_;
# TODO - Make absolutely sure the Page controller is at $C[-1].
if ($Rhetoric::Controllers::C[-1]->name ne 'Page') {
# find index of Page controller
# splice it out
# push it back on to the end
}
$class->next::method();
}
memoize('storage');
# Return an object that handles the storage for blog data based on
# what $CONFIG{storage} dictates.
sub storage {
no strict 'refs';
my $impl = shift || $Rhetoric::CONFIG{'storage'};
my $path = "Rhetoric/Storage/$impl.pm";
my $package = "Rhetoric::Storage::$impl";
require($path); # let it die if it fails.
return ${"${package}::storage"};
}
#_____________________________________________________________________________
package Rhetoric::Controllers;
use common::sense;
use aliased 'Squatting::H';
use Method::Signatures::Simple;
use Rhetoric::Helpers ':all';
use Data::Dump 'pp';
our @C = (
C(
Home => [ '/', '/page/(\d+)' ],
get => method($page) {
my $v = $self->v;
my $storage = $v->storage;
$v->{posts} = $storage->posts($CONFIG{posts_per_page});
$self->render('index');
},
),
C(
Post => [ '/(\d+)/(\d+)/([\w-]+)' ],
get => method($year, $month, $slug) {
my $v = $self->v;
my $storage = $v->storage;
$v->{post} = $storage->post($year, $month, $slug);
$v->{comments} = $storage->comments($v->{post});
$self->render('post');
},
post => method($year, $month, $slug) {
my $v = $self->v;
my $storage = $v->storage;
my $post = $v->{post} = $storage->post($year, $month, $slug);
# XXX - modify post and redirect
}
),
# Need to be able to create posts from a form too, right?!
C(
NewPost => [ '/post' ],
get => method {
$self->render('new_post');
},
post => method {
},
),
C(
NewComment => [ '/comment' ],
post => method {
my $input = $self->input;
my $year = $input->{year};
my $month = $input->{month};
my $slug = $input->{slug};
my $name = $input->{name};
my $email = $input->{email};
my $url = $input->{url};
my $body = $input->{body};
my $storage = $self->v->storage;
my $result = $storage->new_comment($year, $month, $slug, {
name => $name,
email => $email,
url => $url,
body => $body
});
if ($result->success) {
$self->redirect(R('Post', $year, $month, $slug));
} else {
# TODO - put errors in session
$self->redirect(R('Post', $year, $month, $slug));
}
}
),
C(
Category => [ '/category/([\w-]+)' ],
get => method($category) {
my $storage = Rhetoric::storage();
my $v = $self->v;
$v->posts = $storage->category_posts($category);
$self->render('category');
}
),
C(
Env => [ '/env' ],
get => method {
use Data::Dump 'pp';
$self->headers->{'Content-Type'} = 'text/plain';
return pp($self->env);
}
),
C(
Theme => [ '/theme' ],
get => method {
return $self->env->{HTTP_HOST} . " => $CONFIG{theme}\n";
}
),
# Everything else that's not static is a page to be rendered through the view.
# This controller has to be last!
C(
Page => [ '/(.*)' ],
get => method($path) {
if ($path =~ /\.\./) {
$self->status = 404;
return "GTFO";
}
my $v = $self->v;
$self->render($path);
}
),
);
#_____________________________________________________________________________
package Rhetoric::Views;
use Method::Signatures::Simple;
use Template;
use Data::Dump 'pp';
our $tt = Template->new({
INCLUDE_PATH => [ '.', './share/theme/BrownStone' ],
POST_CHOMP => 1,
});
our @V = (
V(
'tt',
layout => method($v, $content) {
my $output;
$v->{R} = \&R;
$v->{content} = $content;
$tt->process('layout.html', $v, \$output);
$output;
},
_ => method($v) {
my $file = "$self->{template}.html";
my $output;
$v->{R} = \&R;
my $r = $tt->process($file, $v, \$output);
warn $r unless ($r);
$output;
},
)
);
1;
=head1 NAME
Rhetoric - a simple blogging system for perl
=head1 SYNOPSIS
Running a blog
$ squatting Rhetoric
Playing with the interactive console
$ squatting -C Rhetoric
\%Rhetoric::CONFIG
$s = Rhetoric::storage()
$s->metadata('title');
$s->recent_posts()
=head1 DESCRIPTION
A simple and straight-forward blogging system for Perl.
=head1 API
=head2 Home
/
/page/(\d+)
=head3 get
=head2 Post
/(\d+)/(\d+)/(\w+)
=head3 get
=head2 NewPost
/post
=head3 get
=head3 post
=head2 Comment
/comment
=head3 post
=head2 Category
/category/(\w+)
=head3 get
=head2 EverythingElse
/(.*)
=head3 get
=head1 AUTHOR
John BEPPU E<lt>beppu@cpan.orgE<gt>
=head1 COPYRIGHT
GPL
=cut