-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdoc
executable file
·46 lines (38 loc) · 925 Bytes
/
doc
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
#!/usr/bin/perl
use strict;
use warnings;
use CGI 'header';
use Text::MultiMarkdown 'markdown';
main();
#---------------------------------------------------
sub request {
# Given an HTTP document request, determine which file to return
my $file = 'docs' . shift;
for('.md', '/index.md'){
return($file.$_,200) if -e $file.$_;
}
return '404.md',404;
}
sub getfile {
# Return the contents of a given file
my $file = shift;
return unless -e $file;
open(IN, $file) or return;
my $out = join('', <IN>);
close IN;
return $out;
}
sub main {
my $path = $ENV{PATH_INFO} || '';
my ($file, $status) = request($path);
my $content = markdown( getfile $file );
my ($title) = $content =~ /<h1[^>]*>(.+)<\/h1>/;
my $tmpl = getfile('tmpl.html');
$tmpl =~ s/CONTENT/$content/s;
$tmpl =~ s/TITLE/$title/s;
print header(
-status => $status,
-charset => 'UTF-8',
);
print $tmpl;
}