-
Notifications
You must be signed in to change notification settings - Fork 3
/
Gruntfile.js
92 lines (82 loc) · 2.64 KB
/
Gruntfile.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
var _ = require('underscore');
var cheerio = require('cheerio');
var eyes = require('eyes');
var S = require('string');
module.exports = function(grunt) {
grunt.initConfig(
{
pkg: grunt.file.readJSON('package.json'),
clean: ['docs/*'],
markdown: {
docs: {
files: 'src/*.md',
dest: 'docs/',
template: 'template/partial.html',
options: {
gfm: true
}
}
},
connect: {
docs: {
options: {
port: 9092,
base: './',
keepalive: true
}
}
},
htmlbuild: {
dist: {
src: 'template/index.html',
dest: '',
options: {
beautify: true,
sections: {
toc: 'docs/toc.html',
views: 'docs/[0-9]*-*.html'
},
styles: {
dist: 'assets/css/*.css'
},
scripts: {
dist: ['assets/js/prism.js', 'assets/js/*.js']
}
}
}
},
watch: {
docs: {
files: ['src/*', 'assets/*'],
tasks: ['default']
}
},
toc: {
'docs/toc.html': 'docs/[0-9]*-*'
}
});
grunt.task.registerMultiTask('toc', 'Generate TOC', function() {
var files = grunt.file.expand(this.data);
var content = _.reduce(files, function(memo,file) {
var $ = cheerio.load(grunt.file.read(file));
var nodes = $('a[id]');
var title = file.trim().replace(/docs\/[0-9]*-(.*).html/, '$1');
var html = '<h2>' + S(title).capitalize().s + '</h2>';
html += '<ul>';
html += nodes.map(function(i, elem) {
var node = $(this);
var title = node.parent().text().trim();
return '<li><a href="#' + node.attr('id') + '">' + title + '</a></li>';
}).join('');
html += '</ul>';
return memo + html;
}, '');
grunt.file.write(this.target, content);
});
grunt.loadNpmTasks('grunt-markdown');
grunt.loadNpmTasks('grunt-contrib-connect');
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.loadNpmTasks('grunt-contrib-clean');
grunt.loadNpmTasks('grunt-html-build');
grunt.registerTask('default', ['clean', 'markdown', 'toc', 'htmlbuild']);
};