This repository has been archived by the owner on Nov 9, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
95 lines (81 loc) · 2.94 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
// Generated by CoffeeScript 1.7.1
var async, fs, nunjucks, path,
__hasProp = {}.hasOwnProperty,
__extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };
fs = require('fs');
path = require('path');
nunjucks = require('nunjucks');
async = require('async');
module.exports = function(env, ready) {
var NunjucksContent, NunjucksGivenLoader, loadFilters;
NunjucksContent = (function(_super) {
__extends(NunjucksContent, _super);
function NunjucksContent(filepath, metadata, tpl) {
this.filepath = filepath;
this.metadata = metadata;
this.tpl = tpl;
}
NunjucksContent.prototype.getFilename = function() {
return this.filepath.relative.replace(/nunjucks$/, 'html');
};
NunjucksContent.prototype.getHtml = function() {
return this.tpl.render(this.metadata);
};
return NunjucksContent;
})(env.plugins.Page);
NunjucksGivenLoader = (function() {
function NunjucksGivenLoader(opts) {
this.opts = opts;
}
NunjucksGivenLoader.prototype.getSource = function(name) {
if (name === 'given') {
return {
src: this.opts.given,
path: this.opts.path
};
} else {
return this.opts.fallback.getTemplate(name);
}
};
NunjucksGivenLoader.prototype.on = function() {};
return NunjucksGivenLoader;
})();
loadFilters = function(nenv) {
if (env.config.nunjucks && env.config.nunjucks.filterdir) {
env.config.nunjucks.filters.map(function(name) {
var file, filter;
file = path.join(env.config.nunjucks.filterdir, name + ".js");
filter = env.loadModule(env.resolvePath(file), true);
nenv.addFilter(name, filter);
});
}
};
NunjucksContent.fromFile = function(filepath, callback) {
return async.waterfall([
function(next) {
return fs.readFile(filepath.full, next);
}, function(buffer, next) {
return env.plugins.MarkdownPage.extractMetadata(buffer.toString(), next);
}, function(result, next) {
var contentPath, error, nenv, nloader, template;
contentPath = path.dirname(filepath.full);
nloader = new NunjucksGivenLoader({
given: result.markdown,
path: contentPath,
fallback: new nunjucks.FileSystemLoader(contentPath)
});
nenv = new nunjucks.Environment(nloader);
loadFilters(nenv);
try {
template = nenv.getTemplate('given');
return next(null, new NunjucksContent(filepath, result.metadata, template));
} catch (_error) {
error = _error;
return next(error);
}
}
], callback);
};
env.registerContentPlugin('pages', "**/*.*(html|nunjucks)", NunjucksContent);
return ready();
};