summary refs log tree commit diff stats
path: root/src/responders.js
diff options
context:
space:
mode:
authorAlan Pearce2017-07-01 11:49:18 +0200
committerAlan Pearce2017-07-01 11:49:18 +0200
commitfb7e421b9efea0a96adcf30d30cc2d318980d928 (patch)
tree37595b22cfab571d93dcae1d848c404b93e86a63 /src/responders.js
parent248c74916e22f397ff95944ac2596b9e40b302e5 (diff)
downloadhomestead-fb7e421b9efea0a96adcf30d30cc2d318980d928.tar.lz
homestead-fb7e421b9efea0a96adcf30d30cc2d318980d928.tar.zst
homestead-fb7e421b9efea0a96adcf30d30cc2d318980d928.zip
Switch templating to hyperfast
This also means that highland is not (currently) required
Diffstat (limited to 'src/responders.js')
-rw-r--r--src/responders.js116
1 files changed, 47 insertions, 69 deletions
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+)<main/m;
 const baseIndentLevel = findMain.exec(getTemplate("layout"))[1].length;
 const postIndentLevel =
-  baseIndentLevel + findMain.exec(getTemplate("post"))[1].length;
+  baseIndentLevel +
+  /^(\s+)<div class="post-content/m.exec(getTemplate("post"))[1].length;
 
 function indentForTemplate(text, indentLevel) {
   return indent(text, indentLevel).slice(indentLevel).replace(/\n+$/, "");
 }
 
 function templateReader(template, indentLevel) {
-  const content = toLines(
-    indentForTemplate(getTemplate(template), indentLevel)
-  );
-  return () => h(content);
-}
-
-function prependDoctype(stream) {
-  return h(["<!DOCTYPE html>"]).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))
+      })
     );
   }
 };