-
Notifications
You must be signed in to change notification settings - Fork 117
/
vm.pl
executable file
·172 lines (152 loc) · 3.47 KB
/
vm.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
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
#! /usr/bin/env perl
use strict;
use warnings;
use Text::Markdown qw(markdown);
use FindBin qw($Bin);
my $title = "no title";
my $body = "no body";
# 解析 GET 参数
my $query = $ENV{QUERY_STRING};
my %query = map {$1 => $2 if /(\w+)=(\S+)/} split('&', $query);
my $mdpath = $query{p};
response() unless $mdpath;
$mdpath = "$Bin/$mdpath.md";
response() unless -r $mdpath;
my $mdfile = prefile($mdpath);
my $text = join("", @{$mdfile->{content}});
# my $text = join("\n", @{$mdfile->{content}});
# <pre> will add anthor empty line
$title = $mdfile->{title} if $mdfile->{title};
$body = markdown($text, {tab_width => 2});
fixlink();
# postbody();
response();
# read markdown file, save some information with hash
# Text::Markdown fail to handle code block such as
# ```perl
# any code snippet
# ```
sub prefile
{
my ($filename) = @_;
my $filemark = {content => [], title => '', tags => [], };
my $codeblok = 0;
open(my $fh, '<', $filename) or die "cannot open $filename $!";
# local $/ = undef; $text = <$fh>;
while (<$fh>) {
# chomp;
# title line
if ($. == 1) {
push(@{$filemark->{content}}, $_);
(my $title = $_ ) =~ s/^[#\s]+//;
$filemark->{title} = $title;
next;
}
# tag line
elsif ($. == 2){
my @tags = /`([^`]+)`/g;
if (@tags) {
push(@{$filemark->{tags}}, @tags);
next;
}
}
# begin/end code block ```perl
if (/^\s*```(\S*)\s*$/) {
my $line = $_;
if (!$codeblok) {
$line = qq{<pre><code class="language-$1">};
$codeblok = 1;
}
else {
$line = qq{</code></pre>\n};
$codeblok = 0;
}
push(@{$filemark->{content}}, $line);
}
else {
push(@{$filemark->{content}}, $_);
}
}
close($fh);
return $filemark;
}
# 回应输出
sub response
{
print "Content-type:text/html\n\n";
print <<EndOfHTML;
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width" />
<link rel="stylesheet" type="text/css" href="/css/main.css">
<link rel="stylesheet" type="text/css" href="/css/markdown.css">
<title> $title </title>
</head>
<body>
$body
</body>
</html>
EndOfHTML
}
sub debug_query
{
$body = "";
while (my ($key, $value) = each %query) {
$body .= "$key => $value<br/>";
}
}
sub postbody
{
$body .= qq{<hr>\n};
for my $tag (@{$mdfile->{tags}}) {
$body .= qq{<code>$tag</code> };
}
}
# fix links from/to home content and prev/next pages
sub fixlink
{
my $base = $ENV{SCRIPT_NAME};
my $subname = $query{p};
if ($subname =~ /^content$/) {
$body =~ s/href="(.+)\.md"/href="$base?p=$1"/g;
}
else {
my ($prev, $next) = findprenext($subname);
my @foot = ();
push(@foot, "<hr/>");
$prev ? push(@foot, qq{<a href="$base?p=$prev">Prev</a>})
: push(@foot, qq{<a>First</a>});
push(@foot, qq{<a href="$base?p=content">| Home |</a>});
$next ? push(@foot, qq{<a href="$base?p=$next">Next</a>})
: push(@foot, qq{<a>Last</a>});
$body .= sprintf(qq{\n<div class="foot-link"> %s \n</div>\n}, join("\n", @foot));
}
}
# read content.md, find the prev/next page
sub findprenext
{
my $curname = shift;
my ($prev, $next) = ('', '');
my $tocname = "$Bin/content.md";
return ($prev, $next) unless -f $tocname;
open(my $fh, '<', $tocname) or die "cannot open $tocname $!";
my ($prev_may, $cur_may);
while (<$fh>) {
chomp;
next unless /\((\S+)\.md\)/;
$prev_may = $cur_may;
$cur_may = $1;
if ($cur_may eq $curname) {
$prev = $prev_may;
next;
}
if ($prev_may eq $curname) {
$next = $cur_may;
last;
}
}
close($fh);
return ($prev, $next);
}