"use strict"; const h = require("highland"); const fs = require("fs"); const rheo = require("rheo"); 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"); const findMain = /^(\s+)
h(content); } function prependDoctype(stream) { return h([""]).concat(stream).pipe(new PassThrough()); } const templates = { layout: templateReader("layout"), home: templateReader("home", baseIndentLevel), post: templateReader("post", baseIndentLevel), 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 showPage(name) { return function(els) { return rheo(templates[name]()); }; } module.exports = { baseIndentLevel, postIndentLevel, indentForTemplate, home(ctx, config, postsStream) { ctx.type = "html"; ctx.body = prependDoctype( templates .layout() .pipe(rheo()) .outer("main", showPage("home")) .inner(".posts", function(postsTemplate) { return postsStream.pipe(postsTemplate.map(renderPostListItem(ctx))); }) .pipe(setTitle(config.site.author)) .render() ); }, 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, post.data.get("title"))) .render() ); }, taxon(ctx, config, taxonItems) { ctx.type = "html"; ctx.body = prependDoctype( templates .layout() .pipe(rheo()) .outer("main", showPage("taxon")) .inner("h1", rheo(config.site.author)) .inner(".posts", function(postsTemplate) { return taxonItems.pipe(postsTemplate.map(renderPostListItem(ctx))); }) .pipe(setTitle(config.site.author)) .render() ); } };