-
Notifications
You must be signed in to change notification settings - Fork 1
/
build.js
142 lines (118 loc) · 3.84 KB
/
build.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
142
'use strict';
// Require dependencies
var fs = require('fs');
var child_process = require('child_process');
var jade = require('jade');
var mkdirp = require('mkdirp');
var sass = require('node-sass');
var colors = require('colors');
// Get config
var data = require('./config.json');
// Set output directory
var outputDir = '/' + data.output_dir + '/';
if (outputDir == '//'){
outputDir = '/';
}
// Render Jade
exports.renderJade = function(){
// Get templates
data.templates.forEach(function(template){
var tempLangs = template.languages;
var tempName = template.name;
var tempFilename = __dirname + '/templates/' + tempName + '/' + 'template.jade';
var tempSource = fs.readFileSync(tempFilename);
var fn = jade.compile(tempSource, {doctype: 'html', filename:tempFilename, pretty: data.pretty});
// Get languages from template
function getLang(lang, url){
var locals = {};
var i18n_t = require(__dirname + '/templates/' + tempName + '/' + lang + '.json');
// Save template strings
for(var i in i18n_t) {
locals[i] = i18n_t[i];
}
// Save layout strings
if(data.use_layout === true){
var i18n_l = require(__dirname + '/templates/layout/' + lang + '.json');
for(var i in i18n_l) {
locals[i] = i18n_l[i];
}
}
// Build output paths
if (data.default_language == lang){
var outputPath = __dirname + outputDir;
}
else {
var outputPath = __dirname + outputDir + lang + '/';
}
if (tempName !== 'index'){
outputPath += url + '/';
}
// Set variables
locals.base_url = data.base_url;
locals.assets_dir = locals.base_url + data.assets_dir + '/';
locals.css_dir = locals.base_url + data.css_dir + '/';
locals.sass_dir = locals.base_url + data.sass_dir + '/';
locals.fonts_dir = locals.base_url + data.fonts_dir + '/';
locals.javascript_dir = locals.base_url + data.javascript_dir + '/';
locals.current_language = lang;
locals.current_page = tempName;
// Generate automatic page links
locals.page_link = function(name){
var page = data.templates.filter(function(t){return t.name === name;});
page = page[0].languages[lang];
if (name != 'index' && data.default_language != lang){
return lang + '/' + page;
}
else if (name != 'index' && data.default_language == lang){
return page;
}
else if (name == 'index' && data.default_language != lang){
return data.base_url + lang + '/';
}
else {
return data.base_url;
}
};
var html = fn(locals);
// Create files
mkdirp(outputPath, function(err){
if (err) {
console.error(err);
} else {
fs.writeFileSync(outputPath + 'index.html', html, 'utf8');
}
});
}
// For each language run getLang function
for (var lang in tempLangs){
getLang(lang, tempLangs[lang]);
}
});
};
// Render Sass
exports.renderSass = function(){
// Output style
var outputStyle = 'compressed';
if (data.pretty){
outputStyle = 'nested';
}
// Get CSS
sass.render({
file: __dirname + outputDir + data.sass_dir + '/index.' + data.sass_type,
outputStyle: outputStyle
}, function(err, css) {
if (err){
console.log('Error: index.' + data.sass_type + ' on path: '.red + __dirname.red + outputDir.red + data.sass_dir.red + ' does not exist! Please create it and run the command again.'.red);
console.log(err);
}
else {
mkdirp(__dirname + outputDir + data.css_dir, function(err){
if (err) {
console.error(err);
} else {
fs.writeFileSync(__dirname + outputDir + data.css_dir + '/index.css', css.css, 'utf8');
}
});
}
});
};