From 136ebe22452c0015770d9f0ac9d94d8941461816 Mon Sep 17 00:00:00 2001 From: Alan Pearce Date: Fri, 30 Jun 2017 13:11:45 +0200 Subject: refactor: remove console.log --- src/responders.js | 113 +++++++++++++++++++++++++++--------------------------- 1 file changed, 57 insertions(+), 56 deletions(-) diff --git a/src/responders.js b/src/responders.js index 824195b..a03533d 100644 --- a/src/responders.js +++ b/src/responders.js @@ -1,58 +1,59 @@ -'use strict' +"use strict"; -const h = require('highland') -const fs = require('fs') -const rheo = require('rheo') -const indent = require('indent-string') +const h = require("highland"); +const fs = require("fs"); +const rheo = require("rheo"); +const indent = require("indent-string"); const toLines = string => - string.split('\n').map((s, i, arr) => (i === arr.length - 1 ? s : s + '\n')) + string.split("\n").map((s, i, arr) => (i === arr.length - 1 ? s : s + "\n")); const getTemplate = name => - fs.readFileSync(`${__dirname}/templates/${name}.html`, 'utf8') + fs.readFileSync(`${__dirname}/templates/${name}.html`, "utf8"); -const findMain = /^(\s+)
h(content) +function templateReader(template, indentLevel) { + const content = toLines( + indentForTemplate(getTemplate(template), indentLevel) + ); + return () => h(content); } const templates = { - layout: templateReader('layout'), - home: templateReader('home', baseIndentLevel), - post: templateReader('post', baseIndentLevel), - taxon: templateReader('taxon', baseIndentLevel) -} + layout: templateReader("layout"), + home: templateReader("home", baseIndentLevel), + post: templateReader("post", baseIndentLevel), + taxon: templateReader("taxon", baseIndentLevel) +}; -function setTitle (siteTitle, pageTitle) { - return rheo.template(function (s) { +function setTitle(siteTitle, pageTitle) { + return rheo.template(function(s) { return s - .inner('title', rheo((pageTitle ? ' · ' : '') + siteTitle)) - .inner('body header h1', rheo(siteTitle)) - }) + .inner("title", rheo((pageTitle ? " · " : "") + siteTitle)) + .inner("body header h1", rheo(siteTitle)); + }); } -function renderPostListItem (ctx) { - return function (template, [, post]) { +function renderPostListItem(ctx) { + return function(template, [, post]) { return template - .attribute('a', 'href', () => ctx.getURL('post', post.basename)) - .inner('a', () => rheo(post.data.get('title'))) - } + .attribute("a", "href", () => ctx.getURL("post", post.basename)) + .inner("a", () => rheo(post.data.get("title"))); + }; } -function showPage (name) { - return function (els) { - return rheo(templates[name]()) - } +function showPage(name) { + return function(els) { + return rheo(templates[name]()); + }; } module.exports = { @@ -60,42 +61,42 @@ module.exports = { postIndentLevel, indentForTemplate, - home (ctx, config, postsStream) { - ctx.type = 'html' + home(ctx, config, postsStream) { + ctx.type = "html"; ctx.body = templates .layout() .pipe(rheo()) - .outer('main', showPage('home')) - .inner('.posts', function (postsTemplate) { - return postsStream.pipe(postsTemplate.map(renderPostListItem(ctx))) + .outer("main", showPage("home")) + .inner(".posts", function(postsTemplate) { + return postsStream.pipe(postsTemplate.map(renderPostListItem(ctx))); }) .pipe(setTitle(config.site.title)) - .render() + .render(); }, - post (ctx, config, post) { - ctx.type = 'html' + post(ctx, config, post) { + ctx.type = "html"; ctx.body = 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.title, post.data.get('title'))) - .render() + .outer("main", showPage("post")) + .inner("article h1", rheo(post.data.get("title"))) + .outer("article main", rheo(post.body)) + .pipe(setTitle(config.site.title, post.data.get("title"))) + .render(); }, - taxon (ctx, config, taxonItems) { - ctx.type = 'html' + taxon(ctx, config, taxonItems) { + ctx.type = "html"; ctx.body = templates .layout() .pipe(rheo()) - .outer('main', showPage('taxon')) - .inner('h1', rheo(config.site.title)) - .inner('.posts', function (postsTemplate) { - return taxonItems.pipe(postsTemplate.map(renderPostListItem(ctx))) + .outer("main", showPage("taxon")) + .inner("h1", rheo(config.site.title)) + .inner(".posts", function(postsTemplate) { + return taxonItems.pipe(postsTemplate.map(renderPostListItem(ctx))); }) .pipe(setTitle(config.site.title)) - .render() + .render(); } -} +}; -- cgit 1.4.1