Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e412ea3

Browse files
committed
Break apart themify into discrete tasks.
1 parent 07a0667 commit e412ea3

File tree

7 files changed

+157
-105
lines changed

7 files changed

+157
-105
lines changed

Gruntfile.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ module.exports = function (grunt) {
1414
grunt.registerTask('lint', ['jshint', 'eslint']);
1515
grunt.registerTask('coverage', ['mocha_istanbul']);
1616
grunt.registerTask('mocha', ['mochaTest']);
17+
grunt.registerTask('themify', ['templates', 'css', 'images', 'content']);
1718
grunt.registerTask('test', ['lint', 'build', 'coverage']);
1819
grunt.registerTask('develop', ['browserify', 'themify']);
1920
grunt.registerTask('build', ['browserify', 'themify', 'uglify', 'usebanner']);

dist/all.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tasks/content.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
'use strict';
2+
3+
4+
function base64(str) {
5+
return 'data:image/png;base64,' + str.toString('base64');
6+
}
7+
8+
9+
module.exports = function content(grunt) {
10+
11+
var src = 'dist/all.js',
12+
tokens = {
13+
logo: '$LOGO$',
14+
primary: '$WORDMARK_PRIMARY$',
15+
secondary: '$WORDMARK_SECONDARY$'
16+
};
17+
18+
function processContent(str) {
19+
var bundles = grunt.file.expand('locales/**/*.properties'),
20+
content = {},
21+
props, locale;
22+
23+
bundles.forEach(function (bundle) {
24+
locale = bundle.split(/locales\/([A-Z]{2})\/([a-z]{2})\//);
25+
locale = locale[2] + '_' + locale[1];
26+
27+
content[locale] = {};
28+
29+
props = grunt.file.read(bundle);
30+
props = props.split(/\n/);
31+
32+
props.forEach(function (line) {
33+
var pair = line.split(/=(.+)/);
34+
35+
if (pair[0]) {
36+
content[locale][pair[0]] = pair[1];
37+
}
38+
});
39+
});
40+
41+
str = str.replace('\'$STRINGS$\'', JSON.stringify(content));
42+
43+
return str;
44+
}
45+
46+
grunt.registerTask('content', 'Grabs localized content and injects it into the JavaScript', function () {
47+
var out = grunt.file.read(src);
48+
49+
out = processContent(out);
50+
51+
grunt.file.write(src, out);
52+
});
53+
54+
};

tasks/css.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
'use strict';
2+
3+
4+
function trim(str) {
5+
return str.replace(/(^\s+|\s+$)/g, '').replace(/(\r\n|\n|\r)/g, '');
6+
}
7+
8+
9+
module.exports = function css(grunt) {
10+
11+
var src = 'dist/all.js';
12+
13+
function processCss(str) {
14+
var styles = trim(grunt.file.read('src/theme/css/index.css'));
15+
16+
return str.replace('$STYLES$', styles);
17+
}
18+
19+
grunt.registerTask('css', 'Injects css into the JavaScript', function () {
20+
var out = grunt.file.read(src);
21+
22+
out = processCss(out);
23+
24+
grunt.file.write(src, out);
25+
});
26+
27+
};

tasks/images.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
'use strict';
2+
3+
4+
function base64(str) {
5+
return 'data:image/png;base64,' + str.toString('base64');
6+
}
7+
8+
9+
module.exports = function images(grunt) {
10+
11+
var src = 'dist/all.js',
12+
tokens = {
13+
logo: '$LOGO$',
14+
primary: '$WORDMARK_PRIMARY$',
15+
secondary: '$WORDMARK_SECONDARY$'
16+
};
17+
18+
function processImages(str) {
19+
var logo = grunt.file.read('src/theme/images/logo.png', { encoding: null }),
20+
primary = grunt.file.read('src/theme/images/wordmark_white.png', { encoding: null }),
21+
secondary = grunt.file.read('src/theme/images/wordmark_blue.png', { encoding: null });
22+
23+
str = str.replace(tokens.logo, base64(logo));
24+
str = str.replace(tokens.primary, base64(primary));
25+
str = str.replace(tokens.secondary, base64(secondary));
26+
27+
return str;
28+
}
29+
30+
grunt.registerTask('images', 'Base64 encodes images and injects them into the JavaScript', function () {
31+
var out = grunt.file.read(src);
32+
33+
out = processImages(out);
34+
35+
grunt.file.write(src, out);
36+
});
37+
38+
};

tasks/templates.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
'use strict';
2+
3+
4+
function trim(str) {
5+
return str.replace(/(^\s+|\s+$)/g, '').replace(/(\r\n|\n|\r)/g, '');
6+
}
7+
8+
9+
module.exports = function templates(grunt) {
10+
11+
var src = 'dist/all.js';
12+
13+
function processTemplates(str) {
14+
var files = grunt.file.expand('src/theme/**/*.html'),
15+
templates = {},
16+
name;
17+
18+
files.forEach(function (file) {
19+
name = file.split(/src\/theme\/(.*).html/);
20+
name = name[1];
21+
22+
templates[name] = trim(grunt.file.read(file));
23+
});
24+
25+
return str.replace('\'$TEMPLATES$\'', JSON.stringify(templates));
26+
}
27+
28+
grunt.registerTask('templates', 'Injects templates into the JavaScript', function () {
29+
var out = grunt.file.read(src);
30+
31+
out = processTemplates(out);
32+
33+
grunt.file.write(src, out);
34+
});
35+
36+
};

tasks/themify.js

Lines changed: 0 additions & 104 deletions
This file was deleted.

0 commit comments

Comments
 (0)