-
Notifications
You must be signed in to change notification settings - Fork 16
/
create_icon_file.rs
172 lines (160 loc) · 5.55 KB
/
create_icon_file.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
use std::fs;
use std::fs::File;
use std::io::Write;
use std::path::PathBuf;
use std::{ffi::OsStr, path::Path};
use heck::ToSnakeCase;
use heck::ToUpperCamelCase;
use regex::Regex;
use scraper::node::Element;
use scraper::ElementRef;
use scraper::Html;
use walkdir::WalkDir;
const ICON_TEMPLATE: &str = r#"#[derive(Copy, Clone, Debug, PartialEq)]
pub struct {ICON_NAME};
impl IconShape for {ICON_NAME} {
fn view_box(&self) -> String {
String::from("{VIEW_BOX}")
}
fn xmlns(&self) -> String {
String::from("{XMLNS}")
}
fn child_elements(&self) -> Element {
rsx! {
{CHILD_ELEMENTS}
}
}
}
"#;
pub fn create_icon_file(svg_path: &str, output_path: &str, icon_prefix: &str) {
let files = collect_svg_files(svg_path, icon_prefix);
let icon_file = files
.into_iter()
.map(|file| {
let svg_str = fs::read_to_string(&file).unwrap();
let fragment = Html::parse_fragment(&svg_str);
let elements = fragment
.tree
.nodes()
.filter_map(|node| {
if node.value().is_element() {
let element = ElementRef::wrap(node).unwrap().value();
if !element.attrs.is_empty() {
return Some(element);
}
}
None
})
.collect::<Vec<_>>();
let svg_element = &elements[0];
let svg_child_elements = &elements[1..];
let icon_name = icon_name(&file, icon_prefix);
let (view_box, xmlns) = extract_svg_attrs(svg_element);
let child_elements = extract_svg_child_elements(svg_child_elements, icon_prefix);
ICON_TEMPLATE
.replace("{ICON_NAME}", &format!("{}{}", icon_prefix, &icon_name))
.replace("{VIEW_BOX}", &view_box)
.replace("{XMLNS}", &xmlns)
.replace("{CHILD_ELEMENTS}", &child_elements)
})
.collect::<Vec<_>>()
.join("\n");
// write to file
let mut file = File::create(output_path).unwrap();
file.write_all(
format!(
"{}\n\n{}",
"use super::super::IconShape;\nuse dioxus::prelude::*;", icon_file
)
.as_bytes(),
)
.unwrap();
file.flush().unwrap();
}
fn collect_svg_files(svg_path: &str, icon_prefix: &str) -> Vec<PathBuf> {
let dir_entries = WalkDir::new(svg_path)
.into_iter()
.filter_map(|e| e.ok())
.collect::<Vec<_>>();
dir_entries
.into_iter()
.filter(|e| match icon_prefix {
"Go" => {
let re = Regex::new(r".*-16.svg$").unwrap();
return re.is_match(e.path().to_str().unwrap());
}
"Md" => {
let split_vec = e.path().components().collect::<Vec<_>>();
return split_vec.iter().any(|c| c.as_os_str() == "materialicons")
&& e.file_name().to_str().unwrap() == "24px.svg";
}
_ => return e.path().extension() == Some(OsStr::new("svg")),
})
.map(|dir| PathBuf::from(dir.path()))
.collect::<Vec<_>>()
}
fn icon_name(path: &Path, icon_prefix: &str) -> String {
match icon_prefix {
"Go" => {
let filename = path.file_name().unwrap().to_str().unwrap();
let name = filename.split('.').next().unwrap();
name.replace("-16", "").to_upper_camel_case()
}
"Md" => {
let split_vec = path.components().collect::<Vec<_>>();
let name = split_vec[split_vec.len() - 3];
name.as_os_str().to_str().unwrap().to_upper_camel_case()
}
_ => {
let filename = path.file_name().unwrap().to_str().unwrap();
let name = filename.split('.').next().unwrap();
name.to_upper_camel_case()
}
}
}
fn extract_svg_attrs(element: &Element) -> (String, String) {
let view_box = element.attr("viewBox").unwrap();
let xmlns = element
.attr("xmlns")
.unwrap_or("http://www.w3.org/2000/svg");
(String::from(view_box), String::from(xmlns))
}
// "fill:none;stroke:#000;
fn extract_svg_child_elements(elements: &[&Element], icon_prefix: &str) -> String {
let elements = match icon_prefix {
"Md" => &elements[1..],
_ => elements,
};
elements
.iter()
.map(|element| {
let tag_name = element.name();
let mut element_attrs = element
.attrs()
.filter_map(|(name, value)| {
let value = if icon_prefix == "Io" {
value.replace("fill:none;stroke:#000;", "")
} else {
value.to_string()
};
let re = Regex::new(r"^data-.*$").unwrap();
if !re.is_match(name) && name != "fill" {
Some(format!(
" {}: \"{}\",",
name.to_snake_case(),
value
))
} else {
None
}
})
.collect::<Vec<_>>();
element_attrs.sort();
let attrs_str = element_attrs.join("\n");
" {TAG_NAME} {\n{ATTRS}\n }"
.replace("{TAG_NAME}", tag_name)
.replace("{ATTRS}", &attrs_str)
})
.collect::<Vec<_>>()
.join("\n")
}