Use translations, fix 'old' style.
This commit is contained in:
parent
ed2926fb16
commit
75a4e86ea1
15 changed files with 103 additions and 139 deletions
104
source/app.d
104
source/app.d
|
|
@ -75,48 +75,53 @@ void addCachingHeader(bool publicCache = true)(ref HTTPServerResponse res) {
|
|||
res.headers["Cache-Control"] = header;
|
||||
}
|
||||
|
||||
/**
|
||||
* Template method for fetching a single page for a subclass of page.
|
||||
* Params:
|
||||
* T = the data structure/class to be passed as template parameter. Must have a slug parameter.
|
||||
* templ = The template to use when rendering.
|
||||
* array = An associative array where the keys are slugs for parameters and the values the template parameter.
|
||||
* req = The server request to consume. Assumes there is an slug parameter.
|
||||
* res = The server response to write to.
|
||||
*
|
||||
*/
|
||||
void getSingle(T, string templ)(ref T[string] array, HTTPServerRequest req, HTTPServerResponse res) {
|
||||
string slug = req.params["slug"];
|
||||
OutputType outputType = getOutputType(slug);
|
||||
|
||||
enforceHTTP(slug in array, HTTPStatus.notFound, "Page not found");
|
||||
T content = array[slug];
|
||||
switch(outputType) with (OutputType) {
|
||||
case MARKDOWN:
|
||||
res.writeBody(content.contentSource, MIME_MARKDOWN);
|
||||
break;
|
||||
default:
|
||||
case HTML:
|
||||
res.render!(templ, content);
|
||||
break;
|
||||
|
||||
struct TranslateContext {
|
||||
import std.typetuple;
|
||||
|
||||
alias languages = TypeTuple!("en_GB", "nl_NL");
|
||||
mixin translationModule!"mijnblog";
|
||||
static string determineLanguage(scope HTTPServerRequest req) {
|
||||
if ("lang" !in req.query) return req.determineLanguageByHeader(languages); // default behaviour using "Accept-Language" header
|
||||
return req.query.get("lang", "en_GB");
|
||||
}
|
||||
}
|
||||
|
||||
struct TranslationContext {
|
||||
import std.typetuple;
|
||||
enum enforceExistingKeys = true;
|
||||
alias languages = TypeTuple!("en_GB", "nl_NL");
|
||||
mixin translationModule!"mijnblog";
|
||||
}
|
||||
/**
|
||||
* Generates boilerplate code for a single response.
|
||||
* params:
|
||||
* arrayName = The name of the associative array to take the items from.
|
||||
* templateName = The name of the template to render.
|
||||
*/
|
||||
string singleResponseMixin(string arrayName, string templateName) {
|
||||
return `string slug = req.params["slug"];
|
||||
OutputType outputType = getOutputType(slug);
|
||||
|
||||
@translationContext!TranslationContext
|
||||
enforceHTTP(slug in ` ~ arrayName ~ `, HTTPStatus.notFound, "Page not found");
|
||||
auto content = ` ~ arrayName ~ `[slug];
|
||||
switch(outputType) with (OutputType) {
|
||||
case MARKDOWN:
|
||||
res.writeBody(content.contentSource, MIME_MARKDOWN);
|
||||
break;
|
||||
default:
|
||||
case HTML:
|
||||
render!("` ~ templateName ~ `", content);
|
||||
break;
|
||||
}`;
|
||||
}
|
||||
|
||||
@translationContext!TranslateContext
|
||||
class MijnBlog {
|
||||
|
||||
public:
|
||||
/**
|
||||
* Generates response for /posts/:slug and /palen/:slug.
|
||||
*/
|
||||
@path("/posts/:slug")
|
||||
void getArticleSingle(string _slug, HTTPServerRequest req, HTTPServerResponse res) {
|
||||
getSingle!(Article, "pages/article.dt")(articles, req, res);
|
||||
//getSingle!(Article, "pages/article.dt")(articles, req, res);
|
||||
mixin(singleResponseMixin("articles", "pages/article.dt"));
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -125,7 +130,7 @@ class MijnBlog {
|
|||
@path("/posts/")
|
||||
void getArticleOverview(HTTPServerRequest req, HTTPServerResponse res) {
|
||||
addCachingHeader(res);
|
||||
render!("pages/article-list.dt", articleList)(res);
|
||||
render!("pages/article-list.dt", articleList);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -134,18 +139,17 @@ class MijnBlog {
|
|||
@path("/projects/")
|
||||
void getProjectOverview(HTTPServerRequest req, HTTPServerResponse res) {
|
||||
addCachingHeader(res);
|
||||
render!("pages/project-list.dt", projectList)(res);
|
||||
render!("pages/project-list.dt", projectList);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Generate response for a project page
|
||||
*/
|
||||
@path("/projects/:slug")
|
||||
void getProject(HTTPServerRequest req, HTTPServerResponse res) {
|
||||
res.headers["Cache-Control"] = "public";
|
||||
getSingle!(Project, "pages/project.dt")(projects, req, res);
|
||||
//getSingle!(Project, "pages/project.dt")(projects, req, res);
|
||||
mixin(singleResponseMixin("projects", "pages/project.dt"));
|
||||
}
|
||||
/**
|
||||
* Generate response for a page
|
||||
|
|
@ -153,7 +157,8 @@ class MijnBlog {
|
|||
@path("/:slug")
|
||||
void getPage(HTTPServerRequest req, HTTPServerResponse res) {
|
||||
addCachingHeader(res);
|
||||
getSingle!(Page, "pages/page.dt")(pages, req, res);
|
||||
//getSingle!(Page, "pages/page.dt")(pages, req, res);
|
||||
mixin(singleResponseMixin("pages", "pages/page.dt"));
|
||||
}
|
||||
|
||||
@path("/")
|
||||
|
|
@ -161,8 +166,20 @@ class MijnBlog {
|
|||
addCachingHeader(res);
|
||||
// If no slug is supplied, it will be adjusted to "index"
|
||||
req.params.addField("slug", "index");
|
||||
getSingle!(Page, "pages/page.dt")(pages, req, res);
|
||||
}
|
||||
//getSingle!(Page, "pages/page.dt")(pages, req, res);
|
||||
mixin(singleResponseMixin("pages", "pages/page.dt"));
|
||||
}
|
||||
|
||||
/**
|
||||
* Template method for fetching a single page for a subclass of page.
|
||||
* Params:
|
||||
* T = the data structure/class to be passed as template parameter. Must have a slug parameter.
|
||||
* templ = The template to use when rendering.
|
||||
* array = An associative array where the keys are slugs for parameters and the values the template parameter.
|
||||
* req = The server request to consume. Assumes there is an slug parameter.
|
||||
* res = The server response to write to.
|
||||
*
|
||||
*/
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -189,15 +206,6 @@ void main() {
|
|||
URLRouter router = new URLRouter;
|
||||
router.get("/static/*", serveStaticFiles("./public/", fSettings));
|
||||
router.registerWebInterface(new MijnBlog);
|
||||
/*router.get("/posts/:slug", &articleGetSingle);
|
||||
router.get("/palen/:slug", &articleGetSingle);
|
||||
router.get("/posts/", &articleGetOverview);
|
||||
router.get("/palen/", &articleGetOverview);
|
||||
router.get("/projects/", &projectGetOverview);
|
||||
router.get("/projects/:slug", &projectGet);
|
||||
router.get("/projecten/:slug", &projectGet);
|
||||
router.get("/:slug", &pageGet);
|
||||
router.get("/", &pageGet);*/
|
||||
|
||||
listenHTTP(settings, router);
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue