-
Notifications
You must be signed in to change notification settings - Fork 4
/
website.d
100 lines (87 loc) · 2.52 KB
/
website.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
import arsd.cgi;
import arsd.webtemplate;
import arsd.archive;
shared static this() {
import std.file;
import std.path;
chdir(thisExePath.dirName);
}
bool libraryHandler(DD)(DD dd) {
auto path = dd.cgi.pathInfo[dd.pathInfoStart + 1 .. $]; // slice off the starting /
if(path == "style.css") {
import std.file;
auto ls = readText("library-style.css");
dd.cgi.setResponseExpiresRelative(60 * 5, true);
dd.cgi.setResponseContentType("text/css");
dd.cgi.gzipResponse = true;
dd.cgi.write(ls[], true);
return true;
}
try {
auto arcz = new ArzArchive();
arcz.openArchive("generated.arcz");
auto fl = arcz.open(path);
auto buf = new char[](fl.size);
fl.rawRead(buf[]);
dd.cgi.setResponseExpiresRelative(60 * 5, true);
dd.cgi.gzipResponse = true;
dd.cgi.write(buf[], true);
return true;
} catch(Exception e) {
return false;
}
}
bool languageHandler(DD)(DD dd) {
auto path = dd.cgi.pathInfo[dd.pathInfoStart + 1 .. $]; // slice off the starting /
if(path == "style.css") {
import std.file;
auto ls = readText("library-style.css");
dd.cgi.setResponseExpiresRelative(60 * 5, true);
dd.cgi.setResponseContentType("text/css");
dd.cgi.gzipResponse = true;
dd.cgi.write(ls[], true);
return true;
}
try {
auto arcz = new ArzArchive();
arcz.openArchive("generated-language.arcz");
auto fl = arcz.open(path);
auto buf = new char[](fl.size);
fl.rawRead(buf[]);
dd.cgi.setResponseExpiresRelative(60 * 5, true);
dd.cgi.gzipResponse = true;
dd.cgi.write(buf[], true);
return true;
} catch(Exception e) {
return false;
}
}
mixin DispatcherMain!(
MyPresenter,
"/".serveRedirect("index.html"),
"/library".dispatchTo!libraryHandler,
"/language".dispatchTo!languageHandler,
"/".serveTemplateDirectory!wtrFactory(extension: "", templateDirectory: "html/"),
"/".serveStaticFileDirectory("assets/"),
);
WebTemplateRenderer wtrFactory(TemplateLoader loader) {
import arsd.dom;
return new WebTemplateRenderer(loader, [
"plaintext": function(string content, AttributesHolder attributes) {
import arsd.dom;
return WebTemplateRenderer.EmbeddedTagResult(new TextNode(content));
},
"markdown": function(string content, AttributesHolder attributes) {
import arsd.markdown;
import arsd.dom;
return WebTemplateRenderer.EmbeddedTagResult(
new Document("<div>" ~ convertMarkdownToHTML(content) ~ "</div>").root
);
}
]);
}
class MyPresenter : WebPresenterWithTemplateSupport!MyPresenter {
override WebTemplateRenderer webTemplateRenderer() {
return wtrFactory(templateLoader());
}
}