| 1 |
use entities::ENTITIES; |
| 2 |
use std::io::Write; |
| 3 |
use std::{env, path::PathBuf}; |
| 4 |
|
| 5 |
fn main() { |
| 6 |
let out_dir: PathBuf = env::var("OUT_DIR").unwrap().parse().unwrap(); |
| 7 |
|
| 8 |
// entity::lookup is handed just the inner entity name, like "amp" for |
| 9 |
// "&"; we only match those with a trailing ";". |
| 10 |
// |
| 11 |
// entities::ENTITIES includes many both with and without a trailing ";". |
| 12 |
// Exclude those without, and then write to source only the name, without |
| 13 |
// the leading or trailing "&" or ";". |
| 14 |
let translated_entities = ENTITIES |
| 15 |
.iter() |
| 16 |
.filter(|e| e.entity.starts_with('&') && e.entity.ends_with(';')) |
| 17 |
.map(|e| (&e.entity[1..e.entity.len() - 1], e.characters)) |
| 18 |
.collect::<Vec<_>>(); |
| 19 |
|
| 20 |
// Generate a perfect hash map for O(1) lookup |
| 21 |
let out = std::fs::File::create(out_dir.join("entitydata.rs")).unwrap(); |
| 22 |
let mut bw = std::io::BufWriter::new(out); |
| 23 |
|
| 24 |
writeln!(bw, "mod entitydata {{").unwrap(); |
| 25 |
writeln!(bw).unwrap(); |
| 26 |
|
| 27 |
write!( |
| 28 |
bw, |
| 29 |
" pub static ENTITY_MAP: phf::Map<&'static str, &'static str> = " |
| 30 |
) |
| 31 |
.unwrap(); |
| 32 |
|
| 33 |
let mut map = phf_codegen::Map::new(); |
| 34 |
for (entity, characters) in &translated_entities { |
| •35 |
map.entry(*entity, &format!("{:?}", characters)); |
| 36 |
} |
| 37 |
writeln!(bw, "{};", map.build()).unwrap(); |
| 38 |
|
| 39 |
writeln!(bw, "}}").unwrap(); |
| 40 |
} |