forked from jlord/sheetsee.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
buildpage.js
75 lines (68 loc) · 1.93 KB
/
buildpage.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
#!/usr/bin/env node
var glob = require('glob')
var fs = require('fs')
var marked = require('marked')
var hbs = require('handlebars')
var mkdirp = require('mkdirp')
var path = require('path')
var cpr = require('cpr')
cpr('./demos', './site/demos', function(err, files) {
if (err) return console.log(err)
cpr('./js', './site/js', function(err, files) {
if (err) return console.log(err)
})
})
fs.readFile('readme.md', function(err, file) {
if (err) return console.log(err)
var name = "index"
var content = file.toString()
var html = changeExtensions(marked(content))
applyTemplate(html, name)
})
glob("docs/*.md", function (err, files) {
if (err) return console.log(err)
var filenames = files.map(function(name) {
return path.basename(name)
})
filenames.forEach(function (file) {
var name = file.split('.md')[0]
var filePath = "./docs/"
var content = fs.readFileSync(filePath + file).toString()
var html = changeExtensions(marked(content))
applyTemplate(html, name)
})
})
function applyTemplate(html, name) {
var content = {content: html, name: name}
if (name === "index") {
content.rootstyle = "."
content.rootdoc = "docs"
content.rootdemo = "."
} else {
content.rootstyle = ".."
content.rootdoc = "."
content.rootdemo = ".."
}
var file = "template.hbs"
var rawTemplate = fs.readFileSync(file).toString()
var template = hbs.compile(rawTemplate)
var page = template(content)
writeFile(page, name)
}
function writeFile(page, name) {
if (name === "index") {
return fs.writeFileSync('./site/' + name + '.html' , page)
}
mkdirp('./site/docs', function (err) {
if (err) return console.error(err)
fs.writeFileSync('./site/docs/' + name + '.html' , page)
})
}
function changeExtensions(html, name) {
if (name === "index") {
html = html.replace('/\./\.\/', '')
}
var re = /.md$/
var newHtml = html.replace(/\.md/g, '.html')
return newHtml
}