From fb7e421b9efea0a96adcf30d30cc2d318980d928 Mon Sep 17 00:00:00 2001 From: Alan Pearce Date: Sat, 1 Jul 2017 11:49:18 +0200 Subject: Switch templating to hyperfast This also means that highland is not (currently) required --- src/responders.js | 116 ++++++++++++++++++++++-------------------------------- 1 file changed, 47 insertions(+), 69 deletions(-) (limited to 'src/responders.js') diff --git a/src/responders.js b/src/responders.js index e472579..9633e15 100644 --- a/src/responders.js +++ b/src/responders.js @@ -1,13 +1,8 @@ "use strict"; -const h = require("highland"); const fs = require("fs"); -const rheo = require("rheo"); +const hyperfast = require("hyperfast"); const indent = require("indent-string"); -const PassThrough = require("stream").PassThrough; - -const toLines = string => - string.split("\n").map((s, i, arr) => (i === arr.length - 1 ? s : s + "\n")); const getTemplate = name => fs.readFileSync(`${__dirname}/templates/${name}.html`, "utf8"); @@ -15,21 +10,15 @@ const getTemplate = name => const findMain = /^(\s+)
h(content); -} - -function prependDoctype(stream) { - return h([""]).concat(stream).pipe(new PassThrough()); + return indentForTemplate(getTemplate(template), indentLevel); } const templates = { @@ -39,29 +28,27 @@ const templates = { taxon: templateReader("taxon", baseIndentLevel) }; -function setTitle(siteTitle, pageTitle) { - return rheo.template(function(s) { - return s - .inner( - "title", - rheo(pageTitle ? `${pageTitle} · ${siteTitle}` : siteTitle) - ) - .inner("body header h1", rheo(siteTitle)); - }); -} - -function renderPostListItem(ctx) { - return function(template, [, post]) { - return template - .attribute("a", "href", () => ctx.getURL("post", post.basename)) - .inner("a", () => rheo(post.data.get("title"))); - }; +function title(siteTitle, pageTitle) { + return pageTitle ? `${pageTitle} · ${siteTitle}` : siteTitle; } -function showPage(name) { - return function(els) { - return rheo(templates[name]()); - }; +const renderPostListItem = ctx => post => ({ + a: { + href: ctx.getURL(post, post.basename), + _text: post.data.get("title") + } +}); + +function layout(config, pageTitle, pageElement) { + return hyperfast(templates.layout, { + title: title(config.site.author.name, pageTitle), + "body > header .p-name": config.site.author.name, + "body > header .u-photo": { + alt: config.site.author.name, + src: config.site.author.photo + }, + "body > main": pageElement + }).outerHTML.trim(); } module.exports = { @@ -69,51 +56,42 @@ module.exports = { postIndentLevel, indentForTemplate, - home(ctx, config, postsStream) { + home(ctx, config, posts) { ctx.type = "html"; - ctx.body = prependDoctype( - templates - .layout() - .pipe(rheo()) - .attribute(".u-photo", "alt", config.site.author.name) - .attribute(".u-photo", "src", config.site.author.photo) - .outer("main", showPage("home")) - .inner(".posts", function(postsTemplate) { - return postsStream.pipe(postsTemplate.map(renderPostListItem(ctx))); - }) - .pipe(setTitle(config.site.author.name)) - .render() + ctx.body = layout( + config, + null, + hyperfast(templates.home, { + ".post": posts.map(renderPostListItem(ctx)) + }) ); }, post(ctx, config, post) { ctx.type = "html"; - ctx.body = prependDoctype( - templates - .layout() - .pipe(rheo()) - .outer("main", showPage("post")) - .inner("article h1", rheo(post.data.get("title"))) - .outer("article main", rheo(post.body)) - .pipe(setTitle(config.site.author.name, post.data.get("title"))) - .render() + + ctx.body = layout( + config, + post.data.get("title"), + hyperfast(templates.post, { + "article h1": post.data.get("title"), + "article .post-content": { + _html: post.body + } + }) ); }, taxon(ctx, config, taxonItems) { ctx.type = "html"; - ctx.body = prependDoctype( - templates - .layout() - .pipe(rheo()) - .outer("main", showPage("taxon")) - .inner("h1", rheo(config.site.author.name)) - .inner(".posts", function(postsTemplate) { - return taxonItems.pipe(postsTemplate.map(renderPostListItem(ctx))); - }) - .pipe(setTitle(config.site.author.name)) - .render() + + ctx.body = layout( + config, + null, + hyperfast(templates.taxon, { + ".post": taxonItems.map(renderPostListItem(ctx)) + }) ); } }; -- cgit 1.4.1