const gulp = require('gulp'), glob = require("glob"), fs = require("fs"), path = require("path"), svg2img = require('svg2img'); 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('', '') .replace(/\n\s+/g, ''); svgContentSymbols += `\t${svgFileContent}\n`; svgContentIcons += `\t\n`; x += padding + iconSize; if (i % columnsCount === columnsCount - 1) { x = paddingOuter; y += padding + iconSize; } }); const svgContent = `\n${svgContentSymbols}\n${svgContentIcons}\n`; 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/tabler-${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('', '') .replace(/\n\s+/g, ''); svgContentSymbols += `\t${svgFileContentStroked}\n`; svgContentIcons += `\t\n`; x += padding + iconSize; }); const svgContent = `\n${svgContentSymbols}\n${svgContentIcons}\n`; fs.writeFileSync('icons-stroke.svg', svgContent); cb(); }); gulp.task('optimize', function (cb) { glob("_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(); }); });