htmlite 0.11.0

An HTML manipulation toolkit
Documentation
use serde_json::Value;
use std::env;
use std::fs;
use std::fs::File;
use std::io::Write;
use std::path::Path;
use std::str::FromStr;

fn main() {
    generate_named_entity_map();
}

// This will generate a static lookup table based on the json in data/entities.json
//
// The table maps character references to the rust string literal that represents the replacement codepoints.
fn generate_named_entity_map() {
    let json_string = String::from_utf8(fs::read("data/entities.json").unwrap()).unwrap();
    let json = Value::from_str(&json_string).unwrap();
    let entities = json.as_object().unwrap();
    let mut builder = phf_codegen::Map::new();
    for (character_reference, obj) in entities {
        let replacement = obj["characters"].as_str().unwrap();
        let replacement_code_points: String = replacement
            .chars()
            .map(|c| format!("\\u{{{:x}}}", u32::from(c)))
            .collect();
        builder.entry(
            character_reference.as_str(),
            format!("\"{}\"", replacement_code_points),
        );
    }

    let path = Path::new(&env::var("OUT_DIR").unwrap()).join("named_character_references.rs");
    let file = File::create(path).unwrap();
    write!(
        &file,
        "static NAMED_CHARACTER_REFERENCES: phf::Map<&'static str, &'static str> = {};",
        builder.build(),
    )
    .unwrap();
}