-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
142 lines (115 loc) · 4.78 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
'use strict';
const path = require('path');
const fs = require('fs');
const _ = require('lodash');
const nunjucks = require('nunjucks');
const cheerio = require('cheerio');
const deepCloneMerge = require('deep-clone-merge');
const yaml = require('js-yaml');
const loadHtml = html => {
return cheerio.load(html, { normalizeWhitespace: true });
};
const formatHtml = $ => {
let html = $.html()
.replace(/’/g, '’')
.replace(/(<[^/][^>]+>)\s*/g, '\n$1')
.replace(/\s*(<\/[^>]+>)/g, '$1\n')
.replace(/(\n\s*)+/g, '\n');
return html.trim();
};
const cleanHtml = $ => {
let html = $.html()
.replace(/’/g, '’')
.replace(/(<[^/][^>]+>)\s*/g, '$1')
.replace(/\s*(<\/[^>]+>)/g, '$1')
.replace(/(\n\s*)+/g, '');
return html.trim();
};
const renderer = (views, locales, globals = require('hmpo-components/lib/globals'), filters = require('hmpo-components/lib/filters'), realistic) => {
let nunjucksEnv = nunjucks.configure(views, {
trimBlocks: true,
lstripBlocks: true
});
globals.addGlobals(nunjucksEnv);
filters.addFilters(nunjucksEnv);
const loadLocale = (p, stack) => {
const stat = fs.statSync(p);
if (stat.isDirectory()) {
const files = fs.readdirSync(p);
let data = {};
files.forEach(file => {
if (file.match(/\.(jso?n|ya?ml)$/)) data = loadLocale(path.resolve(p, file), data);
});
return data;
}
let data;
try {
const text = fs.readFileSync(p).toString();
if (p.match(/\.jso?n$/)) data = JSON.parse(text);
else if (p.match(/\.ya?ml$/)) data = yaml.load(text);
else throw new Error('Unknown file type');
} catch(e) {
throw new Error('Error loading localisation file ' + p + ': ' + e.message);
}
if (!stack) return data;
// mount this file in the correct place in the stack based on filename
const parts = path.basename(p).split('.');
parts.pop();
if (parts[0] === 'default') parts.shift();
while(parts.length) data = { [parts.pop()]: data };
return deepCloneMerge(stack, data);
};
let locale;
if (locales) {
locales = locales.map(locale => loadLocale(locale));
locale = deepCloneMerge(...locales);
}
const render = (options, context = {}) => {
if (typeof options === 'string') options = { template: options };
context = Object.assign({
translate: (key, translateOptions = {}) => {
translateOptions = _.extend({ self: true }, translateOptions);
if (realistic) {
if (!locale) return;
const keys = Array.isArray(key) ? key : [ key ];
return _.reduce(keys, (str, k) => {
return str || _.get(locale, k);
}, null) || translateOptions.default || (translateOptions.self && keys[0]);
}
if (Array.isArray(key)) key = key[0];
if (!locale) return '[' + key + ']';
// check if keys exist in locale en file
let translation = _.get(locale, key) || translateOptions.default;
if (translateOptions.self && !translation && !options.ignore === true && !_.includes(options.ignore, key))
throw new Error('Translation not found for ' + key);
return options.translate ? String(translation) : '[' + key + ']';
},
ctx: key => key ? _.get(context, key) : context
}, context);
let output;
if (options.template) output = nunjucksEnv.render(options.template, context);
else if (options.string) output = nunjucksEnv.renderString(options.string, context);
else if (options.component) {
const filename = options.component.replace(/([A-Z])/g, l => '-' + l.toLowerCase()) + '/macro.njk';
const importString = `{% from "${filename}" import ${options.component} %}`;
const args = [];
if (options.ctx) args.push('ctx');
if (options.params) args.push(JSON.stringify(options.params, null, ' '));
const macroString = `${options.component}(${args.join(',')})`;
const string = options.caller ?
`${importString}{% call ${macroString} %}${options.caller}{% endcall %}` :
`${importString}{{ ${macroString} }}`;
output = nunjucksEnv.renderString(string, context);
}
else throw new Error('Cannot render!');
return loadHtml(output);
};
render.dictionary = locale;
return render;
};
module.exports = {
renderer,
loadHtml,
formatHtml,
cleanHtml
};