-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
96 lines (82 loc) · 2.4 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
'use strict'
const initialAbsurd = require('absurd')
const merge = require('merge')
exports.name = 'absurd'
exports.inputFormats = ['absurd', 'absurdjs']
// Absurd can output results in multiple formats. We output with 'absurd'
// to refrain from making an asumption on how it will be used.
exports.outputFormat = 'absurd'
/**
* Given the options and locals, will construct an Absurd object.
*/
function constructAbsurd(options, locals) {
// Build a base Absurd object.
const absurd = initialAbsurd()
// Retrieve the options.
options = merge(options || {}, locals || {})
// Check if we are to morph the object.
if (options.morph) {
absurd.morph(options.morph)
}
return absurd
}
/**
* Build an Absurd object from the given input, options and locals.
*/
function getAbsurdFromRender(input, options, locals) {
const absurd = constructAbsurd(options, locals)
switch (options.type) {
case 'javascript':
// TODO: Enable rendering Absurd JavaScript with .render(). Perhaps by
// using eval() to get the API module.exports object?
absurd.raw('Using Absurd\'s .render() with JavaScript is disabled.')
break
case 'yaml':
absurd.add(require('js-yaml').safeLoad(input))
break
case 'css':
absurd.importCSS(input)
break
case 'json':
default:
absurd.add(JSON.parse(input))
break
}
return absurd
}
/**
* Construct an Absurd object from a filename, options and locals.
*/
function getAbsurdFromFile(filename, options, locals) {
const absurd = constructAbsurd(options, locals)
absurd.import(filename)
return absurd
}
exports.render = function (input, options, locals) {
return getAbsurdFromRender(input, options, locals).compile(options)
}
exports.renderAsync = function (input, options, locals) {
return new Promise((resolve, reject) => {
getAbsurdFromRender(input, options, locals).compile(options, (err, result) => {
if (err) {
reject(err)
} else {
resolve(result)
}
})
})
}
exports.renderFile = function (input, options, locals) {
return getAbsurdFromFile(input, options, locals).compile(options)
}
exports.renderFileAsync = function (input, options, locals) {
return new Promise((resolve, reject) => {
getAbsurdFromFile(input, options, locals).compile(options, (err, result) => {
if (err) {
reject(err)
} else {
resolve(result)
}
})
})
}