-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.js
executable file
·93 lines (73 loc) · 2.54 KB
/
parser.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
var yaml = require('js-yaml');
var fs = require('fs');
var marked = require("marked");
var mustache = require('mustache');
var child_process = require('child_process');
var chalk = require('chalk');
//http://www.geedew.com/remove-a-directory-that-is-not-empty-in-nodejs/
var deleteFolderRecursive = function(path) {
if( fs.existsSync(path) ) {
fs.readdirSync(path).forEach(function(file,index){
var curPath = path + "/" + file;
if(fs.lstatSync(curPath).isDirectory()) { // recurse
deleteFolderRecursive(curPath);
} else { // delete file
fs.unlinkSync(curPath);
}
});
fs.rmdirSync(path);
}
};
console.log(chalk.yellow("* recreating folders"))
//delete rendered folders and recreate them empty
deleteFolderRecursive('./outputHTMLRendered');
deleteFolderRecursive('./outputPDFRendered');
fs.mkdirSync('./outputHTMLRendered');
fs.mkdirSync('./outputPDFRendered');
var files = fs.readdirSync('./workshops/');
var output = "";
for (i in files) {
var fileName = files[i];
console.log(chalk.yellow("* processing file ") + " > " + fileName);
// Get document, or throw exception on error
try {
var doc = yaml.safeLoad(fs.readFileSync('workshops/'+fileName, 'utf8'));
//console.log(doc);
} catch (e) {
//console.log(e);
}
//console.log(doc);
//transform yaml text content using markdown
for (property in doc) {
//console.log(property, doc[property]);
var m;
if (property.startsWith("img") ) {
m = doc[property];
} else {
m = marked("" + doc[property]);
}
//console.log(m);
doc[property] = m;
}
//console.log(doc["albumUrl"])
var template = fs.readFileSync('./templates/ficha.template', 'utf8');
output += mustache.render(template, doc) + '\n';
//console.log(output);
}
var coverTemplate = fs.readFileSync('./templates/cover.template', 'utf8');
var objs = { fichas: output, cover: coverTemplate }
var mainTemplate = fs.readFileSync('./templates/main.template', 'utf8');
var finalOutput = mustache.render(mainTemplate, objs);
//console.log(finalOutput);
var fileNameHTML = "output.html";
var fileNamePDF = "output.pdf";
//save HTML rendered file
console.log(chalk.yellow("* Creating HTML"));
fs.writeFileSync('outputHTMLRendered/'+fileNameHTML, finalOutput);
var cmd = 'wkhtmltopdf outputHTMLRendered/'+fileNameHTML+' outputPDFRendered/'+fileNamePDF;
//console.log("" cmd);
console.log(chalk.yellow("* Creating PDF"));
child_process.exec(cmd, function(e, stdout, stderr) {
//console.log(e, stdout, stderr);
console.log(chalk.yellow.bold("* DONE"));
});