markdown2pdf 0.4.0

Create PDF with Markdown files (a md to pdf transpiler)
Documentation
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();
}