Skip to content

Commit

Permalink
Merge pull request #698 from matuzalemsteles/issue-696
Browse files Browse the repository at this point in the history
Add generate icon data | Fixes #696
  • Loading branch information
carloslancha authored Mar 6, 2018
2 parents ac304b1 + e7da56d commit 1becf22
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/clayui.com/electric.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ require('gulp-storage')(gulp);

gulp.storage.create('claycss', 'claycss.json');

const generateIconData = require('./lib/icons');
const generateIconData = require('./utils/icons');

let clayJSPath = path.join(clay.srcDir, 'js');

Expand Down
88 changes: 88 additions & 0 deletions packages/clayui.com/utils/icons.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
const _ = require('lodash');
const clay = require('clay');
const countries = require('countries-list').countries
const fs = require('fs');
const langs = require('languages');
const path = require('path');

const alternateCodes = {
'in': 'id',
'iw': 'he',
'nb': 'no'
}

function generateIconData(data) {
const iconsDoc = _.get(data, 'index.children.docs.children.components.children.icons-lexicon');

if (iconsDoc) {
const iconsPath = path.join(clay.buildDir, 'images', 'icons');

let files = fs.readdirSync(iconsPath);

files = _.reject(
files,
function(file) {
return file === 'icons.svg';
}
);

files = _.partition(
files,
function(file) {
return !_.startsWith(file, 'flags-');
}
);

let icons = files[0];
let flags = files[1];

icons = icons.map(mapSVG);
flags = flags.map(mapSVG);

const flagData = flags.reduce(
function(prev, item, index) {
prev[item] = getLangInfo(item);

return prev;
},
{}
);

iconsDoc.icons = icons;
iconsDoc.flags = flags;
iconsDoc.flagData = flagData;
}

return data;
}

function getLangInfo(code) {
const parts = code.split('-');

const langCode = parts[0];
const countryCode = parts[1];

let lang = langs.getLanguageInfo(alternateCodes[langCode] || langCode).name;

if (countryCode) {
const country = countries[countryCode.toUpperCase()];

if (country) {
lang += ' (' + country.name + ')';
}
}

return lang;
}

function mapSVG(item, index) {
let iconName = path.basename(item, '.svg').toLowerCase();

if (_.startsWith(iconName, 'flags-')) {
iconName = iconName.replace(/^flags-/, '');
}

return iconName;
}

module.exports = generateIconData;

0 comments on commit 1becf22

Please sign in to comment.