-
Notifications
You must be signed in to change notification settings - Fork 8
/
Gruntfile.js
177 lines (158 loc) · 5.57 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
module.exports = function(grunt) {
'use strict';
var ejs = require('ejs');
var cheerio = require('cheerio');
// debug switch
var debug = false;
// register buildPage task
grunt.task.registerTask('buildPage', 'a task to build page', function () {
var paths = grunt.file.expand([
'**/*.html',
'!node_modules/**/*.html',
'!templates/**/*.html',
'!deploy/**/*.html'
]);
paths.forEach(function (item, index) {
try {
var str = grunt.file.read(item);
var exec = /<config>([\w\W]+)<\/config>/gmi.exec(str);
var config = JSON.parse(exec[1]);
config.keywords = config.keywords || config.title;
config.description = config.description || config.title;
// adjust dir level
var dirLevel = item.match(/\//g).length;
var chdir = '';
while(dirLevel > 0) {
chdir += '../';
dirLevel--;
}
config.chdir = chdir;
str = str.replace(exec[0], '');
// encoding <, >, ", "
str = str.replace(/<pre>([\w\W]+?)<\/pre>/g, function (s, $1) {
return '<pre>' +
$1.trim().replace(/</g, '<').replace(/>/g, '>').replace(/\"/g, '"') +
'<\/pre>';
});
var content = str.trim();
var header = grunt.file.read('templates/header.html');
var footer = grunt.file.read('templates/footer.html');
var html = (header + content + footer).trim();
var res = ejs.render(html, config);
// move link/style tag to head
var $ = cheerio.load(res, {decodeEntities: false, recognizeSelfClosing: false});
var style = $('body link, body style');
var head = $('head');
style.each(function (index, item) {
head.append(item);
});
// move script tag to bottom
var script = $('.container script');
var cnzz = $('#cnzz');
script.each(function (index, item) {
cnzz.before(item);
});
grunt.file.write('deploy/' + item, $.html());
grunt.log.ok('build deploy/' + item);
} catch (e) {
// no config info, jump build
grunt.log.error('jump deploy/' + item);
}
});
});
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
clean: {
main: {
src: 'deploy'
}
},
copy: {
all: {
files: [
{
src: ['**/*', '!node_modules/**', '!templates/**', '!Gruntfile.js', '!package.json', '!README.md'],
dest: 'deploy/'
}
]
},
static: {
files: [
{
src: ['static/**/*'],
dest: 'deploy/'
}
]
},
debugJs: {
files: [
{
expand: true,
cwd: '',
src: ['static/js/*.js', '!static/js/*.min.js'],
dest: '',
ext: '.min.js'
}
]
}
},
compass: {
main: {
options: {
basePath: 'static/',
sassDir: 'sass',
cssDir: 'css',
imagesDir: 'img',
javascriptsDir: 'js',
outputStyle: debug ? 'expanded' : 'compressed',
force: true,
relativeAssets: true,
noLineComments: true,
assetCacheBuster: false
}
}
},
uglify: {
main: {
files: [{
expand: true,
cwd: '',
src: ['static/js/*.js', '!static/js/*.min.js'],
dest: '',
ext: '.min.js'
}]
}
},
connect: {
server: {
options: {
port: 3000,
hostname: 'localhost',
base: 'deploy'
}
}
},
watch: {
html: {
files: ['**/*.html', '!node_modules/**', '!deploy/**'],
tasks: ['buildPage']
},
sass: {
files: ['static/**/*.scss'],
tasks: ['compass', 'copy:static']
},
js: {
files: ['static/js/*.js'],
tasks: [debug ? 'copy:debugJs' : 'uglify', 'copy:static']
}
}
});
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.loadNpmTasks('grunt-contrib-clean');
grunt.loadNpmTasks('grunt-contrib-copy');
grunt.loadNpmTasks('grunt-contrib-uglify');
grunt.loadNpmTasks('grunt-contrib-connect');
grunt.loadNpmTasks('grunt-contrib-compass');
grunt.registerTask('default', ['clean', 'compass', debug ? 'copy:debugJs' : 'uglify', 'copy:all', 'buildPage']);
grunt.registerTask('server', ['default', 'connect', 'watch']);
};