use crate::clean::{Doc, Section, collapse, fold, is_word};
use pdf_oxide::PdfDocument;
use std::time::{Duration, Instant};
pub fn build_pdf(raw: &[u8], url: &str, fin: &str, budget_s: u64) -> Result<Doc, String> {
let t0 = Instant::now();
let pdf = PdfDocument::from_bytes(raw.to_vec()).map_err(|e| format!("pdf: {e}"))?;
let n = pdf.page_count().map_err(|e| format!("pdf: {e}"))?;
let sections: Vec<Section> = (0..n)
.take_while(|&i| i == 0 || t0.elapsed() < Duration::from_secs(budget_s))
.map(|i| Section {
level: 1,
title: format!("page {}", i + 1),
lines: pdf.extract_text(i).map(|t| t.lines().filter_map(line).collect()).unwrap_or_default(),
})
.collect();
let cut = n - sections.len();
let first = sections.first().map(|s| fold(&s.lines.join(" "))).unwrap_or_default();
let title = info_title(&pdf).filter(|t| on_page(t, &first));
let mut info: indexmap::IndexMap<_, _> = title.iter().map(|t| ("title".to_string(), t.clone())).collect();
info.insert("read_ms".into(), t0.elapsed().as_millis().to_string());
Ok(Doc { info, cut, ..Doc::new(url, fin, title.unwrap_or_else(|| file_name(fin)), sections, "pdf") })
}
pub fn empty_pages(d: &Doc) -> Vec<usize> {
d.sections.iter().enumerate().filter(|(_, s)| s.lines.is_empty()).map(|(i, _)| i + 1).collect()
}
pub fn cut_short(raw: &[u8], d: &Doc) -> bool {
let tail = &raw[raw.len().saturating_sub(1024)..];
!tail.windows(5).any(|w| w == b"%%EOF") && !empty_pages(d).is_empty()
}
pub fn xref_ok(raw: &[u8]) -> bool {
let from = raw.len().saturating_sub(1024);
let Some(i) = raw[from..].windows(9).rposition(|w| w == b"startxref") else { return true };
fn num(b: &[u8]) -> Option<(usize, &[u8])> {
let b = b.trim_ascii_start();
let n = b.iter().take_while(|c| c.is_ascii_digit()).count();
std::str::from_utf8(&b[..n]).ok()?.parse::<usize>().ok().map(|v| (v, &b[n..]))
}
let Some((at, _)) = num(&raw[from + i + 9..]) else { return true };
let Some(t) = raw.get(at..).map(<[u8]>::trim_ascii_start) else { return false };
let obj = num(t).and_then(|(_, r)| num(r)).is_some_and(|(_, r)| r.trim_ascii_start().starts_with(b"obj"));
t.starts_with(b"xref") || obj
}
fn line(s: &str) -> Option<String> {
let s = collapse(s);
let words: Vec<&str> = s.split(' ').collect();
let dots = |w: &str| !w.is_empty() && w.chars().all(|c| c == '.');
let leader = |run: &[&str]| match run.iter().map(|w| w.len()).sum::<usize>() {
_ if !dots(run[0]) => run[0].to_string(),
n @ 1..=3 => ".".repeat(n),
_ => "…".into(),
};
let l = words.chunk_by(|a, b| dots(a) && dots(b)).map(leader).collect::<Vec<_>>().join(" ");
(!l.is_empty()).then_some(l)
}
fn info_title(pdf: &PdfDocument) -> Option<String> {
let info = pdf.resolve_object(pdf.trailer().as_dict()?.get("Info")?).ok()?;
let t = pdf.resolve_object(info.as_dict()?.get("Title")?).ok()?;
let b = t.as_string()?;
let utf16 = |unit: fn([u8; 2]) -> u16| {
let u: Vec<u16> = b[2..].as_chunks::<2>().0.iter().copied().map(unit).collect();
String::from_utf16_lossy(&u)
};
let s = match b {
[0xFE, 0xFF, ..] => utf16(u16::from_be_bytes),
[0xFF, 0xFE, ..] => utf16(u16::from_le_bytes),
_ => b.iter().map(|&c| c as char).collect(),
};
let s = collapse(&s);
(!s.is_empty() && !s.chars().any(char::is_control)).then_some(s)
}
fn on_page(title: &str, page1: &str) -> bool {
let f = fold(title);
let words: Vec<&str> = f.split(|c: char| !is_word(c) || c == '_').filter(|w| w.chars().count() >= 3).collect();
let hits = words.iter().filter(|w| page1.contains(**w)).count();
title.contains(' ') && !words.is_empty() && hits * 2 >= words.len()
}
pub fn file_name(u: &str) -> String {
let p = u.split(['?', '#']).next().unwrap_or(u);
let name = p.trim_end_matches('/').rsplit('/').next().unwrap_or(p);
if name.is_empty() { u.to_string() } else { name.to_string() }
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn lines_fold_dot_leaders_and_spaces() {
assert_eq!(line("TUNA TARTARE wontons . . . . . . . 23").as_deref(), Some("TUNA TARTARE wontons … 23"));
assert_eq!(line("Contents ........ 4").as_deref(), Some("Contents … 4"));
assert_eq!(line("Inc. 2025.").as_deref(), Some("Inc. 2025."));
assert_eq!(line("a ... b").as_deref(), Some("a ... b"));
assert_eq!(line(" "), None);
}
#[test]
fn metadata_title_must_be_on_page_one() {
let p1 = fold("Token Efficient Task Execution via Application Behavior Modeling Alexandru Ianta");
assert!(on_page("Token Efficient Task Execution via Application Behavior Modeling for Web Agents", &p1));
assert!(!on_page("printmgr file", &fold("BERKSHIRE HATHAWAY INC. 2025 ANNUAL REPORT")));
assert!(!on_page("2025.09.02_Kyiv_meniu_A3_darbinis", &fold("Kyiv meniu")));
}
#[test]
fn xref_offset_checks_the_copy() {
let whole: &[u8] = include_bytes!("../tests/fixtures/literal-escape.pdf");
assert!(xref_ok(whole) && xref_ok(include_bytes!("../tests/fixtures/three-pages.pdf")));
let at = whole.windows(4).position(|w| w == b"\\x9c").expect("the literal text");
let shifted = [&whole[..at], &[0x9c], &whole[at + 4..]].concat();
assert!(!xref_ok(&shifted));
assert!(xref_ok(b"%PDF-1.4 no end"), "nothing to check");
let stream = b"%PDF-1.5\n7 0 obj\n<< /Type /XRef >>\nstartxref\n9\n%%EOF";
assert!(xref_ok(stream), "an xref stream");
assert!(!xref_ok(b"%PDF-1.5\nstartxref\n99999\n%%EOF"), "past the end");
}
#[test]
fn file_name_of_url() {
assert_eq!(file_name("file:///tmp/a/menu.pdf"), "menu.pdf");
assert_eq!(file_name("https://x.example/doc.pdf?x=1"), "doc.pdf");
}
#[test]
fn garbage_is_an_error_not_a_panic() {
assert!(build_pdf(b"%PDF-1.7 garbage", "u", "u", 10).is_err());
}
#[test]
fn cut_short_needs_a_lost_page() {
let pdf: &[u8] = include_bytes!("../tests/fixtures/three-pages.pdf");
let read =
|b: &[u8]| build_pdf(b, "u", "u", 10).map(|d| (d.sections.len() - empty_pages(&d).len(), cut_short(b, &d)));
assert_eq!(read(pdf), Ok((3, false)));
assert_eq!(read(&pdf[..pdf.len() - 6]), Ok((3, false)), "`%%EOF` line lost, all 3 pages read");
let mid = pdf.windows(7).position(|w| w == b"5 0 obj").expect("page 2") + 40;
assert_eq!(read(&pdf[..mid]), Ok((1, true)), "cut inside page 2");
}
#[test]
fn budget_cuts_at_a_page() {
let pdf = include_bytes!("../tests/fixtures/two-pages.pdf");
let d = build_pdf(pdf, "u", "u", 10).expect("pdf");
assert_eq!((d.sections.len(), d.cut), (2, 0));
let d = build_pdf(pdf, "u", "u", 0).expect("pdf");
assert_eq!((d.sections.len(), d.cut), (1, 1), "page 1 is always read");
}
}