-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathrender.d
289 lines (236 loc) · 7.66 KB
/
render.d
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
// Copyright (C) 2014-2020 Iain Buclaw
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 3.0 of the License, or (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
// You should have received a copy of the GNU Lesser General Public
// License along with this program; if not, see
// <http://www.gnu.org/licenses/lgpl-3.0.txt>.
// The gdcproject website powered by vibe.d
// This file builds (and optionally caches) pages to be sent to the client.
module gdcproject.render;
import vibe.inet.path;
import vibe.core.file;
import vibe.db.redis.redis;
import gdcproject.downloads;
// Read and return as a string a dynamically loaded template from 'path'.
// If not found, the value of 'notfound' is used inplace of the file.
// The template is assumed to be in html format.
string readTemplate(string path, lazy string notfound)
{
import std.array : appender;
import std.algorithm : findSplit, strip;
// Catch recursively included templates.
static bool[string] rendering;
if (rendering.get(path, false))
return "<!-- RECURSIVE " ~ path ~ " -->";
rendering[path] = true;
scope(exit) rendering[path] = false;
// Read and filter the contents for any sub-templates to include.
string tmpl = readContentsOrNotFound(path, notfound);
auto content = appender!string;
content.reserve(tmpl.length);
while (tmpl.length != 0)
{
// Split up as ['... content', '{{', 'template ...']
auto s1 = findSplit(tmpl, "{{");
// No match for '{{', reached end of template.
if (s1[1].length == 0)
{
content ~= tmpl;
break;
}
// Split up as ['... template', '}}', 'content...']
auto s2 = findSplit(s1[2], "}}");
// No match for '}}', write content unfiltered.
if (s2[1].length == 0)
{
content ~= tmpl;
break;
}
// Write content before '{{'
content ~= s1[0];
// Write contents of sub-template.
string incpath = s2[0].strip(' ');
content ~= readTemplate(incpath, "<!-- NOT FOUND " ~ incpath ~ " -->");
// Still need to process content after '}}'
tmpl = s2[2];
}
return content.data;
}
// Read and return as a string the (hard coded) header template.
// The template is assumed to be in html format.
string readHeader()
{
return readTemplate("templates/header.inc", "<html><body>");
}
// Read and return as a string the (hard coded) footer template.
// The template is assumed to be in html format.
string readFooter()
{
return readTemplate("templates/footer.inc", "</body></html>");
}
// Read return as a string the contents of the file from 'path'.
string readContents(string path)
{
import std.file : read;
return cast(string) read(path);
}
// Same as readContents, except returns 'notfound' on failure.
string readContentsOrNotFound(string path, lazy string notfound)
{
scope(failure) return notfound;
return readContents(path);
}
string generateTOC(string data)
{
import vibe.textfilter.markdown : getMarkdownOutline;
import std.array : appender;
auto content = appender!string();
content ~= `<nav class="toc">`;
void generateSubSections(T)(T sections)
{
content ~= "<ul>\n";
foreach (section; sections)
{
content ~= `<li><a href="#`;
content ~= section.anchor;
content ~= `">`;
content ~= section.caption;
content ~= "</a>\n";
generateSubSections(section.subSections);
content ~= "</li>";
}
content ~= "</ul>\n";
}
generateSubSections(getMarkdownOutline(data));
content ~= "</nav>\n";
return content.data;
}
// Render the page contents to send to client.
string renderPage(string path, string function(string) read, bool nocache = false)
{
import std.array : appender;
import vibe.textfilter.markdown : filterMarkdown;
// First attempt to get from cache.
if (!nocache)
{
scope(failure) goto Lnocache;
RedisClient rc = connectRedis("127.0.0.1");
RedisDatabase rdb = rc.getDatabase(0);
string content = rdb.get!string(path);
rc.quit();
if (content != null)
return content;
}
Lnocache:
auto content = appender!string();
auto data = read(path);
content ~= readHeader();
content ~= generateTOC(data);
content ~= filterMarkdown(data);
content ~= readFooter();
return content.data;
}
// Watch the views directory, recompiling pages when a change occurs.
// Uses Redis as the database backend.
void waitForViewChanges()
{
import core.thread : Thread;
import core.time : seconds;
scope(failure) return;
DirectoryWatcher watcher = Path("views").watchDirectory(true);
while (true)
{
DirectoryChange[] changes;
if (watcher.readChanges(changes, 0.seconds))
{
RedisClient rc = connectRedis("127.0.0.1");
RedisDatabase rdb = rc.getDatabase(0);
foreach (change; changes)
{
string path = change.path.toNativeString();
// Check if one of the downloads templates changed.
// Don't handle delete signals.
if ((path.length > 15 && path[$-15..$] == "/downloads.json")
|| (path.length > 19 && path[$-19..$] == "/downloads.mustache"))
{
path = (path[$-1] == 'n') ? path[0..$-5] : path[0..$-9];
string content = renderPage(path, &renderOldDownloadsPage, true);
rdb.set(path, content);
}
// Should be a markdown file.
if (path.length <= 9 || path[$-3..$] != ".md")
continue;
// Add or remove pages on the fly.
if (change.type == DirectoryChangeType.added
|| change.type == DirectoryChangeType.modified)
{
string content = renderPage(path, &readContents, true);
rdb.set(path, content);
}
else if (DirectoryChangeType.removed)
rdb.del(path);
}
rc.quit();
}
Thread.sleep(5.seconds);
}
}
// Watch the templates directory, rebuilding all pages when a change occurs.
void waitForTemplateChanges()
{
import core.thread : Thread;
import core.time : seconds;
scope(failure) return;
DirectoryWatcher watcher = Path("templates").watchDirectory(false);
while (true)
{
DirectoryChange[] changes;
if (watcher.readChanges(changes, 0.seconds))
{
// Check the name of the file changed, only need to rebuild
// if either the header or footer change.
foreach (change; changes)
{
string path = change.path.toNativeString();
if (path.length == 20
&& (path == "templates/header.inc" || path == "templates/footer.inc"))
{
buildCache();
break;
}
}
}
Thread.sleep(5.seconds);
}
}
// Render and cache all pages. This is called on application start-up,
// and when a change occurs to a header/footer template.
void buildCache()
{
import std.file : dirEntries, SpanMode;
scope(failure) return;
RedisClient rc = connectRedis("127.0.0.1");
RedisDatabase rdb = rc.getDatabase(0);
// Build all markdown pages.
auto de = dirEntries("views", "*.md", SpanMode.depth, false);
foreach (path; de)
{
string content = renderPage(path, &readContents, true);
rdb.set(path, content);
}
// Build (historical) downloads page.
de = dirEntries("views", "downloads.mustache", SpanMode.depth, false);
foreach (path; de)
{
string downloadsPath = path[0..$-9];
string content = renderPage(downloadsPath, &renderOldDownloadsPage, true);
rdb.set(path, content);
}
rc.quit();
}