forked from StylishThemes/StackOverflow-Dark
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGruntfile.js
247 lines (233 loc) · 8.92 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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
module.exports = function (grunt) {
'use strict';
var config, getTheme, file;
try {
config = grunt.file.readJSON('build.json');
} catch (err) {
console.info('build.json not found - using defaults');
config = {
'theme' : 'desert',
'color' : '#4183C4',
'visited' : '#4183C4',
'font' : 'Menlo',
'image' : 'url("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAEYAAABGCAAAAABURb1YAAAFnklEQVR4AWWXCY4cwXbE6h4JMhCvMPc/ov+3Ox9QsLCGpKlFHILd+UQ559AGDkBSFXWmomonqkL/XuUcIPUcONjHCJyE36KaqNiaRnVCG9TGpgIU9hIfohCBVGj24ammo04lU3Fy0NZDAyZi5AElHjgEGlRbG1WjraIt7TmADQkAaoEHsOXAAaeqat9RVTqjosyEH5W+DQCQt8BzIJMfXJI3e5tGRefSlRn3NrO3scnvNl26FN5RITGNfjm3GM/+FhQgnlQeImC+dE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zfeYAHPwy/Rb43L9TLtMt8CtwW1xvQvbR1g9n7qJRNN1kdxcdH7cM58Szxq679MdLOhE72t6VNEpHHkzwRyjBcGth+dE99pIkVbWaEWmvnQ8Yl7Mp59zpRWXdNo6odrO754Zzvxf/010nP7onb1TR+bgbVPB9PcADJvH/043Kvk0UzLRxQ6UiaeTAcyxYl2lyrrvZv8v2wE7U1uVcST0821jJeIDKWuyxiqnuko7q3+zJYXJ46nV3D67EyL+lQ9F2qkr+3kUafQpwYE8T329p6y7OX1TF97fQv/c+l87zj7Hsgpig4qRLd9fMPs74rLGyCzRbhmVqpy7nqvPmTD1gvKe7b2U8u1QzmtuDuEtbgT3c8QBJveo6cueFkUwv59nVmfFw1jl4/unuOT9qERV76ers8n23z/c8/Xy7a4lwwLplmGusbRp33Up13+ZDN3KPxNVMhY62vUQyRTse6+cKHtZYjIfDh260Cjbb3Vy6R8H8SMBzqSwrsB1UTacoknlnOb8V4JD9Njfpwz/fHjpBUeevcAtcl65sqXPX9D87RUTLc/dfOgAAAABJRU5ErkJggg==")',
'tiled' : true,
'attach' : 'scroll',
'webkit' : false
};
}
file = (config.theme || '').toLowerCase().replace(/\s+/g, '-');
function loadTheme() {
let data;
config.themeFile = 'themes/' + file + '.min.css';
if (grunt.file.exists(config.themeFile)) {
data = grunt.file.read(config.themeFile);
} else {
grunt.log.writeln(`\n**** Syntax theme: "${file}" not found; falling back to a basic dark syntax file ****`['yellow'].bold);
config.themeFile = '';
file = '';
// fallback to a basic dark syntax if no theme chosen
data = '.pln,code,pre{background:#333!important;color:#ccc!important}.str{color:#0a0!important}' +
'.lit{color:#d65!important}.pun{color:#999!important}.kwd{color:#08f!important}.tag{color:#999!important}' +
'.atn{color:#088!important}.atv{color:#0a0!important}.dec{color:#90a!important}';
}
config.newTheme = data;
}
// updates file variable if theme not found
loadTheme();
// ** set up build options **
config.sourceFile = 'stackoverflow-dark.css';
// build file name
config.buildFile = 'stackoverflow-dark-' + (file ? file : 'default') + '-' + config.color.replace(/[^\d\w]/g, '') + '.build.min.css';
// background options
config.bgOptions = config.tiled ?
'background-repeat: repeat !important; background-size: auto !important; background-position: left top !important;' :
'background-repeat: no-repeat !important; background-size: cover !important; background-position: center top !important;';
config.bgAttachment = config.attach.toLowerCase() === 'scroll' ? 'scroll' : 'fixed';
// custom build
config.replacements = [{
pattern: /\/\*\[\[bg-choice\]\]\*\/ url\(.*\)/,
replacement: config.image
},{
pattern: '/*[[bg-options]]*/',
replacement: config.bgOptions
},{
pattern: '/*[[bg-attachment]]*/ fixed',
replacement: config.bgAttachment
},{
pattern: /\/\*\[\[base-color\]\]\*\/ #\w{3,6}/g,
replacement: config.color
},{
pattern: /\/\*\[\[visited-color\]\]\*\/ #\w{3,6}/g,
replacement: config.visited
},{
pattern: '/*[[font-choice]]*/',
replacement: config.font
},{
// remove default syntax theme
pattern: /\s+\/\* grunt-remove-block-below (.*(\n|\r))+\s+\/\* grunt-remove-block-above \*\//gm,
replacement: ''
},{
pattern: '/*[[syntax-theme]]*/',
// add selected theme
replacement: config.newTheme
}];
// userstyles.org - remove defaults & leave placeholders
config.replacements_user = [{
pattern: /\/\*\[\[bg-choice\]\]\*\/ url\(.*\)/,
replacement: '/*[[bg-choice]]*/'
},{
pattern: '/*[[bg-attachment]]*/ fixed',
replacement: '/*[[bg-attachment]]*/'
},{
pattern: /\/\*\[\[base-color\]\]\*\/ #\w{3,6}/g,
replacement: '/*[[base-color]]*/'
},{
pattern: /\/\*\[\[visited-color\]\]\*\/ #\w{3,6}/g,
replacement: '/*[[visited-color]]*/'
},{
// remove default syntax theme
pattern: /\s+\/\* grunt-remove-block-below (.*(\n|\r))+\s+\/\* grunt-remove-block-above \*\//m,
replacement: ''
}];
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
config: config,
'string-replace': {
inline: {
files: { '<%= config.buildFile %>' : '<%= config.sourceFile %>' },
options: { replacements: '<%= config.replacements %>' }
},
mark: {
files: { '<%= config.buildFile %>' : '<%= config.buildFile %>' },
options: {
replacements: [{
pattern: /\/\*\[\[/gm,
replacement: '/*![['
}]
}
},
unmark: {
files: { '<%= config.buildFile %>' : '<%= config.buildFile %>' },
options: {
replacements: [{
pattern: /\/\*\!\[\[/gm,
replacement: '/*[['
}]
}
},
fix: {
files: { '<%= config.buildFile %>' : '<%= config.buildFile %>' },
options: {
replacements: [{
pattern: /\;\:\/\*\[\[/gm,
replacement: ';/*[['
}]
}
},
// Webkit browser, unwrap main @-moz-document
webkit : {
files: { '<%= config.buildFile %>' : '<%= config.buildFile %>' },
options: {
replacements: [{
pattern: /(@-moz-document(.*)\{(\n|\r)+)/m,
replacement: ''
},{
pattern: /\/\* grunt-webkit-remove-to-end-of-file(.*(\n|\r))+\}$/gm,
replacement: ''
}]
}
},
// remove webkit placeholder
webkitplaceholder: {
files: { '<%= config.buildFile %>' : '<%= config.buildFile %>' },
options: {
replacements: [{
pattern: ' /* grunt-webkit-remove-to-end-of-file */',
replacement: ''
}]
}
}
},
clean: {
themes: {
src: [ 'themes/*.min.css' ]
}
},
cssmin: {
minify: {
files: { '<%= config.buildFile %>' : '<%= config.buildFile %>' },
options: {
keepSpecialComments: '*',
advanced: false
}
},
themes: {
files: [{
expand : true,
cwd : 'themes/src/',
src : '*.css',
dest : 'themes/',
ext : '.min.css'
}],
options: {
keepSpecialComments: '*',
advanced: false
}
}
},
watch: {
files: [ '<%= config.sourceFile %>' ],
tasks: [ 'default' ]
}
});
grunt.loadNpmTasks('grunt-string-replace');
grunt.loadNpmTasks('grunt-contrib-clean');
grunt.loadNpmTasks('grunt-contrib-cssmin');
grunt.loadNpmTasks('grunt-contrib-watch');
// build custom stackoverflow-Dark style using build.json settings
grunt.registerTask('default', 'Building custom style', function() {
config.buildFile = config.buildFile.replace('.min.css', '.css');
grunt.task.run(['string-replace:inline']);
if (config.webkit) {
grunt.task.run(['string-replace:webkit']);
} else {
grunt.task.run(['string-replace:webkitplaceholder']);
}
});
// build custom minified stackoverflow-Dark style
grunt.registerTask('minify', 'Building custom minified style', function() {
grunt.task.run(['string-replace:inline']);
if (config.webkit) {
grunt.task.run(['string-replace:webkit']);
}
grunt.task.run(['cssmin:minify']);
});
// build userstyle for pasting into https://userstyles.org/styles/35345/stackoverflow-dark
grunt.registerTask('user', 'building userstyles.org file', function () {
config.buildFile = 'stackoverflow-dark-userstyle.build.css';
config.replacements = config.replacements_user;
grunt.task.run([
'string-replace:inline',
'string-replace:webkitplaceholder'
]);
});
grunt.registerTask('usermin', 'building userstyles.org file', function () {
config.buildFile = 'stackoverflow-dark-userstyle.build.css';
config.replacements = config.replacements_user;
grunt.task.run([
'string-replace:inline',
'string-replace:mark',
'cssmin:minify',
'string-replace:unmark',
'string-replace:fix'
]);
});
// build custom minified stackoverflow-Dark style
grunt.registerTask('themes', 'Rebuild minified theme files', function() {
grunt.task.run([
'clean',
'cssmin:themes'
]);
});
};