diff options
author | Martin Fischer <martin@push-f.com> | 2021-11-12 03:47:19 +0100 |
---|---|---|
committer | Martin Fischer <martin@push-f.com> | 2021-11-12 03:52:47 +0100 |
commit | c914b71a28ce7177171b83df2815352bf5741844 (patch) | |
tree | e11a30a30730a9fc3dd03ff49e3a5db029ce253d /build.rs | |
parent | ea7c3c9b8b45d846bcb45573c9b2250d8152be30 (diff) |
fix named entities
In 462bb0ef0ba9e027f5138c87438328db718d15da I dropped the
markup5ever::data dependency but omitted to include its
named_entities data. This commit remedies that mistake.
Diffstat (limited to 'build.rs')
-rw-r--r-- | build.rs | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/build.rs b/build.rs new file mode 100644 index 0000000..8d4404c --- /dev/null +++ b/build.rs @@ -0,0 +1,61 @@ +// Copyright 2014-2017 The html5ever Project Developers. See the +// COPYRIGHT file at the top-level directory of this distribution. +// +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +extern crate phf_codegen; + +use std::collections::HashMap; +use std::env; +use std::fs::File; +use std::io::Write; +use std::path::Path; + +mod entities; + +fn main() { + named_entities_to_phf(&Path::new(&env::var("OUT_DIR").unwrap()).join("named_entities.rs")); +} + +fn named_entities_to_phf(to: &Path) { + let mut entities: HashMap<&str, (u32, u32)> = entities::NAMED_ENTITIES + .iter() + .map(|(name, cp1, cp2)| { + assert!(name.starts_with('&')); + (&name[1..], (*cp1, *cp2)) + }) + .collect(); + + // Add every missing prefix of those keys, mapping to NULL characters. + for key in entities.keys().cloned().collect::<Vec<_>>() { + for n in 1..key.len() { + entities.entry(&key[..n]).or_insert((0, 0)); + } + } + entities.insert("", (0, 0)); + + let mut phf_map = phf_codegen::Map::new(); + for (key, value) in entities { + phf_map.entry(key, &format!("{:?}", value)); + } + + let mut file = File::create(to).unwrap(); + writeln!( + &mut file, + r#" +/// A map of entity names to their codepoints. The second codepoint will +/// be 0 if the entity contains a single codepoint. Entities have their preceeding '&' removed. +"# + ) + .unwrap(); + writeln!( + &mut file, + "pub static NAMED_ENTITIES: Map<&'static str, (u32, u32)> = {};", + phf_map.build(), + ) + .unwrap(); +} |