1 | #!/usr/bin/env coffee
|
2 | path = require 'path'
|
3 | fs = require 'fs'
|
4 | jade = require 'jade'
|
5 | _ = require 'underscore'
|
6 | md = require('node-markdown').Markdown
|
7 |
|
8 | paths =
|
9 | bin: __dirname
|
10 | root: path.join __dirname, '..'
|
11 | docs: path.join __dirname, '../docs'
|
12 | srcs: path.join __dirname, '../docs/doc_files'
|
13 | jade: path.join __dirname, '../docs/templates'
|
14 |
|
15 | # Hash to store any variable needed in our template
|
16 | locals = {}
|
17 |
|
18 | # Create a hash of title => html for every doc source file
|
19 | docFiles = {}
|
20 | sources = fs.readdirSync paths.srcs
|
21 | for file in sources.sort()
|
22 | source = fs.readFileSync(path.join(paths.srcs, file)).toString()
|
23 | title = source.split("\n")[0].replace(/#/g, '').trim()
|
24 |
|
25 | link = "<a name=\"#{file}\"><hr /></a>"
|
26 | docFiles[title] =
|
27 | ref: file
|
28 | source: [link, md source].join "\n"
|
29 | locals.docs = _(docFiles).chain().values().map((val)-> val.source).value().join "\n"
|
30 |
|
31 | # Create a table of contents array
|
32 | toc = []
|
33 | for title, docObj of docFiles
|
34 | toc.push "<li><a href=\"##{docObj.ref}\">#{title}</a></li>"
|
35 | locals.toc = "<ul>" + toc.join("\n") + "</ul>"
|
36 |
|
37 | # Compile the contents of the jade doc files
|
38 | layout = fs.readFileSync path.join paths.jade, 'index.jade'
|
39 | compiled = jade.compile layout
|
40 |
|
41 | # Parse the template with our auto-generated docs and toc, and write it to `index.html`
|
42 | index = compiled locals
|
43 | fs.writeFileSync path.join(paths.root, 'index.html'), index
|