-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.rs
More file actions
102 lines (90 loc) · 3.5 KB
/
build.rs
File metadata and controls
102 lines (90 loc) · 3.5 KB
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
use std::env;
use std::fs;
use std::path::{Path, PathBuf};
fn minify_html(html: &str) -> String {
let mut result = String::new();
let mut in_script = false;
let mut in_style = false;
let mut chars = html.chars().peekable();
while let Some(c) = chars.next() {
match c {
'<' if chars.peek().map_or(false, |&n| n == 's') => {
let next_5: String = chars.clone().take(5).collect();
if next_5 == "script" {
in_script = true;
result.push_str("<script");
chars.by_ref().take(5).for_each(drop);
continue;
}
}
'<' if chars.peek().map_or(false, |&n| n == 's') => {
let next_5: String = chars.clone().take(5).collect();
if next_5 == "style" {
in_style = true;
result.push_str("<style");
chars.by_ref().take(5).for_each(drop);
continue;
}
}
'<' if chars.peek().map_or(false, |&n| n == '/') => {
let next_7: String = chars.clone().take(7).collect();
if next_7 == "/script" && in_script {
in_script = false;
result.push_str("</script>");
chars.by_ref().take(7).for_each(drop);
continue;
} else if next_7 == "/style" && in_style {
in_style = false;
result.push_str("</style>");
chars.by_ref().take(7).for_each(drop);
continue;
}
}
_ => {}
}
if in_script || in_style {
result.push(c);
} else if !c.is_whitespace() {
result.push(c);
} else if !result.ends_with(' ')
&& !result.ends_with('>')
&& chars
.peek()
.map_or(false, |&n| !n.is_whitespace() && n != '<')
{
result.push(' ');
}
}
result
}
fn embed_html_as_rust_code(source_path: &Path, name: &str, out_dir: &Path) {
let html = fs::read_to_string(source_path).expect("Failed to read template");
let minified = minify_html(&html);
let escaped = minified.replace('\\', "\\\\").replace('"', "\\\"");
let rust_code = format!(
r#"pub const {}: &str = "{}";"#,
name.to_uppercase(),
escaped
);
let templates_path = out_dir.join("templates.rs");
let mut content = if templates_path.exists() {
fs::read_to_string(&templates_path).expect("Failed to read templates.rs")
} else {
String::new()
};
content.push('\n');
content.push_str(&rust_code);
fs::write(&templates_path, content).expect("Failed to write templates.rs");
}
fn main() {
let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap());
let templates_dir = PathBuf::from("templates");
fs::create_dir_all(&out_dir).expect("Failed to create output directory");
let templates_path = out_dir.join("templates.rs");
fs::write(&templates_path, "").expect("Failed to clear templates.rs");
embed_html_as_rust_code(&templates_dir.join("index.html"), "index_html", &out_dir);
embed_html_as_rust_code(&templates_dir.join("404.html"), "not_found_html", &out_dir);
println!("cargo:rerun-if-changed=templates/index.html");
println!("cargo:rerun-if-changed=templates/404.html");
println!("cargo:rerun-if-changed=public/logo/logo.png");
}