-
Notifications
You must be signed in to change notification settings - Fork 4
/
Gruntfile.js
162 lines (154 loc) · 6.36 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
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
module.exports = function (grunt) {
'use strict';
var config = grunt.file.readJSON('package.json');
var name = config.name || '';
var title = config.title || '';
var author = config.author || '';
var license = config.license || '';
var language = config.language || '';
var cover = config.cover || 'Cover.png';
var dest = config.dest || 'dist';
var base = config.base || 'book';
var styles = config.styles || 'styles';
var chapters = config.chapters || base + '/chapters/*.md';
var header = config.header || base + '/partials/head.html';
var beforeBody = config.beforeBody || base + '/partials/beforeBody.html';
var afterBody = config.afterBody || base + '/partials/afterBody.html';
var epubMetadata = config.epubMetadata || base + '/partials/epub-metadata.xml';
var compiledMarkdownFile = base + '/' + name + '.md';
grunt.registerTask('clean', ['shell:cleanMarkDown']);
grunt.registerTask('default', ['clean', 'readme', 'html', 'rtf', 'pdf', 'kindle']);
grunt.registerTask('readme', ['clean','shell:readme']);
grunt.registerTask('html', ['clean','shell:cleanHtml', 'shell:combine', 'md-book:html', 'shell:moveMarkdown']);
grunt.registerTask('epub', ['clean','shell:combine', 'replace', 'md-book:epub', 'shell:cleanEpub', 'shell:moveMarkdown']);
grunt.registerTask('rtf', ['clean','shell:combine', 'md-book:rtf', 'shell:moveMarkdown']);
grunt.registerTask('pdf', ['clean','shell:combine', 'md-book:pdf', 'shell:moveMarkdown']);
grunt.registerTask('kindle', ['epub', 'md-book:mobi']);
// Project configuration.
grunt.initConfig({
//Compile all the chapters into one markdown file.
shell: {
combine: {
command: [
'echo %' + title + ' >> ' + compiledMarkdownFile,
'echo % by ' + author + ' >> ' + compiledMarkdownFile,
'echo "" >> ' + compiledMarkdownFile,
'awk \'{print ""}{print}\' ' + chapters + ' >> ' + compiledMarkdownFile
].join('&&')
},
readme: {
command: [
'awk \'{print ""}{print}\' ' + base + '/partials/00-Share.md' + ' >> ' + compiledMarkdownFile,
'grunt shell:combine',
'awk \'{print ""}{print}\' ' + base + '/partials/00-Share.md' + ' >> ' + compiledMarkdownFile,
'mv '+ compiledMarkdownFile + ' README.md'
].join('&&')
},
cleanMarkDown: {
command: 'rm ' + compiledMarkdownFile
},
moveMarkdown: {
command: 'mv ' + compiledMarkdownFile + ' ' + dest + '/'
},
cleanEpub: {
command: 'rm ' + epubMetadata + '.temp'
},
cleanHtml: {
command: ['rm ' + dest + '/' + styles + '/html.css', 'rm ' + dest + '/index.html'].join('&&')
}
},
//Build epub metadata.
replace: {
files: {
src: epubMetadata,
dest: epubMetadata + '.temp'
},
options: {
force: true,
variables: {
'title': title,
'author': author,
'license': license,
'language': language
}
}
},
//Compile the one markdown file into seperate output files.
//First Download and Install PanDoc
//http://johnmacfarlane.net/pandoc/installing.html
'md-book': {
options: {
dest: dest
},
html: {
files: compiledMarkdownFile,
options: {
'-o': 'index.html ',
'-s': '-t html5' +
' --title-prefix "' + title + '" ' +
' --include-in-header ' + header +
' --include-before-body ' + beforeBody +
' --include-after-body ' + afterBody +
' --normalize ' +
' --smart ' +
' --toc ' +
' --self-contained',
'-c': base + '/' + styles + '/html.css'
}
},
epub: {
files: compiledMarkdownFile,
options: {
'-o': name + '.epub',
'-s': '-t epub ' +
' --title-prefix "' + title + '" ' +
' --epub-metadata ' + epubMetadata + '.temp' +
' --epub-stylesheet ' + base + '/' + styles + '/epub.css' +
' --epub-cover-image ' + base + '/' + cover +
' --normalize ' +
' --smart ' +
' --toc '
}
},
//Download: http://www.amazon.com/gp/feature.html?ie=UTF8&docId=1000765211
//Symlink bin: ln -s /path/to/kindlegen /usr/local/bin
mobi: {
files: dest + '/' + name + '.epub',
options: {
'kindle': true
}
},
//You need `pdflatex`
//OS X: http://www.tug.org/mactex/
//Then find its path: find /usr/ -name "pdflatex"
//Then symlink it: ln -s /path/to/pdflatex /usr/local/bin
pdf: {
files: compiledMarkdownFile,
options: {
'-o': name + '.pdf',
'-s': '' +
' --title-prefix "' + title + '" ' +
' --epub-cover-image ' + base + '/' + cover +
' --normalize ' +
' --smart ' +
' --toc '
}
},
rtf: {
files: compiledMarkdownFile,
options: {
'-o': name + '.rtf',
'-s': '' +
' --title-prefix "' + title + '" ' +
' --normalize ' +
' --smart ' +
' --toc '
}
}
}
});
//tasks for converting markdown files
grunt.loadTasks('tasks/');
grunt.loadNpmTasks('grunt-shell');
grunt.loadNpmTasks('grunt-replace');
};