/
index.js
78 lines (69 loc) · 1.74 KB
/
index.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
// Resume builder
// Read command line arguments
var argv = require('optimist')
.usage('Usage')
// Configuration file
.demand('c')
.alias('c', 'config')
.describe('c', 'Config file')
// Output file
.demand('o')
.alias('o', 'output')
.describe('o', 'Output file')
.argv
var _ = require('underscore')
var fs = require('fs')
var YAML = require('yamljs')
var wkhtmltopdf = require('wkhtmltopdf')
// Read config file
var config = YAML.load(argv.c)
// Check globals
if (config.globals) {
for (var i in config.globals) {
global[i] = config.globals[i]
}
}
// Utils for templates
global.lines = function(text, sep) {
return _.isArray(text)? text.join(sep): text
}
// Load and compile config
walkConfig(config)
// Generate HTML
var resumeHtml = config.template(config.resume)
// fs.writeFileSync('tmp.html', resumeHtml)
// Generate PDF
wkhtmltopdf(
// 'tmp.html',
resumeHtml,
{
pageSize: config.pageSize || 'A4',
output: argv.o
}
)
// fs.unlinkSync('tmp.html')
// Walk config to load templates and compile as necessary
function walkConfig(config) {
var styles = ''
config.template = _.template(fs.readFileSync(config.template).toString())
_.each(config.styles, function(style) {
styles += fs.readFileSync(style).toString()
})
config.resume.styles = styles
config.resume.personal = YAML.load(config.resume.personal)
walkSections(config.resume.sections)
}
// Walk section
function walkSections(sections) {
_.each(sections, function(section) {
loadSection(section)
})
}
function loadSection(section) {
if (section.sections) {
walkSections(section.sections)
} else {
section.content = YAML.load(section.content)
section.template = _.template(fs.readFileSync(section.template).toString())
}
}