forked from jlord/git-it
-
Notifications
You must be signed in to change notification settings - Fork 0
/
buildpages.js
135 lines (118 loc) · 3.67 KB
/
buildpages.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
var Handlebars = require('handlebars')
var fs = require('fs')
var glob = require('glob')
var translateLocale = require('./translate-locale.js')
var layout = fs.readFileSync(__dirname + '/layout.hbs').toString()
var thefiles = []
// Take in a language type if any
var lang = process.argv[2]
var rawFiles = __dirname + (lang ? '/raw-content-' + lang + '/' : '/raw-content/')
var builtContent = __dirname + (lang ? '/challenges-' + lang + '/' : '/challenges/')
// I can probably use glob better to avoid
// finding the right files within the files
glob("*.html", {cwd: rawFiles}, function (err, files) {
thefiles = files
if (err) return console.log(err)
// var matches = files.map(function(file) {
// if (file.match('guide/raw-content/')) {
// return file
// }
// })
buildPage(files)
})
function buildPage(files) {
files.forEach(function(file) {
// shouldn't have to do this if my
// mapping were correct
if (!file) return
// if language, run the noun and verb
// translations
var content = {
header: buildHeader(file),
footer: buildFooter(file),
body: fs.readFileSync(rawFiles + file).toString()
}
if (lang) {
content.body = translateLocale(content.body, lang)
}
var shortname = makeShortname(file)
var template = Handlebars.compile(layout)
var final = template(content)
fs.writeFileSync(builtContent + shortname + 'html', final)
})
// hard coded right now because, reasons
console.log("Built!")
}
function makeShortname(filename) {
// FROM guide/raw-content/10_merge_tada.html
// TO merge_tada.
return filename.split('/').pop().split('_')
.slice(1).join('_').replace('html', '')
}
function makeTitleName(filename) {
var short = makeShortname(filename).split('_')
.join(' ').replace('.', '')
return grammarize(short)
}
function buildHeader(filename) {
var num = filename.split('/').pop().split('_')[0]
var data = getPrevious(num)
var title = makeTitleName(filename)
var source = fs.readFileSync(__dirname + '/partials/header.html').toString()
var template = Handlebars.compile(source)
var content = {
challengetitle: title,
challengenumber: num,
lang: lang ? '-' + lang : '',
preurl: data.preurl,
nexturl: data.nexturl
}
return template(content)
}
function grammarize(name) {
var correct = name
var wrongWords = ['arent', 'githubbin', 'its']
var rightWords = ["aren't", "GitHubbin", "it's"]
wrongWords.forEach(function(word, i) {
if (name.match(word)) {
correct = name.replace(word, rightWords[i])
}
})
return correct
}
function buildFooter(file) {
var num = file.split('/').pop().split('_')[0]
var data = getPrevious(num)
data.lang = lang ? '-' + lang : ''
var source = fs.readFileSync(__dirname + '/partials/footer.html').toString()
var template = Handlebars.compile(source)
return template(data)
}
function getPrevious(num) {
var pre = parseInt(num) - 1
var next = parseInt(num) + 1
var preurl = ''
var prename = ''
var nexturl = ''
var nextname = ''
thefiles.forEach(function(file) {
if (pre === 0) {
prename = "All Challenges"
preurl = lang ? '../index-' + lang + '.html' : '../index.html'
} else if (file.match(pre)) {
prename = makeTitleName(file)
var getridof = pre + '_'
preurl = file.replace(getridof, '')
}
if (next === 12) {
nextname = "Done!"
nexturl = lang ? '../index-' + lang + '.html' : '../index.html'
} else if (file.match(next)) {
nextname = makeTitleName(file)
var getridof = next + '_'
nexturl = file.replace(getridof, '')
}
})
return {prename: prename, preurl: preurl,
nextname: nextname, nexturl: nexturl}
}