-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
101 lines (88 loc) · 2.44 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
const ENGINE = {
config: {
paths: {
doc: 'Paths required by Pug.js',
format: Object,
default: {}
}
},
extensions: ['.pug'],
handle: 'pug'
}
module.exports = () => {
const debug = require('debug')('web:templates:pug')
const path = require('path')
const pug = require('pug')
const requireDir = require('require-dir')
const EnginePug = function (options) {
debug('Starting Pug.js engine...')
this.config = options.config
this.helpers = options.helpers
this.pagesPath = options.pagesPath
this.templates = {}
}
/**
* Returns the engine core module.
*
* @return {function} The engine core module.
*/
EnginePug.prototype.getCore = function () {
return pug
}
/**
* Returns information about the engine.
*
* @return {object} An object containing the engine name and version.
*/
EnginePug.prototype.getInfo = function () {
return {
engine: ENGINE.handle,
version: undefined
}
}
/**
* Initialises the engine.
*
* @return {Promise} A Promise that resolves when the engine is fully loaded.
*/
EnginePug.prototype.initialise = function () {
debug('Pug initialised')
const engineConfig = this.config.get('engines')
if (engineConfig &&
engineConfig.pug &&
engineConfig.pug.paths && engineConfig.pug.paths.helpers
) {
const helperPath = path.join(process.cwd(), engineConfig.pug.paths.helpers)
this.helperFunctions = requireDir(helperPath, { recurse: true, camelcase: true })
}
}
/**
* Registers the template with markup.
*
* @return {Promise} A Promise that resolves with the loaded data.
*/
EnginePug.prototype.register = function (name, data, path) {
this.templates[name] = pug.compile(data, {
basedir: this.pagesPath,
filename: path
})
}
/**
* Renders a template.
*
* @param {string} name The name of the template.
* @param {string} data The template content.
* @param {object} locals The variables to add to the context.
* @param {object} options Additional render options.
*
* @return {Promise} A Promise that resolves with the render result.
*/
EnginePug.prototype.render = function (name, data, locals, options) {
if (this.helperFunctions) {
Object.assign(locals, this.helperFunctions)
}
return Promise.resolve(this.templates[name](locals))
}
return EnginePug
}
module.exports.metadata = ENGINE