use std::fs;
use std::path::Path;
use std::time::UNIX_EPOCH;
const SIMPLE_CSS: &str = include_str!("../assets/simple.min.css");
const APP_DISPLAY_NAME: &str = "mdo";
const APP_VERSION: &str = env!("CARGO_PKG_VERSION");
const MDO_DEFAULT_TYPOGRAPHY_CSS: &str = include_str!("../assets/mdo-default-typography.css");
const GFM_ALERTS_CSS: &str = r#"
:root{--alert-note:#0969da;--alert-tip:#1a7f37;--alert-important:#8250df;--alert-warning:#9a6700;--alert-caution:#cf222e}
@media (prefers-color-scheme: dark){:root{--alert-note:#58a6ff;--alert-tip:#3fb950;--alert-important:#a371f7;--alert-warning:#d29922;--alert-caution:#f85149}}
:root[data-theme="light"]{--alert-note:#0969da;--alert-tip:#1a7f37;--alert-important:#8250df;--alert-warning:#9a6700;--alert-caution:#cf222e}
:root[data-theme="dark"]{--alert-note:#58a6ff;--alert-tip:#3fb950;--alert-important:#a371f7;--alert-warning:#d29922;--alert-caution:#f85149}
blockquote[class^="markdown-alert-"]{--alert-color:var(--accent);border-left:.3rem solid var(--alert-color);background:var(--accent-bg);padding:.75rem 1rem}
blockquote.markdown-alert-note{--alert-color:var(--alert-note)}blockquote.markdown-alert-tip{--alert-color:var(--alert-tip)}blockquote.markdown-alert-important{--alert-color:var(--alert-important)}blockquote.markdown-alert-warning{--alert-color:var(--alert-warning)}blockquote.markdown-alert-caution{--alert-color:var(--alert-caution)}
"#;
const THEME_TOGGLE: &str = r#"<style id="mdo-theme-toggle">
:root[data-theme="light"]{color-scheme:light;--bg:#fff;--accent-bg:#f5f7ff;--text:#212121;--text-light:#585858;--accent:#0d47a1;--accent-hover:#1266e2;--accent-text:var(--bg);--code:#d81b60;--preformatted:#444;--disabled:#efefef}
:root[data-theme="dark"]{color-scheme:dark;--bg:#212121;--accent-bg:#2b2b2b;--text:#dcdcdc;--text-light:#ababab;--accent:#ffb300;--accent-hover:#ffe099;--accent-text:var(--bg);--code:#f06292;--preformatted:#ccc;--disabled:#111}
:root[data-theme="dark"] img,:root[data-theme="dark"] video{opacity:.8}
#theme-toggle{position:fixed;top:.75rem;right:.75rem;z-index:1000;padding:.25rem .6rem;font-size:1rem;line-height:1;cursor:pointer;border-radius:var(--standard-border-radius);border:var(--border-width) solid var(--border);background:var(--accent-bg);color:var(--text)}
@media (max-width:55rem){body{padding-top:2.75rem}}
@media print{#theme-toggle{display:none}}
</style>
<script>
(function(){
var read=function(){try{return localStorage.getItem('theme')}catch(e){return null}};
var apply=function(t){document.documentElement.dataset.theme=t;};
var mq=matchMedia('(prefers-color-scheme: dark)');
var saved=read();
var manual=saved!==null;
apply(saved||(mq.matches?'dark':'light'));
document.addEventListener('DOMContentLoaded',function(){
var b=document.createElement('button');
b.id='theme-toggle';b.type='button';
var sync=function(){
var dark=document.documentElement.dataset.theme==='dark';
b.textContent=dark?'\u2600':'\u263E';
var label=dark?'Switch to light theme':'Switch to dark theme';
b.title=label;
b.setAttribute('aria-label',label);
};
b.onclick=function(){
var next=document.documentElement.dataset.theme==='dark'?'light':'dark';
apply(next);
manual=true;
try{localStorage.setItem('theme',next)}catch(e){}
sync();
};
if(mq.addEventListener){
mq.addEventListener('change',function(e){
if(!manual){apply(e.matches?'dark':'light');sync();}
});
}
document.body.appendChild(b);
sync();
});
})();
</script>
"#;
pub(crate) fn wrap_html5(
body: &str,
title: &str,
base_href: Option<&str>,
css_override: Option<&str>,
highlight_css: Option<&str>,
source_modified_unix_secs: Option<u64>,
) -> String {
let base_tag = match base_href {
Some(href) => format!("<base href=\"{}\">\n", html_escape(href)),
None => String::new(),
};
let css_override_block = css_override
.filter(|css| !css.trim().is_empty())
.map(|css| {
format!(
"<style id=\"mdo-css-override\">\n/* Custom CSS from --css */\n{}\n</style>\n",
escape_style_end_tags(css)
)
})
.unwrap_or_default();
let mdo_default_typography = format!(
"<style id=\"mdo-default-typography\">\n{}\n</style>\n",
escape_style_end_tags(MDO_DEFAULT_TYPOGRAPHY_CSS)
);
let highlight_css = highlight_css.unwrap_or_default();
let generator = format!("{APP_DISPLAY_NAME} {APP_VERSION}");
let source_meta = source_modified_unix_secs
.map(|secs| {
let machine = utc_datetime_from_unix_secs(secs);
let human = human_utc_datetime_from_unix_secs(secs);
format!(
"<footer class=\"mdo-source-meta\">Source modified: <time datetime=\"{machine}\">{human}</time></footer>\n\
<script>\n\
(function(){{var t=document.querySelector('.mdo-source-meta time');if(!t)return;var d=new Date(t.getAttribute('datetime'));if(isNaN(d))return;try{{t.textContent=d.toLocaleString(undefined,{{year:'numeric',month:'long',day:'numeric',hour:'numeric',minute:'2-digit'}});}}catch(e){{}}}})();\n\
</script>\n",
machine = html_escape(&machine),
human = html_escape(&human),
)
})
.unwrap_or_default();
format!(
"<!DOCTYPE html>\n\
<html lang=\"en\">\n\
<head>\n\
<meta charset=\"utf-8\">\n\
<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">\n\
<meta name=\"generator\" content=\"{generator}\">\n\
{base_tag}\
<title>{title}</title>\n\
<style>\n{css}\n{gfm_alerts_css}\n{highlight_css}\n.mdo-source-meta{{margin-top:3rem;border:0;padding:1rem 0 1.5rem;font-size:.8rem;line-height:1.4;color:var(--text-light);text-align:center}}\n</style>\n\
{theme_toggle}\
{mdo_default_typography}\
{css_override_block}\
</head>\n\
<body>\n\
<main>\n{body}\n</main>\n\
{source_meta}\
</body>\n\
</html>\n",
generator = html_escape(&generator),
base_tag = base_tag,
title = html_escape(title),
css = SIMPLE_CSS,
gfm_alerts_css = GFM_ALERTS_CSS,
highlight_css = highlight_css,
theme_toggle = THEME_TOGGLE, mdo_default_typography = mdo_default_typography,
css_override_block = css_override_block,
body = body,
source_meta = source_meta,
)
}
fn escape_style_end_tags(css: &str) -> String {
const STYLE_END_PREFIX: &[u8] = b"</style";
let mut escaped = String::with_capacity(css.len());
let bytes = css.as_bytes();
let mut i = 0;
while i < css.len() {
if i + STYLE_END_PREFIX.len() <= css.len()
&& bytes[i..i + STYLE_END_PREFIX.len()].eq_ignore_ascii_case(STYLE_END_PREFIX)
{
escaped.push_str("<\\/style");
i += STYLE_END_PREFIX.len();
} else {
let ch = css[i..]
.chars()
.next()
.expect("index should always point at a char boundary");
escaped.push(ch);
i += ch.len_utf8();
}
}
escaped
}
const MONTH_NAMES: [&str; 12] = [
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December",
];
pub(crate) fn source_modified_unix_secs(input: &Path) -> Option<u64> {
fs::metadata(input)
.and_then(|metadata| metadata.modified())
.ok()
.and_then(|mtime| mtime.duration_since(UNIX_EPOCH).ok())
.map(|elapsed| elapsed.as_secs())
}
fn utc_datetime_from_unix_secs(secs: u64) -> String {
let days = (secs / 86_400) as i64;
let (year, month, day) = civil_from_days(days);
let seconds_of_day = secs % 86_400;
let hour = seconds_of_day / 3_600;
let minute = (seconds_of_day % 3_600) / 60;
let second = seconds_of_day % 60;
format!("{year:04}-{month:02}-{day:02}T{hour:02}:{minute:02}:{second:02}Z")
}
fn human_utc_datetime_from_unix_secs(secs: u64) -> String {
let days = (secs / 86_400) as i64;
let (year, month, day) = civil_from_days(days);
let seconds_of_day = secs % 86_400;
let hour24 = seconds_of_day / 3_600;
let minute = (seconds_of_day % 3_600) / 60;
let (hour12, meridiem) = match hour24 {
0 => (12, "AM"),
1..=11 => (hour24, "AM"),
12 => (12, "PM"),
_ => (hour24 - 12, "PM"),
};
let month_name = MONTH_NAMES[(month - 1) as usize];
format!("{month_name} {day}, {year}, {hour12}:{minute:02} {meridiem} UTC")
}
fn civil_from_days(days_since_unix_epoch: i64) -> (i64, i64, i64) {
let z = days_since_unix_epoch + 719_468;
let era = if z >= 0 { z } else { z - 146_096 } / 146_097;
let doe = z - era * 146_097;
let yoe = (doe - doe / 1_460 + doe / 36_524 - doe / 146_096) / 365;
let y = yoe + era * 400;
let doy = doe - (365 * yoe + yoe / 4 - yoe / 100);
let mp = (5 * doy + 2) / 153;
let day = doy - (153 * mp + 2) / 5 + 1;
let month = mp + if mp < 10 { 3 } else { -9 };
let year = y + if month <= 2 { 1 } else { 0 };
(year, month, day)
}
fn html_escape(s: &str) -> String {
s.replace('&', "&")
.replace('<', "<")
.replace('>', ">")
.replace('"', """)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn formats_unix_timestamps_as_machine_utc_datetimes() {
assert_eq!(utc_datetime_from_unix_secs(0), "1970-01-01T00:00:00Z");
assert_eq!(
utc_datetime_from_unix_secs(951_827_696),
"2000-02-29T12:34:56Z"
);
}
#[test]
fn formats_unix_timestamps_as_readable_utc_datetimes() {
assert_eq!(
human_utc_datetime_from_unix_secs(0),
"January 1, 1970, 12:00 AM UTC"
);
assert_eq!(
human_utc_datetime_from_unix_secs(951_827_696),
"February 29, 2000, 12:34 PM UTC"
);
assert_eq!(
human_utc_datetime_from_unix_secs(13 * 3_600 + 5 * 60),
"January 1, 1970, 1:05 PM UTC"
);
}
#[test]
fn wrap_html5_keeps_generator_meta_without_visible_branding() {
let html = wrap_html5("<p>hi</p>", "Title", None, None, None, Some(0));
assert!(html.contains(&format!(
"<meta name=\"generator\" content=\"mdo {APP_VERSION}\">"
)));
assert!(!html.contains("Generated by"));
assert!(!html.contains("mdo-generated"));
}
#[test]
fn wrap_html5_shows_source_modified_time_with_machine_datetime() {
let html = wrap_html5("<p>hi</p>", "Title", None, None, None, Some(951_827_696));
assert!(html.contains(
"<footer class=\"mdo-source-meta\">Source modified: \
<time datetime=\"2000-02-29T12:34:56Z\">February 29, 2000, 12:34 PM UTC</time></footer>"
));
}
#[test]
fn wrap_html5_renders_without_source_modified_time() {
let html = wrap_html5("<p>hi</p>", "Title", None, None, None, None);
assert!(html.contains("<main>\n<p>hi</p>\n</main>"));
assert!(!html.contains("Source modified"));
assert!(!html.contains("<footer"));
}
#[test]
fn wrapped_document_hides_theme_toggle_in_print() {
let html = wrap_html5("<p>hi</p>", "Title", None, None, None, None);
let fixed_position = html
.find("#theme-toggle{position:fixed")
.expect("toggle should be positioned on screen");
let print_hide = html
.find("@media print{#theme-toggle{display:none}}")
.expect("toggle should be hidden in print");
assert!(print_hide > fixed_position);
}
#[test]
fn wrapped_document_uses_compact_print_typography() {
let html = wrap_html5("<p>hi</p>", "Title", None, None, None, None);
let typography = html
.split_once("<style id=\"mdo-default-typography\">")
.expect("default typography block should be present")
.1
.split_once("</style>")
.expect("typography block should be closed")
.0;
assert!(typography.contains("@media print {"));
assert!(typography.contains("font-size: 11pt;"));
assert!(typography.contains("font-size: 20pt;"));
assert!(typography.contains("font-size: 1rem;"));
}
#[test]
fn wrapped_document_styles_alerts_in_light_and_dark_palettes() {
let html = wrap_html5("<p>hi</p>", "Title", None, None, None, None);
assert!(html.contains("\n:root{--alert-note:"));
assert!(html.contains("@media (prefers-color-scheme: dark){:root{--alert-note:"));
assert!(html.contains(":root[data-theme=\"light\"]{--alert-note:"));
assert!(html.contains(":root[data-theme=\"dark\"]{--alert-note:"));
for kind in ["note", "tip", "important", "warning", "caution"] {
assert!(html.contains(&format!("blockquote.markdown-alert-{kind}")));
}
}
#[test]
fn css_override_escapes_style_end_tags() {
let escaped = escape_style_end_tags("h1{} </STYLE><script>alert(1)</script>");
assert!(escaped.contains("<\\/style><script>"));
assert!(!escaped.to_ascii_lowercase().contains("</style><script>"));
}
}