Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add generate icon data | Fixes #696 #698

Merged
merged 1 commit into from
Mar 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;