summary refs log tree commit diff stats
path: root/src/responders.js
blob: e472579c4041b84772947d624e534504bd54dac2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
"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+)<main/m;
const baseIndentLevel = findMain.exec(getTemplate("layout"))[1].length;
const postIndentLevel =
  baseIndentLevel + findMain.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());
}

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())
        .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()
    );
  },

  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()
    );
  },

  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()
    );
  }
};