aboutsummaryrefslogtreecommitdiff
path: root/gulpfile.js
blob: 3db1297b7907168af7b777dffb6d4cd128f8fa61 (plain)
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
const gulp = require('gulp'),
	cp = require('child_process'),
	glob = require("glob"),
	fs = require("fs"),
	path = require("path");


gulp.task('icons-sprite', function (cb) {
	const columnsCount = 17,
		padding = 29,
		paddingOuter = 5,
		iconSize = 24;

	glob("_site/icons/*.svg", {}, function (er, files) {
		const iconsCount = files.length,
			rowsCount = Math.ceil(iconsCount / columnsCount),
			width = columnsCount * (iconSize + padding) + 2 * paddingOuter - padding,
			height = rowsCount * (iconSize + padding) + 2 * paddingOuter - padding;

		let svgContentSymbols = '',
			svgContentIcons = '',
			x = paddingOuter,
			y = paddingOuter;
		files.forEach(function (file, i) {
			let name = path.basename(file, '.svg');

			let svgFile = fs.readFileSync(file),
				svgFileContent = svgFile.toString();

			svgFileContent = svgFileContent
				.replace('<svg xmlns="http://www.w3.org/2000/svg"', `<symbol id="${name}"`)
				.replace(' width="24" height="24"', '')
				.replace('</svg>', '</symbol>')
				.replace(/\n\s+/g, '');

			svgContentSymbols += `\t${svgFileContent}\n`;
			svgContentIcons += `\t<use xlink:href="#${name}" x="${x}" y="${y}" width="${iconSize}" height="${iconSize}" />\n`;

			x += padding + iconSize;

			if (i % columnsCount === columnsCount - 1) {
				x = paddingOuter;
				y += padding + iconSize;
			}
		});

		const svgContent = `<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 ${width} ${height}" width="${width}" height="${height}" style="color: #354052"><rect x="0" y="0" width="${width}" height="${height}" fill="#fff"></rect>\n${svgContentSymbols}\n${svgContentIcons}\n</svg>`;

		fs.writeFileSync('icons.svg', svgContent);
		cb();
	});
});

gulp.task('icons-stroke', function (cb) {

	const icon = "disabled",
		strokes = ['.5', '1', '1.5', '2', '2.75'],
		svgFileContent = fs.readFileSync(`_site/icons/${icon}.svg`).toString(),
		padding = 16,
		paddingOuter = 5,
		iconSize = 64,
		width = (strokes.length * (iconSize + padding) - padding) + paddingOuter * 2,
		height = iconSize + paddingOuter * 2;

	let svgContentSymbols = '',
		svgContentIcons = '',
		x = paddingOuter;
	
	strokes.forEach(function (stroke) {
		let svgFileContentStroked = svgFileContent
			.replace('<svg xmlns="http://www.w3.org/2000/svg"', `<symbol id="icon-${stroke}"`)
			.replace(' width="24" height="24"', '')
			.replace(' stroke-width="2"', ` stroke-width="${stroke}"`)
			.replace('</svg>', '</symbol>')
			.replace(/\n\s+/g, '');

		svgContentSymbols += `\t${svgFileContentStroked}\n`;
		svgContentIcons += `\t<use xlink:href="#icon-${stroke}" x="${x}" y="${paddingOuter}" width="${iconSize}" height="${iconSize}" />\n`;

		x += padding + iconSize;
	});

	const svgContent = `<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 ${width} ${height}" width="${width}" height="${height}" style="color: #354052"><rect x="0" y="0" width="${width}" height="${height}" fill="#fff"></rect>\n${svgContentSymbols}\n${svgContentIcons}\n</svg>`;

	fs.writeFileSync('icons-stroke.svg', svgContent);
	cb();
});

gulp.task('optimize', function (cb) {
	glob("src/_icons/*.svg", {}, function (er, files) {

		files.forEach(function (file, i) {
			let svgFile = fs.readFileSync(file),
				svgFileContent = svgFile.toString();

			svgFileContent = svgFileContent
				.replace(/><\/(polyline|line|rect|circle|path)>/g, '/>')
				.replace(/rx="([^"]+)"\s+ry="\1"/g, 'rx="$1"')
				.replace(/\s?\/>/g, ' />')
				.replace(/\n\s*<(line|circle|path|polyline)/g, "\n  <$1")
				.replace(/polyline points="([0-9.]+)\s([0-9.]+)\s([0-9.]+)\s([0-9.]+)"/g, 'line x1="$1" y1="$2" x2="$3" y2="$4"')
				.replace(/\s+"/g, '"')
				.replace(/\n\n+/g, "\n");

			console.log('file', file);

			fs.writeFileSync(file, svgFileContent);
		});

		cb();
	});
});

gulp.task('build', function (cb) {
	cp.exec('bundle exec jekyll build', function() {

		cp.exec('rm -f ./icons/* && cp ./_site/icons/* ./icons', function() {
			cb();
		});
	})
});