diff --git a/crates/bibclean/README.md b/crates/bibclean/README.md index 4c13d41..3ac92b1 100644 --- a/crates/bibclean/README.md +++ b/crates/bibclean/README.md @@ -180,6 +180,7 @@ bibclean books.ndjson | jq -c 'select(.slug_proposal.tier | IN("placeholder-auth | `mojibake_title` | 3 | UTF-8 read as Windows-1252: "américain" | | `mojibake_author` | 3 | The same, in the author field | | `placeholder_suffix` | 2 | A placeholder author left on the end of the title: "… - Unknown" | +| `url_title` | 4 | A web address saved as the title | | `path_title` | 4 | A file path sitting in the title | | `numeric_author` | 4 | An author field opening with a bare year or series number | | `unknown_author` | 3 | "Unknown", "Anonymous", empty | diff --git a/crates/bibclean/src/lib.rs b/crates/bibclean/src/lib.rs index 083330d..353395b 100644 --- a/crates/bibclean/src/lib.rs +++ b/crates/bibclean/src/lib.rs @@ -218,8 +218,13 @@ pub fn clean(book: &Book) -> Cleaned { // Underscores first: a title mangled by a filename may also be shouting, // and title-casing it before the underscores come out would case the // format suffix as though it were a word. + // Not in an address or a path, where the underscores are the name and + // taking them out leaves something that is neither title nor address. if let Some(title) = cleaned.title.clone() && reasons.iter().any(|reason| reason == "underscore_title") + && !reasons + .iter() + .any(|reason| reason == "url_title" || reason == "path_title") { let fixed = fix::clean_underscores(&title); if fixed != title { @@ -481,6 +486,22 @@ mod tests { ); } + #[test] + fn an_address_is_named_and_left_alone() { + for title in [ + "http://www.voxfux.com/features/stranger_than_fiction.htm", + "file://D:\\Books\\white_paper", + "www.example.org/some_page", + ] { + let cleaned = clean(&book(title, "Someone")); + assert_eq!(cleaned.book.title.as_deref(), Some(title), "{title}"); + assert!( + cleaned.reasons.iter().any(|reason| reason == "url_title"), + "{title}" + ); + } + } + #[test] fn a_shouting_title_is_cased_and_the_change_is_recorded() { let cleaned = clean(&book("THE HUMAN CONDITION", "Arendt, Hannah")); diff --git a/crates/bibclean/src/reasons.rs b/crates/bibclean/src/reasons.rs index c972494..521fcc1 100644 --- a/crates/bibclean/src/reasons.rs +++ b/crates/bibclean/src/reasons.rs @@ -34,6 +34,10 @@ static JUNK_TITLE: LazyLock = LazyLock::new(|| { /// A path sitting in the title field. Distinct from a filename, which is a /// bare name ending in an extension. +/// A web address saved as the title: "http://www.voxfux.com/features/…". +static URL_TITLE: LazyLock = + LazyLock::new(|| pattern(r"(?i)^\W*(?:https?://|file://|www\.)\S")); + static PATH_TITLE: LazyLock = LazyLock::new(|| pattern(r"(^[A-Za-z]:[\\/])|(\\\\[^\\]+\\)|(^/(?:home|users|mnt|tmp|var)/)")); @@ -173,6 +177,7 @@ const WEIGHTS: &[(&str, u32)] = &[ ("title_in_author", 3), ("underscore_title", 1), ("unknown_author", 3), + ("url_title", 4), ]; /// Everything wrong with a record, named. @@ -199,6 +204,9 @@ pub fn problems(title: Option<&str>, author: Option<&str>, isbn: Option<&str>) - if PATH_TITLE.is_match(title) { reasons.push("path_title"); } + if URL_TITLE.is_match(title) { + reasons.push("url_title"); + } if FILENAME_TITLE.is_match(title) { reasons.push("filename_title"); }