-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuild.rs
65 lines (59 loc) · 2.3 KB
/
build.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
use std::fs::{self, DirEntry, File};
use std::io::Write;
use std::collections::BTreeMap;
use std::path::Path;
fn main() -> Result<(), std::io::Error> {
convert_icon_svg_to_css()
}
fn convert_icon_svg_to_css() -> Result<(), std::io::Error> {
let icons_path = Path::new("assets/ui_icons");
let css_path = Path::new("static/css/");
fs::create_dir_all(css_path)?;
let mut f = File::create(css_path.join("zorgit-icons.css"))?;
let dirs = walk_dirs(icons_path)?;
for (dir, entries) in dirs {
f.write_all(format!("/* Icons from {} */", dir).as_bytes())?;
f.write_all(b"\n")?;
for icon in entries {
if icon.file_type()?.is_file() && icon.path().extension().unwrap() == "svg" {
let sanitized = sanitize(fs::read_to_string(icon.path())?);
let escaped = escape(sanitized);
let name = icon.file_name().into_string().unwrap().replace(".svg", "");
let icon_content = format!(".zg-{} {{ mask-image: url(\"data:image/svg+xml,{svg}\"); -webkit-mask-image: url(\"data:image/svg+xml,{svg}\"); }}", name, svg=escaped);
f.write_all(&icon_content.into_bytes())?;
f.write_all(b"\n")?;
}
}
}
Ok(())
}
fn walk_dirs(dir: &Path) -> Result<BTreeMap<String, Vec<DirEntry>>, std::io::Error> {
let mut entries: BTreeMap<String, Vec<DirEntry>> = BTreeMap::new();
if dir.is_dir() {
let key = dir.file_name().unwrap().to_str().unwrap().to_string();
for entry in fs::read_dir(dir)? {
let entry = entry?;
let path = entry.path();
if path.is_dir() {
let e = walk_dirs(&path)?;
entries.extend(e);
} else {
if let Some(content) = entries.get_mut(&key) {
content.push(entry);
}
else {
let mut content = Vec::new();
content.push(entry);
entries.insert(key.clone(), content);
}
}
}
}
Ok(entries)
}
fn sanitize(text: String) -> String {
text.replace("\n", "").replace("\r", "").replace("\t", "").replace(" ", "").trim().to_string()
}
fn escape(text: String) -> String {
text.replace("\"", "'").replace("#", "%23")
}