Expand today and default the title date

This commit is contained in:
gloamxun committed 2026-10-24 23:16:07 +08:00
1 parent fe9e93f796
commit 9b44d68440
4 files changed
+94 -8

No files matched your search

+90 -4
View File
@@ -1172,6 +1172,8 @@ fn geometry_and_body(source: &str) -> (String, PageGeometry) {
body = expand_macros(&body, &geometry.macros);
body = expand_links(&body);
body = expand_conditionals(&body, &mut geometry.conditionals);
let today = today_string();
body = expand_today(&body, &today);
(body, geometry)
}
@@ -3584,6 +3586,57 @@ fn thanks_to_footnotes(text: &str) -> String {
out
}
/// The LaTeX `\today` date in article form. Route-D has no time zone
/// database, so the date comes from the UTC Unix clock.
fn today_string() -> String {
const MONTHS: [&str; 12] = [
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December",
];
let days = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.map(|duration| (duration.as_secs() / 86_400) as i64)
.unwrap_or(0);
let (year, month, day) = civil_from_days(days);
format!("{} {}, {}", MONTHS[(month - 1) as usize], day, year)
}
fn civil_from_days(days: i64) -> (i64, u32, u32) {
let z = days + 719_468;
let era = if z >= 0 { z } else { z - 146_096 } / 146_097;
let day_of_era = z - era * 146_097;
let year_of_era =
(day_of_era - day_of_era / 1_460 + day_of_era / 36_524 - day_of_era / 146_096) / 365;
let year = year_of_era + era * 400;
let day_of_year = day_of_era - (365 * year_of_era + year_of_era / 4 - year_of_era / 100);
let month_index = (5 * day_of_year + 2) / 153;
let day = day_of_year - (153 * month_index + 2) / 5 + 1;
let month = if month_index < 10 {
month_index + 3
} else {
month_index - 9
};
(
if month <= 2 { year + 1 } else { year },
month as u32,
day as u32,
)
}
fn expand_today(text: &str, today: &str) -> String {
text.replace("\\today", today)
}
fn clean_title_value(text: &str) -> String {
let mut out = String::new();
let mut rest = text;
@@ -3664,6 +3717,7 @@ fn expand_maketitle(blocks: &[SourceParagraph], geometry: &PageGeometry) -> Vec<
return blocks.to_vec();
}
let base = geometry.base_font_size.unwrap_or(10 * SCALE);
let today = today_string();
let mut result = Vec::new();
for block in blocks {
if block.text.trim() != "\\maketitle" {
@@ -3672,7 +3726,7 @@ fn expand_maketitle(blocks: &[SourceParagraph], geometry: &PageGeometry) -> Vec<
}
if let Some(title) = geometry.title.as_deref().filter(|title| !title.is_empty()) {
let mut paragraph = title_block(
clean_title_value(&thanks_to_footnotes(title)),
clean_title_value(&expand_today(&thanks_to_footnotes(title), &today)),
title_size(base),
block.space_before,
);
@@ -3686,14 +3740,19 @@ fn expand_maketitle(blocks: &[SourceParagraph], geometry: &PageGeometry) -> Vec<
.filter(|author| !author.is_empty())
{
result.push(title_block(
clean_title_value(&thanks_to_footnotes(author)),
clean_title_value(&expand_today(&thanks_to_footnotes(author), &today)),
title_large_size(base),
author_space,
));
}
if let Some(date) = geometry.date.as_deref().filter(|date| !date.is_empty()) {
let date = match geometry.date.as_deref() {
Some(date) if date.trim().is_empty() => None,
Some(date) => Some(expand_today(date, &today)),
None => Some(today.clone()),
};
if let Some(date) = date {
result.push(title_block(
clean_title_value(&thanks_to_footnotes(date)),
clean_title_value(&thanks_to_footnotes(&date)),
title_large_size(base),
12 * SCALE,
));
@@ -4813,6 +4872,33 @@ See \ref{fig:a} and \ref{tab:b}.";
assert_eq!(blocks[1].space_before, 0);
}
#[test]
fn today_expands_to_the_current_date() {
let (body, _geometry) = geometry_and_body("Body \\today.");
assert_eq!(body.trim(), format!("Body {}.", today_string()));
let date = today_string();
assert!(date.chars().any(|character| character.is_ascii_digit()));
assert!(date.contains(','));
}
#[test]
fn maketitle_defaults_to_today_when_no_date_is_set() {
let (body, geometry) = geometry_and_body("\\maketitle");
let blocks = paragraphs(&body).expect("paragraphs");
let document = build_document(&blocks, &geometry);
let today = today_string();
assert!(document.units.iter().any(|unit| unit.source == today));
}
#[test]
fn explicit_empty_date_suppresses_the_title_date() {
let (body, geometry) = geometry_and_body("\\date{}\n\\maketitle");
let blocks = paragraphs(&body).expect("paragraphs");
let document = build_document(&blocks, &geometry);
let today = today_string();
assert!(document.units.iter().all(|unit| unit.source != today));
}
#[test]
fn nested_itemize_indents_and_relabels() {
let source = "\\begin{itemize}\n\\item Outer\n\\begin{itemize}\n\\item Inner\n\\end{itemize}\n\\end{itemize}";