-
Notifications
You must be signed in to change notification settings - Fork 0
/
partial-funcs.mjs
115 lines (100 loc) · 4.27 KB
/
partial-funcs.mjs
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
import { promises as fsp } from 'fs';
import fs from 'fs';
import { documents, assets, layouts, partials } from './cache/file-cache.mjs';
import util from 'util';
export async function partial(config, fname, metadata) {
if (!fname || typeof fname !== 'string') {
throw new Error(`partial fname not a string ${util.inspect(fname)}`);
}
// console.log(`partial ${fname}`);
const found = partials.find(fname);
if (!found) {
throw new Error(`No partial found for ${fname} in ${util.inspect(config.partialsDirs)}`);
}
// console.log(`partial ${fname} ==> ${found.vpath} ${found.fspath}`);
/* No longer necessary to <code>stat</code> these files, because
* DirsWatcher has already determined these are regular files.
*
let stats;
try {
stats = await fsp.stat(found.fspath);
} catch (err) {
throw new Error(`partial could not stat ${found.vpath} at ${found.fspath}`, err.stack);
}
if (!stats.isFile()) {
throw new Error(`renderPartial non-file found for ${fname} - ${found.vpath}`);
}
*/
// console.log(`partial ${fname} ==> vpath ${found.vpath} fspath ${found.fspath}`);
const renderer = config.findRendererPath(found.vpath);
if (renderer) {
// console.log(`partial about to render ${util.inspect(found.vpath)}`);
let partialText = found.docContent
? found.docContent
: await fsp.readFile(found.fspath, 'utf8');
// Some renderers (Nunjuks) require that metadata.config
// point to the config object. This block of code
// duplicates the metadata object, then sets the
// config field in the duplicate, passing that to the partial.
let mdata = {};
let prop;
for (prop in metadata) {
mdata[prop] = metadata[prop];
}
mdata.config = config;
mdata.partialSync = partialSync.bind(renderer, config);
mdata.partial = partial.bind(renderer, config);
return renderer.render(partialText, mdata, found);
} else if (found.vpath.endsWith('.html') || found.vpath.endsWith('.xhtml')) {
// console.log(`partial reading file ${found.vpath}`);
return fsp.readFile(found.fspath, 'utf8');
} else {
throw new Error(`renderPartial no Renderer found for ${fname} - ${found.vpath}`);
}
// This has been moved into Mahabhuta
// return mahaPartial.doPartialAsync(partial, attrs);
}
export function partialSync(config, fname, metadata) {
if (!fname || typeof fname !== 'string') {
throw new Error(`partial fname not a string ${util.inspect(fname)}`);
}
const found = partials.find(fname);
if (!found) new Error(`No partial found for ${fname} in ${util.inspect(config.partialsDirs)}`);
/* No longer necessary to <code>stat</code> these files, because
* DirsWatcher has already determined these are regular files.
*
let stats;
try {
stats = fs.statSync(found.fspath);
} catch (err) {
throw new Error(`partialSync could not stat ${found.vpath} at ${found.fspath}`, err.stack);
}
if (!stats.isFile()) {
throw new Error(`renderPartial non-file found for ${fname} - ${found.vpath}`);
}
*/
var renderer = config.findRendererPath(found.vpath);
if (renderer) {
// Some renderers (Nunjuks) require that metadata.config
// point to the config object. This block of code
// duplicates the metadata object, then sets the
// config field in the duplicate, passing that to the partial.
let mdata = {};
let prop;
for (prop in metadata) {
mdata[prop] = metadata[prop];
}
mdata.config = config;
mdata.partialSync = partialSync.bind(renderer, config);
let partialText = found.docContent
? found.docContent
: fs.readFileSync(found.fspath, 'utf8');
return renderer.renderSync(partialText, mdata, found);
} else if (found.vpath.endsWith('.html') || found.vpath.endsWith('.xhtml')) {
return fs.readFileSync(found.fspath, 'utf8');
} else {
throw new Error(`renderPartial no Renderer found for ${fname} - ${found.vpath}`);
}
// This has been moved into Mahabhuta
// return mahaPartial.doPartialSync(fname, metadata);
}