♻️ refactor

This commit is contained in:
_ 2025-09-11 02:16:18 +00:00
parent 5784a299a7
commit 70f302b5b5
3 changed files with 186 additions and 95 deletions

View file

@ -66,12 +66,55 @@ fn calendar_link(calendar_ui: &crate::CalendarUi) -> maud::PreEscaped<String> {
}
}
pub(crate) fn write_html(
fn calendars_page(upstreams: &[crate::CalendarUi], now: DateTime<chrono_tz::Tz>) -> String {
let description = "A list of upstream calendars used by this Wide-Angle Calendar instance";
let title = "Upstream calendars";
maud::html! {
(maud::PreEscaped("<!DOCTYPE html>"))
html lang="en" {
head {
meta http-equiv="Content-Type" content="text/html; charset=utf-8" {}
meta name="viewport" content="width=device-width, initial-scale=1" {}
(maud::PreEscaped(CSS))
meta property="og:locale" content="en" {}
meta property="og:type" content="website" {}
meta name="description" content=(description) {}
meta property="description" content=(description) {}
meta property="og:description" content=(description) {}
title { (title) }
met property="og:title" content=(title) {}
}
body {
h1 { (title) }
p {
a href="index.html" { "Wide-Angle Calendar" }
" / "
a href="calendars.html" { (title) }
}
p { "Written at: " (now.format("%F %T")) }
p { "These are the calendars that Wide-Angle Calendar pulls from." }
ol {
@for upstream in upstreams {
li { (calendar_link(upstream)) }
}
}
}
}
}
.into_string()
}
fn index_page(
config: &Config,
upstreams: &[crate::CalendarUi],
instances: &[EventInstance],
now: DateTime<chrono_tz::Tz>,
) -> Result<()> {
) -> Result<String> {
let today = now.date_naive();
let mut last_month_printed: Option<String> = None;
let mut last_date_printed = None;
@ -170,100 +213,59 @@ pub(crate) fn write_html(
});
}
let description = &config.description;
let title = &config.title;
let s = maud::html! {
(maud::PreEscaped("<!DOCTYPE html>"))
html lang="en" {
head {
meta http-equiv="Content-Type" content="text/html; charset=utf-8" {}
meta name="viewport" content="width=device-width, initial-scale=1" {}
(maud::PreEscaped(CSS))
meta property="og:locale" content="en" {}
meta property="og:type" content="website" {}
meta name="description" content=(description) {}
meta property="description" content=(description) {}
meta property="og:description" content=(description) {}
title { (title) }
met property="og:title" content=(title) {}
}
body {
h1 { (title) }
img src="hero.webp" width="700" height="233" {}
p { "Written at: " (now.format("%F %T")) }
p { a href = "calendars.html" { "Upstream calendars" } }
@for entry in html_list {
(entry)
}
}
}
}
.into_string();
Ok(s)
}
fn atomic_write(path: &str, content: &str) -> Result<()> {
let mut file = atomic_write_file::AtomicWriteFile::options().open(path)?;
file.write_all(content.as_bytes())?;
file.commit()?;
Ok(())
}
pub(crate) fn write_html(
config: &Config,
upstreams: &[crate::CalendarUi],
instances: &[EventInstance],
now: DateTime<chrono_tz::Tz>,
) -> Result<()> {
std::fs::create_dir_all("output")?;
{
let temp_path = "output/calendars.html.tmp";
let final_path = "output/calendars.html";
let mut f = std::fs::File::create(temp_path)?;
let description = "A list of upstream calendars used by this Wide-Angle Calendar instance";
let title = "Upstream calendars";
let s = maud::html! {
(maud::PreEscaped("<!DOCTYPE html>"))
html lang="en" {
head {
meta http-equiv="Content-Type" content="text/html; charset=utf-8" {}
meta name="viewport" content="width=device-width, initial-scale=1" {}
(maud::PreEscaped(CSS))
meta property="og:locale" content="en" {}
meta property="og:type" content="website" {}
meta name="description" content=(description) {}
meta property="description" content=(description) {}
meta property="og:description" content=(description) {}
title { (title) }
met property="og:title" content=(title) {}
}
body {
h1 { (title) }
p {
a href="index.html" { "Wide-Angle Calendar" }
" / "
a href="calendars.html" { (title) }
}
p { "Written at: " (now.format("%F %T")) }
p { "These are the calendars that Wide-Angle Calendar pulls from." }
ol {
@for upstream in upstreams {
li { (calendar_link(upstream)) }
}
}
}
}
}
.into_string();
f.write_all(s.as_bytes())?;
std::fs::rename(temp_path, final_path)?;
}
{
let temp_path = "output/index.html.tmp";
let final_path = "output/index.html";
let mut f = std::fs::File::create(temp_path)?;
let description = &config.description;
let title = &config.title;
let s = maud::html! {
(maud::PreEscaped("<!DOCTYPE html>"))
html lang="en" {
head {
meta http-equiv="Content-Type" content="text/html; charset=utf-8" {}
meta name="viewport" content="width=device-width, initial-scale=1" {}
(maud::PreEscaped(CSS))
meta property="og:locale" content="en" {}
meta property="og:type" content="website" {}
meta name="description" content=(description) {}
meta property="description" content=(description) {}
meta property="og:description" content=(description) {}
title { (title) }
met property="og:title" content=(title) {}
}
body {
h1 { (title) }
img src="hero.webp" width="700" height="233" {}
p { "Written at: " (now.format("%F %T")) }
p { a href = "calendars.html" { "Upstream calendars" } }
@for entry in html_list {
(entry)
}
}
}
}
.into_string();
f.write_all(s.as_bytes())?;
std::fs::rename(temp_path, final_path)?;
}
atomic_write("output/calendars.html", &calendars_page(upstreams, now))?;
atomic_write("output/index.html", &index_page(config, instances, now)?)?;
Ok(())
}