1
0
mirror of https://github.com/simple-icons/simple-icons.git synced 2025-01-25 01:32:58 +02:00
simple-icons/src/build.js

174 lines
5.9 KiB
JavaScript
Raw Normal View History

2015-10-14 16:52:04 +01:00
// Get JSON from source file
var source = require('./simple-icons.json');
// Loop through icons
for (var i = 0; i < source.icons.length; i++) {
var hex = source.icons[i].hex;
// Add red, green and blue values to the JSON object
var red = parseInt(hex.substr(0,2), 16) / 255;
var green = parseInt(hex.substr(2,2), 16) / 255;
var blue = parseInt(hex.substr(4,2), 16) / 255;
// Add hue to the JSON object
var max = Math.max(red, green, blue);
var min = Math.min(red, green, blue);
var delta = max - min;
2015-10-16 14:35:52 +01:00
source.icons[i].luminance = 100 * (max + min) / 2;
2015-10-14 16:52:04 +01:00
if (delta === 0) {
var hue = 0;
2015-10-16 15:43:13 +01:00
source.icons[i].saturation = 0;
2015-10-14 16:52:04 +01:00
} else {
2015-10-16 15:43:13 +01:00
if (source.icons[i].luminance < 50) {
source.icons[i].saturation = 100 * (max - min) / (max + min);
} else {
source.icons[i].saturation = 100 * (max - min) / (2 - max - min);
}
2015-10-14 16:52:04 +01:00
if (max === red) {
var hue = ((green - blue) / delta) * 60;
if (hue < 0) {
hue += 360;
}
} else if (max === green) {
var hue = (((blue - red) / delta) + 2) * 60;
} else {
var hue = (((red - green) / delta) + 4) * 60;
}
}
source.icons[i].hue = hue;
}
// Sort icons by hue
2015-10-15 14:30:13 +01:00
for (var i = 0; i < source.icons.length; i++) {
2015-11-10 11:22:45 +00:00
source.icons[i].hue += 90;
2015-10-15 14:30:13 +01:00
source.icons[i].hue = source.icons[i].hue % 360;
}
2015-10-14 16:52:04 +01:00
source.icons.sort(function(a, b) {
return parseFloat(a.hue) - parseFloat(b.hue);
2015-10-14 16:52:04 +01:00
});
2015-10-15 14:30:13 +01:00
var tmp = [];
for (var i = 0; i < source.icons.length; i++) {
if (source.icons[i].luminance < 15) {
2015-10-15 14:30:13 +01:00
tmp.push(source.icons[i]);
source.icons.splice(i,1);
i--;
}
}
2015-10-16 14:35:52 +01:00
for (var i = 0; i < source.icons.length; i++) {
if (source.icons[i].saturation < 25) {
2015-10-16 14:35:52 +01:00
tmp.push(source.icons[i]);
source.icons.splice(i,1);
i--;
}
}
2015-10-15 14:30:13 +01:00
tmp.sort(function(a, b) {
return parseFloat(b.luminance) - parseFloat(a.luminance);
});
for (var i = 0; i < tmp.length; i++) {
source.icons.push(tmp[i]);
}
2015-10-14 16:52:04 +01:00
// Read header and footer content into variables
var fs = require('fs');
function readFile(path, callback) {
try {
var filename = require.resolve(path);
fs.readFile(filename, 'utf8', callback);
} catch (e) {
callback(e);
}
}
var fs = require('fs');
2015-10-22 14:57:08 +01:00
var header = fs.readFileSync('./header.html', 'utf8');
var footer = fs.readFileSync('./footer.html', 'utf8');
2015-10-14 16:52:04 +01:00
2015-10-15 14:30:13 +01:00
// Build content
2016-08-04 15:48:09 +01:00
var main = " <ul class=\"tiles tiles--full\">";
2015-10-15 14:30:13 +01:00
2015-10-14 16:52:04 +01:00
for (var i = 0; i < source.icons.length; i++) {
2015-10-17 11:38:33 +01:00
var fileName = source.icons[i].title.toLowerCase();
2016-05-22 10:39:53 +01:00
fileName = fileName.replace(/[!|’|.| |-]/g, ''); // Replace bang, apostrophe, period and space with nothing.
2015-12-10 10:33:15 +00:00
fileName = fileName.replace(/[+]/, 'plus'); // Replace the plus symbol with “plus”.
2015-10-22 14:57:08 +01:00
filePath = "../icons/" + fileName + ".svg";
2015-10-15 14:30:13 +01:00
var fs = require('fs');
var svg = fs.readFileSync(filePath, 'utf8');
2016-08-04 15:37:48 +01:00
var searchTerms = source.icons[i].title.toLowerCase() + " " + source.icons[i].hex.toLowerCase();
if (source.icons[i].title.toLowerCase() != fileName.toLowerCase()) {
searchTerms = searchTerms + " " + fileName.toLowerCase();
}
main += "\n <li class=\"tiles__item\" data-search=\"" + searchTerms + "\" style=\"background-color:#" + source.icons[i].hex + "\"><a href=\"https://simpleicons.org/icons/" + fileName + ".svg\" class=\"icon--link\" title=\"" + source.icons[i].title + "\">" + svg + "<span class=\"tile-name\">" + source.icons[i].title + "</span></a>" + "<span class=\"hex\">#" + source.icons[i].hex + "</span></li>";
2015-10-14 16:52:04 +01:00
}
// Put all content together and export to index.html
var htmlOutput = header + main + footer;
2015-10-22 14:57:08 +01:00
fs.writeFile("../index.html", htmlOutput, function(err) {
2015-10-14 16:52:04 +01:00
if(err) {
return console.log(err);
}
2016-01-07 18:41:17 +00:00
console.log("The index.html file was built with " + source.icons.length + " icons!");
2015-10-19 11:08:22 +01:00
});
2015-10-19 16:38:48 +01:00
// Also output to 404.html
2015-10-22 14:57:08 +01:00
fs.writeFile("../404.html", htmlOutput, function(err) {
2015-10-19 16:38:48 +01:00
if(err) {
return console.log(err);
}
2016-01-07 18:41:17 +00:00
console.log("The 404.html file was built with " + source.icons.length + " icons!");
2016-08-04 16:26:46 +01:00
});
var sass = "// Brand colours from simpleicons.org\n";
var less = "// Brand colours from simpleicons.org\n";
var maxNameLength = 0;
for (var i = 0; i < source.icons.length; i++) {
var fileName = source.icons[i].title.toLowerCase();
fileName = fileName.replace(/[!|’|.| |-]/g, ''); // Replace bang, apostrophe, period and space with nothing.
fileName = fileName.replace(/[+]/, 'plus'); // Replace the plus symbol with “plus”.
if (fileName.length > maxNameLength) {
maxNameLength = fileName.length;
}
}
// Sort icons alphabetically
source.icons.sort(function(a, b) {
if (a.title < b.title) {
return -1;
}
if (a.title > b.title) {
return 1;
}
// names must be equal
return 0;
});
for (var i = 0; i < source.icons.length; i++) {
var fileName = source.icons[i].title.toLowerCase();
fileName = fileName.replace(/[!|’|.| |-]/g, ''); // Replace bang, apostrophe, period and space with nothing.
fileName = fileName.replace(/[+]/, 'plus'); // Replace the plus symbol with “plus”.
spacing = "";
if (fileName.length < maxNameLength) {
spacing = " ".repeat(maxNameLength - fileName.length);
}
sass += "\n$color-brand-" + fileName.toLowerCase() + ": " + spacing + "#" + source.icons[i].hex.toUpperCase() + ";";
less += "\n@color-brand-" + fileName.toLowerCase() + ": " + spacing + "#" + source.icons[i].hex.toUpperCase() + ";";
}
// Generate Sass file with colour variables
fs.writeFile("../colour-variables.scss", sass, function(err) {
if(err) {
return console.log(err);
}
console.log("The Sass file was built");
});
// Generate Less file with colour variables
fs.writeFile("../colour-variables.less", less, function(err) {
if(err) {
return console.log(err);
}
console.log("The Less file was built");
2015-10-14 16:52:04 +01:00
});