diff options
-rw-r--r-- | src/responders.js | 113 |
1 files 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+)<main/m -const baseIndentLevel = findMain.exec(getTemplate('layout'))[1].length +const findMain = /^(\s+)<main/m; +const baseIndentLevel = findMain.exec(getTemplate("layout"))[1].length; const postIndentLevel = - baseIndentLevel + findMain.exec(getTemplate('post'))[1].length + baseIndentLevel + findMain.exec(getTemplate("post"))[1].length; -function indentForTemplate (text, indentLevel) { - return indent(text, indentLevel).slice(indentLevel).replace(/\n+$/, '') +function indentForTemplate(text, indentLevel) { + return indent(text, indentLevel).slice(indentLevel).replace(/\n+$/, ""); } -function templateReader (template, indentLevel) { - const content = toLines(indentForTemplate(getTemplate(template), indentLevel)) - console.log(template, content) - return () => 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(); } -} +}; |