Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
co(function *() {
// get font data from `fonts/fontawesome-webfont.svg`
const fontPath = path.resolve(source, './fonts/fontawesome-webfont.svg');
const svgContent = yield readFile(fontPath, 'utf8');
const parsedSvg = yield parseXml(svgContent);
const glyphs = parsedSvg.svg.defs[0].font[0].glyph;
const fontData = {};
lang.each(glyphs, glyph => {
fontData[glyph.$.unicode.charCodeAt(0)] = glyph.$;
});
//console.log(fontData);
// get font list from `./less/variables.less`
const lessPath = path.resolve(source, './less/variables.less');
const lessContent = yield readFile(lessPath, 'utf8');
const iconList = [];
const matchedHexes = [];
lang.each(lessContent.split(/\n/), line => {
if (!line) return;
const match = /@fa-var-([^:]+):\s*"\\([0-9a-f]+)";/.exec(line);
if (match) {
const unicodeHex = match[2];
const unicodeDec = parseInt(unicodeHex, 16);
co(function *() {
// get font data
const themeLessPath = path.resolve(source, './components/style/themes/default.less');
const themeLessContent = yield readFile(themeLessPath, 'utf8');
const iconUrl = themeLessContent.match(/@icon\-url\s*:\s*"(\S+)"/)[1];
const svgResponse = yield request(`${iconUrl}.svg`);
const svgContent = svgResponse.body;
const parsedSvg = yield parseXml(svgContent);
const glyphs = parsedSvg.svg.defs[0].font[0].glyph;
const fontData = {};
lang.each(glyphs, glyph => {
if (glyph.$.unicode) {
fontData[glyph.$.unicode.charCodeAt(0)] = glyph.$;
}
});
//console.log(fontData);
// get font list from `./components/style/core/iconfont.less`
const lessPath = path.resolve(source, './components/style/core/iconfont.less');
const lessContent = yield readFile(lessPath, 'utf8');
const iconList = [];
lang.each(lessContent.split(/\n/), line => {
if (!line) return;
const match = /.@\{iconfont-css-prefix\}-([^:]+):before\s*\{content:"\\([0-9a-f]+)";\}/.exec(line);
if (match) {
const unicodeHex = match[2];
const fontPath = path.resolve(source, './fonts/fontawesome-webfont.svg');
const svgContent = yield readFile(fontPath, 'utf8');
const parsedSvg = yield parseXml(svgContent);
const glyphs = parsedSvg.svg.defs[0].font[0].glyph;
const fontData = {};
lang.each(glyphs, glyph => {
fontData[glyph.$.unicode.charCodeAt(0)] = glyph.$;
});
//console.log(fontData);
// get font list from `./less/variables.less`
const lessPath = path.resolve(source, './less/variables.less');
const lessContent = yield readFile(lessPath, 'utf8');
const iconList = [];
const matchedHexes = [];
lang.each(lessContent.split(/\n/), line => {
if (!line) return;
const match = /@fa-var-([^:]+):\s*"\\([0-9a-f]+)";/.exec(line);
if (match) {
const unicodeHex = match[2];
const unicodeDec = parseInt(unicodeHex, 16);
if (!lang.contains(matchedHexes, unicodeHex)) {
matchedHexes.push(unicodeHex);
iconList.push(lang.extend({
id: match[1],
unicodeDec,
unicodeHex,
}, fontData[unicodeDec])
);
}
} else {
function parse(str, delimiter) {
delimiter = delimiter || ',';
const result = [];
const csvArray = str.split('\n');
const firstRow = csvArray.shift();
const columnKeys = firstRow.split(delimiter);
lang.each(csvArray, (row) => {
const csvRowArray = row.split(delimiter);
const jsonRow = {};
if (csvRowArray.length === columnKeys.length) {
lang.each(csvRowArray, (part, index) => {
// fixing numeric values
jsonRow[columnKeys[index]] = isNaN(part) ? part : parseFloat(part, 10);
});
result.push(jsonRow);
}
});
return result;
}
lang.each(csvArray, (row) => {
const csvRowArray = row.split(delimiter);
const jsonRow = {};
if (csvRowArray.length === columnKeys.length) {
lang.each(csvRowArray, (part, index) => {
// fixing numeric values
jsonRow[columnKeys[index]] = isNaN(part) ? part : parseFloat(part, 10);
});
result.push(jsonRow);
}
});
return result;
request(URL_ICON_LESS, (iconLessErr, iconLessRes, iconLessBody) => {
if (!iconLessErr && iconLessRes.statusCode === 200) {
// icon list
const iconNameByDec = {};
lang.each(iconLessBody.split(/\n/), (line) => {
line = lang.trim(line);
if (!line) {
return;
}
// icon font pattern
// @fa-var-500px: "\f26e";
const match = /@fa-var-([^\s:]+)\s*:\s*"\\([0-9a-f]+)"\s*;/.exec(line);
if (match) {
const name = match[1];
const unicodeHex = match[2];
const unicodeDec = parseInt(unicodeHex, 16);
iconNameByDec[unicodeDec] = name;
}
});
// svg fonts file
request(URL_ICON_LESS, (iconLessErr, iconLessRes, iconLessBody) => {
if (!iconLessErr && iconLessRes.statusCode === 200) {
// icon list
const iconNameByDec = {};
lang.each(iconLessBody.split(/\n/), (line) => {
line = lang.trim(line);
if (!line) {
return;
}
// icon font pattern
// .glyphicon-asterisk { &:before { content: "\002a"; } }
const match = /\.glyphicon-([^{\s]+)\s*\{\s*&:before\s*\{\s*content:\s*"\\([0-9a-f]+)"\s*;/.exec(line);
if (match) {
const name = match[1];
const unicodeHex = match[2];
const unicodeDec = parseInt(unicodeHex, 16);
iconNameByDec[unicodeDec] = name;
}
});
// svg fonts file
#!/usr/bin/env node
'use strict';
const fs = require('fs');
const lang = require('zero-lang');
const mapStream = require('map-stream');
const path = require('path');
const vfs = require('vinyl-fs');
const iconsMeta = require('../lib/const/icons-meta');
const icons = {};
lang.each(iconsMeta, meta => {
icons[meta.prefix.replace(/\-$/, '')] = meta;
});
icons.zero = {
name: 'zero',
prefix: 'zero-'
};
function reduceIcons(file, cb) { // removing prefix in filename
const arr = file.path.split(path.sep);
const len = arr.length;
const type = arr[len - 2];
const name = arr[len - 1].replace(/\.svg$/, '');
icons[type] = icons[type] || {};
icons[type].icons = icons[type].icons || [];
icons[type].icons.push(name);
module.exports = (icons) => {
const resultIcons = [];
lang.each(icons, (icon) => {
if (icon.name && icon.body) { // removing invalid ones
delete icon.unicode;
icon.name = fixName(icon.name); // fixing invalid names
resultIcons.push(icon);
}
});
return resultIcons;
};
extractSvgFonts(svgFontsBody, {}, (icons) => {
lang.each(icons, (icon) => {
if (icon.unicode) {
const dec = icon.unicode.charCodeAt(0);
icon.name = iconNameByDec[dec] || '';
}
});
fs.writeFile(
options.output,
JSON.stringify(filterIcons(icons), null, '\t'),
'utf8',
(err) => {
if (err) {
console.error(err);
}
}
);
});