Skip to content

Commit

Permalink
Add check for unicode normalization in config
Browse files Browse the repository at this point in the history
This ensures that the config sections match Wikipedia's Unicode
normalization. We could also normalize every section in every article to
handle an edge case, but I don't think that's worth it yet.

Signed-off-by: Evan Lloyd New-Schmidt <[email protected]>
  • Loading branch information
newsch committed Apr 21, 2024
1 parent 19d9f2c commit 58864b4
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 8 deletions.
5 changes: 3 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ anyhow = { version = "1.0.71", features = ["backtrace"] }
clap = { version = "4.3.2", features = ["derive"] }
csv = "1.2.2"
ego-tree = "0.6.2"
expect-test = "1.4.1"
html5ever = "0.26.0"
log = "0.4.18"
markup5ever = "0.11.0"
Expand All @@ -29,6 +28,10 @@ tracing-subscriber = { version = "0.3.17", features = ["env-filter"] }
url = "2.3.1"
urlencoding = "2.1.2"

[dev-dependencies]
expect-test = "1.4.1"
unicode-normalization = "0.1.23"

[profile.release]
overflow-checks = true
lto = true
Expand Down
51 changes: 46 additions & 5 deletions src/html.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,23 @@ struct Config<'a> {
sections_to_remove: BTreeMap<&'a str, BTreeSet<&'a str>>,
}

/// Path to the processing config file.
///
/// Other compile-time macros expect a string literal, so this must be a macro instead of a const str.
macro_rules! config_path {
() => {
concat!(
env!("CARGO_MANIFEST_DIR"),
"/article_processing_config.json"
)
};
}

static CONFIG: Lazy<Config<'static>> = Lazy::new(|| {
serde_json::from_str(include_str!(concat!(
env!("CARGO_MANIFEST_DIR"),
"/article_processing_config.json"
)))
.expect("\"article_processing_config.json\" is either invalid json or the wrong structure")
serde_json::from_str(include_str!(config_path!())).expect(concat!(
config_path!(),
" is either invalid json or the wrong structure"
))
});

static HEADERS: Lazy<Selector> =
Expand Down Expand Up @@ -488,6 +499,36 @@ mod test {
assert!(!CONFIG.sections_to_remove.is_empty());
}

/// Ensure config sections match Wikipedia's Unicode normalization (NFC) so
/// that they can be correctly compared bytewise.
///
/// As the discussion below mentions, there is an edge-case where section
/// names in the article contain templates that expand to non-normalized
/// text, which this does not handle.
///
/// See also:
/// - Mediawiki discussion of normalization: https://mediawiki.org/wiki/Unicode_normalization_considerations
/// - Online conversion tool: https://util.unicode.org/UnicodeJsps/transform.jsp?a=Any-NFC
#[test]
fn static_config_sections_are_normalized() {
use unicode_normalization::{is_nfc, UnicodeNormalization};

let mut all_sections_are_normalized = true;
for section in CONFIG.sections_to_remove.values().flatten() {
if !is_nfc(section) {
all_sections_are_normalized = false;
let normalized = String::from_iter(section.nfc());
eprintln!("Section to remove {section:?} should be normalized to {normalized:?}");
}
}

assert!(
all_sections_are_normalized,
"Not all sections in {} are in Unicode NFC. Please replace the reported sections.",
config_path!()
);
}

fn expand_links(document: &mut Html) {
let links: Vec<_> = document
.select(&Selector::parse("a").unwrap())
Expand Down

0 comments on commit 58864b4

Please sign in to comment.