Add width/height to vocabulary.

pull/1/head
Atul Varma 2021-02-03 18:55:39 -05:00
rodzic 5fc1ac97b4
commit 8037ae9e3f
1 zmienionych plików z 22 dodań i 1 usunięć

Wyświetl plik

@ -4,6 +4,8 @@ import cheerio from "cheerio";
export type Symbol = { export type Symbol = {
name: string; name: string;
width: number;
height: number;
svg: string; svg: string;
}; };
@ -20,19 +22,36 @@ function removeAttr(attr: string, $: cheerio.Root, g: cheerio.Cheerio) {
}); });
} }
function getSvgPixelDimension($: cheerio.Root, attr: string): number {
const value = $("svg").attr(attr);
if (!value) {
throw new Error(`<svg> ${attr} attribute is empty or missing!`);
}
const match = value.match(/^(\d+)px$/);
if (!match) {
throw new Error(
`unable to parse <svg> ${attr} attribute value '${value}'!`
);
}
return parseInt(match[1]);
}
export function build() { export function build() {
const filenames = fs.readdirSync(SVG_DIR); const filenames = fs.readdirSync(SVG_DIR);
const vocab: SerializedVocabulary = []; const vocab: SerializedVocabulary = [];
for (let filename of filenames) { for (let filename of filenames) {
if (path.extname(filename) === ".svg") { if (path.extname(filename) === ".svg") {
console.log(`Adding ${filename} to vocabulary.`);
const svgMarkup = fs.readFileSync(path.join(SVG_DIR, filename), { const svgMarkup = fs.readFileSync(path.join(SVG_DIR, filename), {
encoding: "utf-8", encoding: "utf-8",
}); });
const $ = cheerio.load(svgMarkup); const $ = cheerio.load(svgMarkup);
const width = getSvgPixelDimension($, "width");
const height = getSvgPixelDimension($, "height");
const g = $("svg > g"); const g = $("svg > g");
removeAttr("fill", $, g); removeAttr("fill", $, g);
removeAttr("stroke", $, g); removeAttr("stroke", $, g);
const name = g.attr('id'); const name = g.attr("id");
if (!name) { if (!name) {
throw new Error(`${filename} has no <g> with an 'id' attribute!`); throw new Error(`${filename} has no <g> with an 'id' attribute!`);
} }
@ -43,6 +62,8 @@ export function build() {
const symbol: Symbol = { const symbol: Symbol = {
name, name,
svg, svg,
width,
height,
}; };
vocab.push(symbol); vocab.push(symbol);
} }