evobench_tools/
html_files.rs

1//! Helpers/templates for writing HTML output files
2
3use std::{path::Path, sync::Arc};
4
5use ahtml::{HtmlAllocator, att};
6use anyhow::Result;
7
8use crate::ctx;
9
10/// Create a HTML file that when loaded in the browser redirects to
11/// `url`
12pub fn write_redirect_html_file(path: &Path, url: &str) -> Result<()> {
13    let html = HtmlAllocator::new(1000000, Arc::new("write_redirect_html_file"));
14    let doc = html.html(
15        [],
16        [
17            html.head(
18                [],
19                [html.meta(
20                    [
21                        att("http-equiv", "refresh"),
22                        // XX escaping?
23                        att("content", format!("0, url={url}")),
24                    ],
25                    [],
26                )?],
27            )?,
28            html.body(
29                [],
30                [html.p(
31                    [],
32                    [
33                        html.text("Redirecting to ")?,
34                        html.a([att("href", url)], [html.text(url)?])?,
35                    ],
36                )?],
37            )?,
38        ],
39    )?;
40    let doc_string = html.to_html_string(doc, true);
41    std::fs::write(path, &doc_string).map_err(ctx!("writing html file to {path:?}"))?;
42    Ok(())
43}