forked from cambridge-healthcare/grunt-stencil
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprocess.js
86 lines (67 loc) · 2.26 KB
/
process.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
"use strict";
/*
* This module handles the logic of processing a single
* given file, whether it is a template, page or partial.
*
* The compiler, options, readers and file matchers to
* be used are all injected.
*
* Mainly, this module makes sure that circular dependencies
* do not occur with partial inclusions, by keeping track
* of processed files.
*/
module.exports = setup;
var _ = require("underscore");
function setup (config) {
var options = config.options || {},
read_header = config.read_header,
compile = config.compile,
find_closest_match = config.find_closest_match;
var process_page = process_file.bind(null, "."),
process_partial = process_file.bind(null, options.partials),
process_template = process_file.bind(null, options.templates),
processed = {};
var initial_env = _.extend({}, options.env, { include: process_partial });
return process_page;
function process_file (folder, filename, params) {
var input_file = find_closest_match(folder, filename);
if (processed[input_file]) throw new Error("Circular dependency detected.");
processed[input_file] = true;
var compiled_page = page(input_file, params);
delete processed[input_file];
return compiled_page;
}
function page (input_file, params) {
var header = read_header(input_file);
var env = merge_env(header, params);
var content = bundle(compile(input_file, env), header);
if (header.template) {
content = bundle(wrap(header.template, content), header);
}
return content;
}
function wrap (template, page) {
return process_template(template, { document: page }).toString();
}
function merge_env (/* objects... */) {
return _.flatten([
initial_env,
arguments
]).reduce(function (agg, value) {
return _.extend(agg, value);
}, {});
}
/*
* This function takes advantage of JavaScript feature for objects
* that whenever an object is conctenated to a string, the
* toString() method is invoked.
*
* valueOf() is useful in comparisons.
*/
function bundle (str, header) {
return _.extend(header, {
valueOf: function () { return str; },
toString: function () { return str; }
});
}
}