Added content, projects and dark mode

This commit is contained in:
Chris Josten 2020-06-24 15:53:18 +02:00
parent 4a9cfda0bd
commit e1b43bc4c3
23 changed files with 280 additions and 31 deletions

View file

@ -20,9 +20,12 @@ Project[string] projects;
immutable string articleSortPred = "a.firstPublished > b.firstPublished";
Article*[] articleList;
immutable string pageSortedPred = "a.title < b.title";
immutable string pageSortPred = "a.title < b.title";
Page*[] pageList;
Project[] projectList;
immutable string projectSortPred = "a.title < b.title";
Project*[] projectList;
enum OutputType {
HTML,
@ -95,12 +98,21 @@ void projectGetOverview(HTTPServerRequest req, HTTPServerResponse res) {
* Generate response for a page
*/
void pageGet(HTTPServerRequest req, HTTPServerResponse res) {
// If no slug is supplied, it will be adjusted to "index"
if (("slug" in req.params) is null) {
req.params.addField("slug", "index");
}
getSingle!(Page, "pages/page.dt")(pages, req, res);
}
/**
* Generate response for a project page
*/
void projectGet(HTTPServerRequest req, HTTPServerResponse res) {
res.headers["Cache-Control"] = "public";
getSingle!(Project, "pages/project.dt")(projects, req, res);
}
/**
* Generates response whenever an error occurs.
*/
@ -128,17 +140,21 @@ void main() {
router.get("/posts/", &articleGetOverview);
router.get("/palen/", &articleGetOverview);
router.get("/projects/", &projectGetOverview);
router.get("/projecten/", &projectGetOverview);
router.get("/projects/:slug", &projectGet);
router.get("/projecten/:slug", &projectGet);
router.get("/static/*", serveStaticFiles("./public/"));
router.get("/:slug", &pageGet);
router.get("/", &pageGet);
listenHTTP(settings, router);
runTask({
initPages!(Page, pageSortedPred)(pages, pageList, "pages");
initPages!(Page, pageSortPred)(pages, pageList, "pages");
});
runTask({
initPages!(Article, articleSortPred)(articles, articleList, "articles");
});
runTask({
initPages!(Project, projectSortPred)(projects, projectList, "projects");
});
runApplication();
}

View file

@ -5,17 +5,21 @@ import dyaml;
import vibe.vibe;
import page;
import staticpaths;
import utils;
/**
* Represents a project, like an unfinished application
*/
class Project : Page {
protected immutable string PROJECT_ICON_DIR = IMG_DIR ~ "projects/icons/";
protected immutable string PROJECT_IMAGE_DIR = IMG_DIR ~ "projects/images/";
protected string m_state;
protected string[] m_platforms;
protected string[] m_technologies;
protected string m_icon;
protected string[] m_images;
protected string m_description;
/**
* Creates a project from a file
@ -28,13 +32,12 @@ class Project : Page {
override protected void loadHeader(Node headerNode) {
super.loadHeader(headerNode);
this.m_state = headerNode.getOr!string("state", "unknown");
this.m_platforms = headerNode.getOr!(Node[])("platforms", [])
.map!(x => x.get!string).array;
this.m_technologies = headerNode.getOr!(Node[])("technologies", [])
.map!(x => x.get!string).array;
this.m_icon = headerNode.getOr!string("icon", "");
this.m_images = headerNode.getOr!(Node[])("images", [])
.map!(x => x.get!string).array;
this.m_platforms = headerNode.getArray!string("platforms", []);
this.m_technologies = headerNode.getArray!string("technologies", []);
this.m_icon = PROJECT_ICON_DIR ~ headerNode.getOr!string("icon", "");
this.m_images = headerNode.getArray!string("images", [])
.map!(x => PROJECT_IMAGE_DIR ~ x).array;
this.m_description = headerNode.getOr!string("description", "<no description>");
}
@property string state() { return m_state; }
@ -42,5 +45,6 @@ class Project : Page {
@property string[] technologies() { return m_technologies; }
@property string icon() { return m_icon; }
@property string[] images() { return m_images; }
@property string description() { return m_description; }
}

2
source/staticpaths.d Normal file
View file

@ -0,0 +1,2 @@
immutable string STATIC_DIR = "/static/";
immutable string IMG_DIR = STATIC_DIR ~ "img/";

View file

@ -1,3 +1,5 @@
import std.algorithm;
import std.array;
import std.conv;
import std.datetime;
@ -14,3 +16,12 @@ T getOr(T)(Node node, string key, T or) {
return or;
}
}
T[] getArray(T)(Node node, string key, T[] or = []) {
try {
return node.getOr!(Node[])(key, [])
.map!(x => x.as!T).array;
} catch (NodeException e) {
return or;
}
}