fn gen_html()

in src/html.rs [393:497]


fn gen_html(
    tera: &Tera,
    path: &Path,
    result: &CovResult,
    conf: &Config,
    output: &Path,
    rel_path: &Path,
    global: Arc<Mutex<HtmlGlobalStats>>,
    abs_link_prefix: &Option<String>,
) {
    if !rel_path.is_relative() {
        return;
    }

    let mut f = match File::open(path) {
        Err(_) => {
            //eprintln!("Warning: cannot open file {:?}", path);
            return;
        }
        Ok(f) => f,
    };

    let stats = get_stats(result);
    get_dirs_result(global, rel_path, &stats);

    let output_file = output.join(add_html_ext(rel_path));
    create_parent(&output_file);
    let mut output = match File::create(&output_file) {
        Err(_) => {
            eprintln!("Cannot create file {:?}", output_file);
            return;
        }
        Ok(f) => f,
    };
    let base_url = get_base(rel_path);
    let filename = rel_path.file_name().unwrap().to_str().unwrap();
    let parent = rel_path.parent().unwrap().to_str().unwrap().to_string();
    let mut index_url = base_url;
    index_url.push_str("index.html");

    let mut ctx = make_context(conf, &get_base(rel_path));
    ctx.insert("current", filename);

    let (top_level_link, parent_link) = match abs_link_prefix {
        Some(prefix) => {
            let prefix_buf = PathBuf::from(prefix);
            let mut top_level = prefix_buf.clone();
            top_level.push("index.html");
            let parent_buf = PathBuf::from(parent.clone());
            let mut abs_parent = prefix_buf.join(parent_buf);
            abs_parent.push(PathBuf::from("index.html"));
            (
                top_level.display().to_string(),
                abs_parent.display().to_string(),
            )
        }
        None => (index_url.to_string(), "./index.html".to_string()),
    };
    ctx.insert(
        "parents",
        &[
            (top_level_link, "top_level"),
            (parent_link, parent.as_str()),
        ],
    );
    ctx.insert("stats", &stats);

    let mut file_buf = Vec::new();
    if let Err(e) = f.read_to_end(&mut file_buf) {
        eprintln!("Failed to read {}: {}", path.display(), e);
        return;
    }

    let file_utf8 = String::from_utf8_lossy(&file_buf);
    if matches!(&file_utf8, Cow::Owned(_)) {
        // from_utf8_lossy needs to reallocate only when invalid UTF-8, warn.
        eprintln!(
            "Warning: invalid utf-8 characters in source file {}. They will be replaced by U+FFFD",
            path.display()
        );
    }

    let items = file_utf8
        .lines()
        .enumerate()
        .map(move |(i, l)| {
            let index = i + 1;
            let count = result
                .lines
                .get(&(index as u32))
                .map(|&v| v as i64)
                .unwrap_or(-1);

            (index, count, l)
        })
        .collect::<Vec<_>>();

    ctx.insert("items", &items);

    let out = tera.render("file.html", &ctx).unwrap();

    if output.write_all(out.as_bytes()).is_err() {
        eprintln!("Cannot write the file {:?}", output_file);
    }
}