-
Notifications
You must be signed in to change notification settings - Fork 2
/
templates-manager.js
128 lines (115 loc) · 3.93 KB
/
templates-manager.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
'use strict';
var path = require('path');
var fs = require('fs');
var logger = require('./logger');
var handlebars = require('handlebars');
var templatesManager = function templatesManager() {
var templates = {}; // {'template': [{suffix: 'default', filename: 'template/template.html'}, {...}], 'layout-homepage': [...]}
var compiled = {}; // {'template.html': compiled template, 'layout-homepage.html': compiled template, ...}
var commonTemplates = []; // [{'type': 'site', 'target': 'sitemap.html', filename: 'template/per-site-sitemap.html'}, {...}]
this.options = {};
this.getFolderIndex = function(files) {
var lowerCaseFiles = files.map(function(file) { return file.toLowerCase(); });
var potentialIndexes = ['readme.md', 'readme.markdown', 'home.md', 'home.markdown'];
for (var i = 0 ; i < potentialIndexes.length ; i++) {
if (lowerCaseFiles.indexOf(potentialIndexes[i]) !== -1) {
return files[lowerCaseFiles.indexOf(potentialIndexes[i])];
}
}
return '';
};
this.scanTemplates = function(layout) {
var folder = this.options.folder;
var files = fs.readdirSync(folder);
templates[layout] = [];
files.forEach(function(file) {
if (path.extname(file) === '.html') {
if (file.indexOf(layout) === 0) {
var suffix = path.basename(file, path.extname(file)).substr(layout.length).substr(1);
templates[layout].push({
filename: path.join(folder, file),
suffix: (suffix ? suffix : 'default')
});
}
}
});
};
this.getVersions = function(filename, layout, isIndex) {
if (!templates[layout]) {
this.scanTemplates(layout);
}
var versions = {};
templates[layout].forEach(function(template) {
versions[template.suffix] = {
format: template.suffix,
templateFile: template.filename,
uri: path.join(
path.dirname(filename),
(isIndex ? 'index' : path.basename(filename, path.extname(filename))) +
(template.suffix === 'default' ? '' : '-' + template.suffix) + '.html'
).replace(/\\/g, '\/')
};
});
return versions;
};
this.getCompiledTemplate = function(layoutVersionFile) {
if (!compiled[layoutVersionFile]) {
var sources;
logger.debug('Template %s was not in cache', layoutVersionFile);
try {
sources = fs.readFileSync(layoutVersionFile, {encoding: 'utf-8'});
} catch (e) {
logger.error('Template read error "%s": %s', layoutVersionFile, e);
return undefined;
}
compiled[layoutVersionFile] = handlebars.compile(sources);
}
return compiled[layoutVersionFile];
};
this.scanCommonTemplates = function() {
var folder = this.options.folder;
var files = fs.readdirSync(folder);
var source;
files.forEach(function(file) {
// partial templates, to be included in main templates
if ((file.indexOf('partial-') === 0) && (path.extname(file) === '.html')) {
try {
source = fs.readFileSync(path.join(folder, file), {encoding: 'utf-8'});
handlebars.registerPartial(
path.basename(file, path.extname(file)).substr('partial-'.length),
source);
} catch (e) {
logger.error('Partial read error "%s": %s', file, e);
}
} else if (file.indexOf('per-') === 0) { // other common templates (site, category, tag)
var filenameParts = file.split('-');
filenameParts.shift(); // remove 'per-'
var type = filenameParts.shift();
var target = filenameParts.join('-');
commonTemplates.push({
type: type,
target: target,
filename: path.join(folder, file)
});
//logger.success('commonTemplates', commonTemplates);
}
});
};
this.getCommonTemplates = function() {
return commonTemplates;
};
this.init = function(options) {
templates = {};
commonTemplates = [];
compiled = {};
this.options = options;
};
};
templatesManager.instance = null;
templatesManager.getInstance = function() {
if (this.instance === null) {
this.instance = new templatesManager();
}
return this.instance;
};
module.exports = templatesManager.getInstance();