-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.js
150 lines (135 loc) · 4.24 KB
/
index.js
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// cheapo gulp-load-plugins
var $ = {
map: require('gulp-map'),
markdown: require('gulp-markdown'),
util: require('gulp-util'),
filetree: require('gulp-filetree'),
size: require('gulp-size'),
frontMatter: require('gulp-front-matter')
}
var path = require('path')
var lazypipe = require('lazypipe')
// for async for template loading
var Q = require('kew')
var fs = require('fs')
var jade = require('jade')
var File = require('vinyl')
// showing off
var archy = require('archy')
var chalk = require('chalk')
/**
* Render the template, injecting the file into `page.file`.
*
* Switch templates by settings `meta.layout` on the file.
*
* Template variables:
*
* - page: Vinyl object <File ..>
* - page.meta: Front-matter properties
* - page.tree: Site tree
* - page.subtree: Subtree rooted at this file
* - page.content: Main content of the current page
*
* The nodes in the tree's (Treenode) have the following properties:
*
* - node.leaf: value at current node (Vinyl object) (may be undefined)
* - node.parent: parent node (Treenode) (undefined for root)
* - node.label: string label (~ path.basename(file.path))
* - node.nodes: (empty) array of Treenodes that are children of this node
*
* There are also some methods on Treenodes.
*
* - node.is_leaf(): true if no children
* - node.is_singleton(): true if has only one child
* - node.is_last_child(): true if it is the last child of the parent
*
*/
function render_tmpl () {
// store compiled templates for great good
var cache = {}
var compile_template = function (filename) {
return Q
.fcall(function () {
// return cached immediately
if (cache[filename]) {
return cache[filename]
}
// (asynchronously) load and compile template
$.util.log('loading template: ' + filename)
return Q
.nfcall(fs.readFile, filename)
.then(function (tmpl_content) {
// turn template in promise returning function and cache it
var compiled = jade.compile(tmpl_content, {pretty: true, filename: filename})
$.util.log('compiled template: ' + chalk.yellow(filename))
return (cache[filename] = compiled)
})
})
.fail(function (err) {
$.util.log('failed compiling jade template', chalk.red(err))
})
}
return $.map(function (file) {
// select template
var t = (file.frontMatter && file.frontMatter.layout) || 'default'
// pull from cache, compile if needed
return compile_template('templates/' + t + '.jade')
.then(function (compiled_template) {
$.util.log('rendering [' + chalk.yellow(t) + '] "' +
chalk.magenta(path.basename(file.path)) + '"')
try {
// render it with template variable 'page'
var html = compiled_template({page: file})
} catch (err) {
console.log('[' + chalk.red('ERR') +
'] Failed rendering jade template\n\t' +
chalk.red(err.message))
}
return new File({
cwd: file.cwd,
base: file.base,
path: file.path.replace(/\.md$/, '.html'),
contents: new Buffer(html)
})
})
.fail(function (err) {
$.util.log('Failed rendering jade template', chalk.red(err))
})
})
}
// replace [[My Page]] with <a href='My-Page.html'>My Page</a>
/*
var resolve_wiki_links = function () {
return $.replace(/\[\[(.*?)\]\]/g,
function wikiLink (match, text) {
var href = text.trim().replace(/ /g, '-') + '.html'
return '<a href="' + href + '">' + text + '</a>'
})
}
*/
var extended_attributes = function (file) {
file.path = file.path && file.path.replace(/\.md$/, '.html')
file.basename = path.basename(file.path)
file.shortName = file.basename && file.basename.replace(/\.html$/, '')
file.href = file.relative
return file
}
var show_tree_once = function () {
var once = false
return $.map(function (file) {
if (!once && file.tree) {
$.util.log('File tree\n' + archy(file.tree))
once = true
}
return file
})
}
module.exports = lazypipe()
.pipe($.map, extended_attributes)
.pipe($.frontMatter)
.pipe($.markdown)
// // .pipe(resolve_wiki_links)
.pipe($.filetree)
.pipe(show_tree_once)
.pipe(render_tmpl)
.pipe($.size)