0.4.10: remove fill none from svg

This commit is contained in:
Sam Denty 2022-12-26 17:46:30 +00:00
parent 1980cb955a
commit 640e986ef5
No known key found for this signature in database
GPG key ID: 7B4EAF7B9E291B79
3 changed files with 16 additions and 3 deletions

2
Cargo.lock generated
View file

@ -1638,7 +1638,7 @@ checksum = "7bd3e3206899af3f8b12af284fafc038cc1dc2b41d1b89dd17297221c5d225de"
[[package]] [[package]]
name = "site_icons" name = "site_icons"
version = "0.4.9" version = "0.4.10"
dependencies = [ dependencies = [
"byteorder", "byteorder",
"clap", "clap",

View file

@ -1,6 +1,6 @@
[package] [package]
name = "site_icons" name = "site_icons"
version = "0.4.9" version = "0.4.10"
authors = ["Sam Denty <sam@samdenty.com>"] authors = ["Sam Denty <sam@samdenty.com>"]
edition = "2018" edition = "2018"
license = "GPL-3.0" license = "GPL-3.0"

View file

@ -1,4 +1,5 @@
use percent_encoding::{utf8_percent_encode, AsciiSet, CONTROLS}; use percent_encoding::{utf8_percent_encode, AsciiSet, CONTROLS};
use std::borrow::Cow;
const DATA_URI: &AsciiSet = &CONTROLS const DATA_URI: &AsciiSet = &CONTROLS
.add(b'\r') .add(b'\r')
@ -28,7 +29,19 @@ pub fn encode_svg(svg: &str) -> String {
}; };
// use single quotes instead of double to avoid encoding. // use single quotes instead of double to avoid encoding.
let encoded = regex!("\"").replace_all(&encoded, "'"); let mut encoded = regex!("\"").replace_all(&encoded, "'");
// remove a fill=none attribute
if let Some(captures) = regex!("^[^>]+fill='?(none)'?").captures(&encoded) {
let index = captures.get(1).unwrap();
let mut result = String::new();
for (i, c) in encoded.chars().enumerate() {
if i < index.start() || i >= index.end() {
result.push(c);
}
}
encoded = Cow::from(result);
}
// remove whitespace // remove whitespace
let encoded = regex!(r">\s{1,}<").replace_all(&encoded, "><"); let encoded = regex!(r">\s{1,}<").replace_all(&encoded, "><");