-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathbuild.rs
More file actions
52 lines (46 loc) · 1.97 KB
/
Copy pathbuild.rs
File metadata and controls
52 lines (46 loc) · 1.97 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
use std::collections::BTreeMap;
use std::env;
use std::fs::File;
use std::io::{BufWriter, Write};
use std::path::Path;
fn main() {
println!("cargo:rerun-if-changed=assets/entities.json");
println!("cargo:rerun-if-changed=build.rs");
let json_text = std::fs::read_to_string("assets/entities.json")
.expect("assets/entities.json not found");
let parsed: serde_json::Value =
serde_json::from_str(&json_text).expect("entities.json is not valid JSON");
let map = parsed.as_object().expect("entities.json root must be object");
// entity references must end with `;`. Filter to the
// semicolon-terminated entries only; strip the leading `&` and trailing
// `;` so the lookup key is the bare name.
let mut entries: BTreeMap<String, String> = BTreeMap::new();
for (key, value) in map {
if !key.ends_with(';') {
continue;
}
let name = key.trim_start_matches('&').trim_end_matches(';');
let chars = value
.get("characters")
.and_then(|v| v.as_str())
.expect("entity entry missing `characters`");
entries.insert(name.to_string(), chars.to_string());
}
let out_dir = env::var_os("OUT_DIR").unwrap();
let dest = Path::new(&out_dir).join("entities_table.rs");
let mut out = BufWriter::new(File::create(&dest).expect("cannot create entities_table.rs"));
writeln!(
out,
"/// Auto-generated by build.rs from assets/entities.json.\n\
/// Lookup map from named HTML5 entity (no `&` or `;`) to its decoded\n\
/// string. Includes only the CommonMark-valid (semicolon-terminated)\n\
/// entries from the WHATWG list.\n\
pub static NAMED_ENTITIES: phf::Map<&'static str, &'static str> = "
)
.unwrap();
let mut builder = phf_codegen::Map::new();
for (name, chars) in &entries {
builder.entry(name.as_str(), format!("{chars:?}"));
}
writeln!(out, "{};", builder.build()).unwrap();
}