-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmdwiki_
362 lines (279 loc) · 9.12 KB
/
mdwiki_
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
#!/usr/bin/env perl
use Mojolicious::Lite;
use Data::Dumper;
my $config = plugin 'Config';
get '/' => sub {
my $c = shift;
$c->render(template => 'index');
};
get '/edit/:slug' => sub {
my $c = shift;
my $slug = $c->param('slug');
my $md_raw = slurp_md($slug);
my $html = md_to_html ($md_raw);
$c->render(template => 'edit', md_raw => $md_raw, html => $html, 'slug' => $slug);
};
post '/edit/:slug' => sub {
my $c = shift;
my $slug = $c->param('slug');
#print $c->param('btn');
if ($c->param('btn') eq 'Preview') { #preview
my $md_raw = $c->param('md_raw');
my $html = md_to_html ($md_raw);
$c->render(template => 'edit', md_raw => $md_raw, html => $html, 'slug' => $slug);
};
if ($c->param('btn') eq 'Save') { #save
my $md_raw = $c->param('md_raw');
save_md($slug, $md_raw);
$c->flash( flash_ok => "Article saved");
$c->redirect_to("/$slug");
};
};
get '/login' => sub {
my $c = shift;
$c->redirect_to("/index") if ($c->session->{'user_name'});
$c->render(template => 'login');
};
post '/login' => sub {
my $c = shift;
my $pwc = $c->param('passcode');
my $user_name = $config->{'authors'}->{$pwc};
if ($user_name) {
$c->session->{'user_name'} = $user_name;
$c->flash( flash_ok => "You are logged in, $user_name");
$c->redirect_to("/index");
};
$c->flash( flash_not_ok => "Unknown passcode");
$c->render(template => 'login');
};
get '/logout' => sub {
my $c = shift;
my $user_name = $c->session->{'user_name'};
undef $c->session->{'user_name'};
$c->flash( flash_ok => "You are now logged out, $user_name");
$c->redirect_to("/index");
};
get '/new/:slug' => {slug => ''} , sub {
my $c = shift;
my $title = $c->param('slug');
my $slug_ = clean_up_slug($title);
my $html = '(click Preview button above to see your edits)';
return $c->render(template => 'new', title => $title, slug_ => $slug_);
};
app->validator->add_check(slug_is_valid => sub {
my ($validation, $name, $value) = @_;
return new_slug_is_valid($value);
});
post '/new' => sub {
my $c = shift;
my $slug = $c->param('slug');
my $title = $c->param('title');
my $validation = $c->validation;
return $c->render unless $validation->has_data;
$validation->required('slug')->size(3, 40)->slug_is_valid();
$validation->required('title')->size(3, 128);
return $c->render('new') if ($c->validation->has_error || ($c->param('btn') eq 'test_slug'));
if (($c->param('btn') eq 'create')) { #&& (!$c->validation->has_error)) {
my $md_raw = <<"EOT";
# $title
## First section goes here
* a bullet item
## Second section goes here
### a sub-section
// this line is only seen in edit mode, use as notes for author
EOT
$c->flash( flash_ok => 'Slug OK, add text below' );
$c->render( template => 'edit', title => $title, slug => $slug,
md_raw => $md_raw, html => '' );
};
}; #=> 'new';
get '/index' => sub {
my $c = shift;
my @slugs = @{all_slugs()}; # get filesnames without extension
my %firstlines;
foreach my $f (@slugs) {
open(my $fh, '<:encoding(UTF-8)', "/var/www/html/mdwiki/md/$f.md")
or die "cannot open < $f : $!";
my $firstline = <$fh>; #read 1st line
if ($firstline && $firstline =~ /^#.*/) { $firstlines{$f} = $firstline };
close $fh;
};
$c->render(template => 'index', slugs => \@slugs, firstlines => \%firstlines);
};
get '/:slug' => sub {
my $c = shift;
my $slug = $c->param('slug');
my $html = md_to_html(slurp_md($slug));
$c->render(template => 'wiki', 'html' => $html, 'slug' => $slug);
};
# ----------
sub new_slug_is_valid {
my $slug = shift;
my $existing_slugs = all_slugs();
my @reserved_words = qw(edit admin new login logout index about readme fuck);
push (@$existing_slugs, @reserved_words);
map {lc} @$existing_slugs;
my @hits = (grep {$_ eq lc $slug} @$existing_slugs);
#print Dumper @$existing_slugs;
#print Dumper @;
return scalar @hits;
}
sub clean_up_slug {
my $slug = shift;
$slug =~ s/[ ]+/_/g;
$slug =~ s/[^a-z0-9_-]+//ig;
#print "slug = $slug";
}
sub all_slugs {
opendir my $dir, "./md" or die "Cannot open directory : $!";
my @files = readdir $dir;
closedir $dir;
@files = grep { !/^\.{1,2}/ } @files; # remove . and ..
@files = grep { /\.md$/ } @files; # keep .md files only
map {$_ =~ s/(.*?)\.md$/$1/ } @files; # get filesnames without extension
return \@files;
}
sub slurp_md {
my $file_name = shift;
$file_name .= ".md";
open(my $fh, '<:encoding(UTF-8)', "/var/www/html/mdwiki/md/$file_name")
or die "cannot open < $file_name : $!";
local $/ = undef;
my $md = <$fh>; #slurp content
close $fh;
return $md;
}
sub save_md {
my $file_name = shift . ".md";
my $md = shift;
open (my $fh, ">:encoding(UTF-8)", "./md/$file_name") or die "cannot open file for writing $!";
print $fh $md;
close ($fh);
}
use Text::Markdown 'markdown';
sub md_to_html {
my $md = shift;
my $html = markdown($md);
$html =~ s/>http:\/\//>/g; #remove http:// from visible site name
return $html;
}
app->start;
__DATA__
@@ index.html.ep
% layout 'default';
% title 'Index of Pages';
<h2>Welcome to the Mojolicious real-time web framework!</h2>
% my @slugz = sort @$slugs;
<ul>
% for my $slug (@slugz) {
<li> <a href="/<%= $slug %>"><%= $slug %></a> : <%= $$firstlines{$slug} %> </li>
% };
<li> Special pages: </li>
% if (session 'user_name') {
<li> <a href="/new">Create New Page</a>
<li> <a href="/logout">Logout</a> <%= session ('user_name') %>
% } else {
<li> <a href="/login">Login</a>
% };
</ul>
@@ wiki.html.ep
% layout 'default';
% title 'wiki';
<a href="/edit/<%= $slug %>">Edit this page</a>
<article>
%== $html
</article>
@@ new.html.ep
% layout 'default', suppress_footer => 1;
% title 'new article';
%= form_for '/new' => (method => 'POST') => begin
<div>
% if (my $err = validation->error('slug')) {
<p>
%= 'At least 3 charactes are required.' if $err->[0] eq 'required';
%= 'Already in use' if $err->[0] eq 'is_valid_slug';
</p>
% };
</div>
%= label_for title => 'Title'
%= text_field 'title', size => "100"
%= label_for slug => 'Slug'
%= text_field 'slug', style => "display: inline-block", pattern => '[a-zA-Z0-9_.]+'
%= submit_button 'Test Slug', name => 'btn', value => 'test_slug', caption => 'Test Slug'
<br/>
%#= submit_button 'Save & Create Page', name => 'btn', value => 'create'
<button name='btn' value='create' type="submit">Save & Create Pag</button>
<a style="display: inline-block" href="/index">Cancel</a>
% end
@@ edit.html.ep
% layout 'default', suppress_footer => 1;
% title 'Edit page';
%= form_for "/edit/$slug" => (method => 'POST') => begin
%#= form_for '<%= url_for "/edit/$slug" %>' => (method => 'POST') => begin
%#= text_field 'title'
%#= text_field 'slug', value => $slug
%#= submit_button 'Test Slug', name => 'btn', value => 'test_slug'
<a href="/<%= $slug %>">Cancel</a> |
<a href="https://rawgit.com/fletcher/human-markdown-reference/master/index.html" target='_blank'>MD cheat sheet</a>
<button onclick="open_cs(); return false;">Mark Down Cheat Sheet</button>
<a href="#" onclick="open_cs();return 0">Mark Down Cheat Sheet</a>
<script>
function open_cs() {
var w = window.open("https://rawgit.com/fletcher/human-markdown-reference/master/index.html", "_blank", "toolbar=no, scrollbars=yes, status=no, menubar=no, location=no, width=480, height=480");
return w?false:true;
}
</script>
%= text_area 'md_raw' => $md_raw, rows => 40, cols => 96, style => "font-family:Consolas,Monaco,Lucida Console,Liberation Mono,DejaVu Sans Mono,Bitstream Vera Sans Mono,Courier New, monospace;"
%= submit_button 'Save', name => 'btn', value => 'Save'
%= submit_button 'Preview', name => 'btn', value => 'Preview'
% end
<a href="/<%= $slug %>">Cancel</a>
<hr/>
<article>
%== $html
</article>
@@ login.html.ep
% layout 'default';
% title 'Log In';
<%= session 'user_name' %>
<h2>Login with just the passcode you received</h2>
%= form_for "/login" => (method => 'POST') => begin
%= password_field 'passcode'
%= submit_button 'Login', name => 'btn', value => 'login'
%= end
@@ layouts/default.html.ep
<!DOCTYPE html>
<head>
<link rel="stylesheet" href="/tacit-small.css">
<style>
label.field-with-error { color: #dd7e5e }
input.field-with-error { background-color: #fd9e7e }
</style>
</head>
<html>
<head><title><%= title %></title></head>
<body style="font-size:0.8em">
% if ( my $message = flash 'flash_ok' ) {
<div style="border:1px solid #fc0; background: #fec; padding:1em;">
<%= b( $message )->decode('UTF-8')->to_string %>
</div>
% }
% if ( my $message = flash 'flash_not_ok' ) {
<div style="border:1px solid #f00; background: #F7B5B5; padding:1em;">
<%= b( $message )->decode('UTF-8')->to_string %>
</div>
% }
<div style="font-size:50%">
<%= content %></div></body>
% unless (defined stash('suppress_footer')) {
<footer>
<div style="border:1px solid #505050; background: #d1d1d1; padding:1em;">
[ <a href="/index">Index</a> | Readme | About
| template:
]
</div>
</footer>
% };
</html>
@@
# ex: set tabstop=2 shiftwidth=2 expandtab: