-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
99 lines (84 loc) · 2.73 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
'use strict'
const path = require('path')
const Twig = require('twig')
const validPackageName = require('validate-npm-package-name')
const twigRender = Twig.twig
const loadModuleFilter = (name, filterModule, extendFunctionName) => {
// Validate that we're loading an actual package.
if (!validPackageName(filterModule).validForNewPackages) {
return
}
try {
// Load the filter module.
const out = require(filterModule)
// Check if the module is just a function.
if (typeof out === 'function') {
Twig[extendFunctionName](name, out)
} else if (out && (typeof out === 'object')) {
// Perhaps it is an associative array of functions?
for (const outName in out) {
if (typeof out[outName] === 'function') {
Twig[extendFunctionName](outName, out[outName])
}
}
}
} catch (error) {
console.error(error)
}
}
const transformer = {
name: 'twig',
outputFormat: 'html',
}
transformer.compile = function (input, options) {
// Construct the Twig options.
options = options || {}
options.data = input
if ('filename' in options && !('path' in options)) {
options.path = options.filename
}
// Make sure path is always a string, and not an object.
// TODO: Make sure the `root` is correct?
if (options.path && typeof options.path !== 'string') {
const pathRoot = options.root || options.path.root
options.path = pathRoot ? path.join(pathRoot, path.format(options.path)) : path.format(options.path)
}
// Extend Filters and Functions
const extendable = {
filters: 'extendFilter',
functions: 'extendFunction',
}
for (const extendableName of Object.keys(extendable)) {
const extendFunctionName = extendable[extendableName]
// Allow options.filters to be a require() string.
if (typeof options[extendableName] === 'string') {
try {
options[extendableName] = require(options[extendableName])
} catch (error) {
console.error(error)
}
}
// Loop through all the given filters.
for (const name of Object.keys(options[extendableName] || {})) {
const filter = options[extendableName][name]
if (typeof filter === 'string') {
loadModuleFilter(name, options[extendableName][name], extendFunctionName)
} else if (typeof filter === 'function') {
Twig[extendFunctionName](name, options[extendableName][name])
}
}
}
// Build the template.
let output = ''
try {
// Build the template renderer.
const template = twigRender(options)
// Use .bind() so that the template is "this" when rendering.
output = template.render.bind(template)
} catch (error) {
console.log(options)
console.error(error)
}
return output
}
module.exports = transformer