-
Notifications
You must be signed in to change notification settings - Fork 1
/
hyde.js
188 lines (158 loc) · 6.32 KB
/
hyde.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#! /usr/bin/env node
var path = require('path');
var jade = require('jade');
var fs = require('fs');
var md = require('marked');
var less = require('less');
var _ = require('underscore');
var fs = require('fs');
var mkdirp = require('mkdirp');
if (process.argv.length == 3) {
var init = process.argv[2];
mkdirp('Hyde_Project', function(err) {
if(err) {
return console.log(err);
}
console.log("The path was created.");
});
if (init === 'init') {
fs.writeFile('Hyde_Project/.hyde.json','{"vars": {}}',function (err) {
if(err) {
return console.log(err);
}
console.log("The file was saved.");
});
fs.writeFile('Hyde_Project/main.jade', '', function (err) {
if (err) {
return console.log(err);
}
console.log("The file was saved.");
});
}
} else if (process.argv.length != 4) {
throw new Error('Expected two arguments.\n'
+ 'Usage: hyde <source directoy> <target directory>');
} else {
if (process.argv[2] === 'compile'){
var sourceDir = path.join(process.cwd(),'Hyde_Project');
var targetDir = path.join(process.cwd(), process.argv[3]);
}
else {
var sourceDir = path.join(process.cwd(), process.argv[2]);
var targetDir = path.join(process.cwd(), process.argv[3]);
}
//Load in global variables
var defaultConfig = {
'main_template_file': 'main.jade',
'vars': {}
};
var config = _.defaults(require(path.join(sourceDir, '.hyde.json')), defaultConfig);
defaultConfig['vars']['getStructure'] = require('./getStructure');
//Jade compiler functions
var jadeFunctions = {};
var jadeOptions = {};
//Build main jade template
var mainTemplateFile = path.join(sourceDir, config['main_template_file']);
var defaultJadeFunction = jadeFunctions[mainTemplateFile] = jade.compileFile(mainTemplateFile, jadeOptions);
var copyFile = function (src, dest) {
fs.createReadStream(src).pipe(fs.createWriteStream(dest));
};
var guaranteeDirectory = function (dir) {
if (!fs.existsSync(dir)){
fs.mkdirSync(dir);
}
}
//Less options
var lessOptions = _.defaults(config.lessOptions);
//Build pages
// dirPath: String The directory relative to the sourceDir
var Directory = function (dirPath) {
this.dirPath = dirPath;
};
Directory.prototype.getChildren = function (callback) {
fs.readdir(this.getSourcePath(), callback);
};
Directory.prototype.getSourcePath = function () {
return path.join(sourceDir, this.dirPath);
}
Directory.prototype.getTargetPath = function () {
return path.join(targetDir, this.dirPath);
}
//compiles the given directory, including recursive directories
Directory.prototype.compile = function () {
console.info('Compiling ' + this.dirPath);
var thisObj = this;
//guarantee target directory exists
guaranteeDirectory(this.getTargetPath());
this.getChildren(function (err, files) {
if (err) throw (err);
_.each(files, function (filename) {
var sourceFilename = path.join(thisObj.getSourcePath(), filename);
fs.lstat(sourceFilename, function (err, stats) {
if (err) throw err;
if (stats.isFile()) {
var ext = _.last(filename.split('.')).toLowerCase();
var base = filename.substr(0, filename.length - ext.length - 1);
//if json, compile
if (ext === 'json' && base !== '.hyde') {
console.info('Parsing json ' + sourceFilename);
var pageObj = _.defaults(require(path.join(thisObj.getSourcePath(), filename)), defaultConfig['vars']);
var pageOutput;
//compile corresponding markdown file
fs.readFile(path.join(thisObj.getSourcePath(), base + '.md'), 'utf8', function (err, data) {
if (err) throw err;
pageObj['content'] = md(data);
//generate html from jade
if (pageObj.template) {
var fullTemplatePath = path.join(thisObj.getSourcePath(), pageObj.template);
if (!jadeFunctions[fullTemplatePath]) {
//need to compile jade file
jadeFunctions[fullTemplatePath] = jade.compileFile(fullTemplatePath, jadeOptions);
}
pageOutput = (jadeFunctions[fullTemplatePath])(pageObj);
} else {
//use default function
pageOutput = defaultJadeFunction(pageObj);
}
var targetFilepath;
if (base === 'index') {
targetFilepath = path.join(thisObj.getTargetPath(), 'index.html');
} else {
//create containing directory
guaranteeDirectory(path.join(thisObj.getTargetPath(), base));
targetFilepath = path.join(thisObj.getTargetPath(), base, 'index.html');
}
//write output
fs.writeFile(targetFilepath, pageOutput, function (err) {
if (err) throw err;
});
});
} else if (ext === 'md' || ext == 'jade') {
//do nothing
console.info('Skipping ' + sourceFilename);
} else if (ext === 'less') {
console.info('Compiling ' + sourceFilename);
fs.readFile(path.join(sourceFilename), 'utf8', function (err, data) {
if (err) throw err;
//replace .less file in source with .css in target
less.render(data, lessOptions).then(function (output) {
fs.writeFile(path.join(thisObj.getTargetPath(), base + '.css'), output.css, function (err) {
if (err) throw err;
});
})
});
} else if (base !== '.hyde') {
//otherwise, copy
console.info('Copying ' + sourceFilename);
copyFile(sourceFilename, path.join(thisObj.getTargetPath(), filename));
}
} else if (stats.isDirectory()) {
//recurse
(new Directory(thisObj.dirPath + '/' + filename)).compile();
}
});
});
});
};
(new Directory('.')).compile();
}