diff options
author | Alan Pearce | 2017-07-01 11:49:18 +0200 |
---|---|---|
committer | Alan Pearce | 2017-07-01 11:49:18 +0200 |
commit | fb7e421b9efea0a96adcf30d30cc2d318980d928 (patch) | |
tree | 37595b22cfab571d93dcae1d848c404b93e86a63 /src | |
parent | 248c74916e22f397ff95944ac2596b9e40b302e5 (diff) | |
download | homestead-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')
-rw-r--r-- | src/actions.js | 59 | ||||
-rw-r--r-- | src/responders.js | 116 | ||||
-rw-r--r-- | src/templates/home.html | 12 | ||||
-rw-r--r-- | src/templates/post.html | 20 |
4 files changed, 88 insertions, 119 deletions
diff --git a/src/actions.js b/src/actions.js index 02414a1..11acff5 100644 --- a/src/actions.js +++ b/src/actions.js @@ -1,46 +1,41 @@ -'use strict' +"use strict"; -const send = require('koa-send') -const h = require('highland') -const responders = require('./responders') +const send = require("koa-send"); +const responders = require("./responders"); -function toArrayStream (iterator) { - return h(iterator.entries()) +function home(config, posts) { + const postsArray = Array.from(posts.values()); + return async function(ctx, next) { + responders.home(ctx, config, postsArray); + }; } -function home (config, posts) { - const postsStream = toArrayStream(posts) - return async function (ctx, next) { - responders.home(ctx, config, postsStream) - } -} - -function post (config, posts) { - return async function (ctx, next) { - ctx.assert(posts.has(ctx.params.filename), 404, 'Post not found') - const post = posts.get(ctx.params.filename) +function post(config, posts) { + return async function(ctx, next) { + ctx.assert(posts.has(ctx.params.filename), 404, "Post not found"); + const post = posts.get(ctx.params.filename); - responders.post(ctx, config, post) - } + responders.post(ctx, config, post); + }; } -function taxonGenerator (config, term, items) { - return async function (ctx, next) { - const value = ctx.params.value - ctx.assert(items.has(ctx.params.value), 404, `${term} ${value} not found`) +function taxonGenerator(config, term, items) { + return async function(ctx, next) { + const value = ctx.params.value; + ctx.assert(items.has(ctx.params.value), 404, `${term} ${value} not found`); - const taxonItems = toArrayStream(items.get(value)) + const taxonItems = items.get(value); - responders.taxon(ctx, config, taxonItems) - } + responders.taxon(ctx, config, taxonItems); + }; } -const prefix = /^\/static\// -async function serveFiles (ctx) { +const prefix = /^\/static\//; +async function serveFiles(ctx) { if (prefix.test(ctx.path)) { - await send(ctx, ctx.path.replace(prefix, ''), { - root: './static' - }) + await send(ctx, ctx.path.replace(prefix, ""), { + root: "./static" + }); } } @@ -49,4 +44,4 @@ module.exports = { post, taxonGenerator, serveFiles -} +}; 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)) + }) ); } }; diff --git a/src/templates/home.html b/src/templates/home.html index dfc4677..97ac89a 100644 --- a/src/templates/home.html +++ b/src/templates/home.html @@ -1,7 +1,5 @@ -<main class="home"> - <ul class="posts"> - <li class="post"> - <a href="/">Test post please ignore</a> - </li> - </ul> -</main> +<ul class="posts"> + <li class="post"> + <a href="/">Test post please ignore</a> + </li> +</ul> diff --git a/src/templates/post.html b/src/templates/post.html index b6eba4e..384e6bb 100644 --- a/src/templates/post.html +++ b/src/templates/post.html @@ -1,11 +1,9 @@ -<main class="post"> - <article> - <h1>post title</h1> - <main> - Fringilla ut morbi tincidunt augue interdum velit euismod! - Interdum velit laoreet id donec ultrices tincidunt arcu, non - sodales neque sodales ut etiam sit amet nisl purus, in - mollis nunc sed. - </main> - </article> -</main> +<article> + <h1>post title</h1> + <div class="post-content"> + Fringilla ut morbi tincidunt augue interdum velit euismod! + Interdum velit laoreet id donec ultrices tincidunt arcu, non + sodales neque sodales ut etiam sit amet nisl purus, in + mollis nunc sed. + </div> +</article> |