From 66fc1caaace6152ac58d24f62fc38b2e6a6e7340 Mon Sep 17 00:00:00 2001 From: Atul Varma Date: Wed, 3 Feb 2021 19:58:25 -0500 Subject: [PATCH] don't replace fill/stroke of 'none'. --- lib/svg-vocabulary.json | 28 ++++++++++++++-------------- lib/vocabulary.ts | 22 +++++++++++++--------- 2 files changed, 27 insertions(+), 23 deletions(-) diff --git a/lib/svg-vocabulary.json b/lib/svg-vocabulary.json index 06dbf1c..7d32fcb 100644 --- a/lib/svg-vocabulary.json +++ b/lib/svg-vocabulary.json @@ -7,19 +7,19 @@ }, { "name": "bicycle", - "svg": "\n\n", + "svg": "\n\n", "width": 1280, "height": 720 }, { "name": "bone", - "svg": "\n\n\n", + "svg": "\n\n\n", "width": 1280, "height": 720 }, { "name": "church", - "svg": "\n\n\n\n", + "svg": "\n\n\n\n", "width": 720, "height": 720 }, @@ -37,25 +37,25 @@ }, { "name": "crown_3point", - "svg": "\n\n\n\n", + "svg": "\n\n\n\n", "width": 1280, "height": 720 }, { "name": "crown", - "svg": "\n\n\n", + "svg": "\n\n\n", "width": 1280, "height": 720 }, { "name": "cup", - "svg": "\n\n\n", + "svg": "\n\n\n", "width": 720, "height": 720 }, { "name": "dagger", - "svg": "\n\n\n\n\n\n\n\n", + "svg": "\n\n\n\n\n\n\n\n", "width": 1280, "height": 720 }, @@ -79,7 +79,7 @@ }, { "name": "hand_fist", - "svg": "\n\n\n", + "svg": "\n\n\n", "width": 720, "height": 720 }, @@ -97,7 +97,7 @@ }, { "name": "hand_point", - "svg": "\n\n\n", + "svg": "\n\n\n", "width": 1280, "height": 720 }, @@ -133,7 +133,7 @@ }, { "name": "Pyramid", - "svg": "\n\n\n\n\n", + "svg": "\n\n\n\n\n", "width": 720, "height": 720 }, @@ -145,19 +145,19 @@ }, { "name": "Spiral", - "svg": "\n\n", + "svg": "\n\n", "width": 720, "height": 720 }, { "name": "Spiral_Double", - "svg": "\n\n\n", + "svg": "\n\n\n", "width": 1280, "height": 720 }, { "name": "Spiral_Triple", - "svg": "\n\n\n\n", + "svg": "\n\n\n\n", "width": 720, "height": 720 }, @@ -169,7 +169,7 @@ }, { "name": "sword_2", - "svg": "\n\n\n\n\n\n\n\n", + "svg": "\n\n\n\n\n\n\n\n", "width": 1280, "height": 720 }, diff --git a/lib/vocabulary.ts b/lib/vocabulary.ts index 9b1195a..42db264 100644 --- a/lib/vocabulary.ts +++ b/lib/vocabulary.ts @@ -2,23 +2,27 @@ import fs from "fs"; import path from "path"; import cheerio from "cheerio"; -export type Symbol = { +export type SvgSymbolData = { name: string; width: number; height: number; svg: string; }; -export type SerializedVocabulary = Symbol[]; - const MY_DIR = __dirname; const SVG_DIR = path.join(MY_DIR, "..", "svg"); const VOCAB_PATH = path.join(MY_DIR, "svg-vocabulary.json"); -function removeAttr(attr: string, $: cheerio.Root, g: cheerio.Cheerio) { +function removeAttrIfNotNone( + attr: string, + $: cheerio.Root, + g: cheerio.Cheerio +) { const items = g.find(`[${attr}]`); items.each(function (this: any, i, el) { - $(this).removeAttr(attr); + if ($(this).attr(attr) !== "none") { + $(this).removeAttr(attr); + } }); } @@ -38,7 +42,7 @@ function getSvgPixelDimension($: cheerio.Root, attr: string): number { export function build() { const filenames = fs.readdirSync(SVG_DIR); - const vocab: SerializedVocabulary = []; + const vocab: SvgSymbolData[] = []; for (let filename of filenames) { if (path.extname(filename) === ".svg") { console.log(`Adding ${filename} to vocabulary.`); @@ -49,8 +53,8 @@ export function build() { const width = getSvgPixelDimension($, "width"); const height = getSvgPixelDimension($, "height"); const g = $("svg > g"); - removeAttr("fill", $, g); - removeAttr("stroke", $, g); + removeAttrIfNotNone("fill", $, g); + removeAttrIfNotNone("stroke", $, g); const name = g.attr("id"); if (!name) { throw new Error(`${filename} has no with an 'id' attribute!`); @@ -59,7 +63,7 @@ export function build() { if (!svg) { throw new Error(`${filename} has no with child elements!`); } - const symbol: Symbol = { + const symbol: SvgSymbolData = { name, svg, width,