| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
use std::sync::OnceLock; |
| 25 |
|
| 26 |
use quasi_webview::Shell; |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
const V: &str = env!("STATIC_VERSION"); |
| 36 |
|
| 37 |
fn parts() -> &'static quasi_webview::Parts { |
| 38 |
static PARTS: OnceLock<quasi_webview::Parts> = OnceLock::new(); |
| 39 |
PARTS.get_or_init(|| { |
| 40 |
Shell::under("/static") |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
.layered(["base", "components", "responsive"]) |
| 52 |
|
| 53 |
|
| 54 |
.with_head_first( |
| 55 |
"<link rel=\"preload\" href=\"/static/fonts/Lato-Regular.woff2\" as=\"font\" type=\"font/woff2\" crossorigin>\ |
| 56 |
<link rel=\"preload\" href=\"/static/fonts/ysrf.woff2\" as=\"font\" type=\"font/woff2\" crossorigin>", |
| 57 |
) |
| 58 |
.styled(format!("/static/geometry.css?v={V}")) |
| 59 |
.styled(format!("/static/layout.css?v={V}")) |
| 60 |
.styled(format!("/static/style.css?v={V}")) |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
.with_head(format!( |
| 66 |
"<link rel=\"icon\" href=\"/static/images/favicon.ico\" type=\"image/x-icon\">\ |
| 67 |
<script src=\"/static/upload.js?v={V}\"></script>\ |
| 68 |
<script type=\"module\" src=\"/static/dist-{V}/core/index.js\"></script>" |
| 69 |
)) |
| 70 |
.parts() |
| 71 |
}) |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
pub fn head() -> &'static str { |
| 79 |
&parts().head |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
pub fn body_attrs() -> &'static str { |
| 87 |
&parts().body_attrs |
| 88 |
} |
| 89 |
|
| 90 |
pub use makeover_layout::Measure; |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
#[must_use] |
| 110 |
pub const fn measure(measure: Measure) -> &'static str { |
| 111 |
match measure { |
| 112 |
Measure::Contained => "centered-page", |
| 113 |
Measure::Reading => "article-page", |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
_ => "padded-page", |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
#[cfg(test)] |
| 122 |
mod tests { |
| 123 |
use super::*; |
| 124 |
|
| 125 |
#[test] |
| 126 |
fn the_layer_statement_precedes_every_stylesheet() { |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
let head = head(); |
| 131 |
let stmt = head |
| 132 |
.find("@layer makeover, base, components, responsive;") |
| 133 |
.expect("the order is stated"); |
| 134 |
for sheet in ["geometry.css", "layout.css", "style.css"] { |
| 135 |
assert!(stmt < head.find(sheet).expect("the sheet is linked")); |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
#[test] |
| 140 |
fn the_head_is_not_closed_and_carries_no_title() { |
| 141 |
|
| 142 |
|
| 143 |
assert!(!head().contains("</head>")); |
| 144 |
assert!(!head().contains("<title>")); |
| 145 |
assert!(head().starts_with("<!doctype html><html lang=\"en\">")); |
| 146 |
} |
| 147 |
|
| 148 |
#[test] |
| 149 |
fn the_fonts_are_preloaded_before_the_sheets_that_race_them() { |
| 150 |
let head = head(); |
| 151 |
assert!(head.find("Lato-Regular.woff2") < head.find("style.css")); |
| 152 |
} |
| 153 |
|
| 154 |
#[test] |
| 155 |
fn the_body_attributes_start_with_a_space_so_a_page_can_add_its_own() { |
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
let attrs = body_attrs(); |
| 160 |
assert!(attrs.starts_with(' ')); |
| 161 |
assert!(attrs.contains("hx-ext=\"morph\"")); |
| 162 |
|
| 163 |
|
| 164 |
assert!(!attrs.contains("class=")); |
| 165 |
} |
| 166 |
|
| 167 |
#[test] |
| 168 |
fn every_measure_keeps_the_class_the_templates_used_to_write() { |
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
assert_eq!(measure(Measure::Wide), "padded-page"); |
| 173 |
assert_eq!(measure(Measure::Contained), "centered-page"); |
| 174 |
assert_eq!(measure(Measure::Reading), "article-page"); |
| 175 |
} |
| 176 |
|
| 177 |
#[test] |
| 178 |
fn no_template_still_writes_a_layout_class_by_hand() { |
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
let mut offenders = Vec::new(); |
| 186 |
for entry in walk("templates") { |
| 187 |
let source = std::fs::read_to_string(&entry).expect("a template reads"); |
| 188 |
for (at, line) in source.lines().enumerate() { |
| 189 |
if !line.contains("block body_attrs") { |
| 190 |
continue; |
| 191 |
} |
| 192 |
|
| 193 |
|
| 194 |
let derived = line.contains("crate::shell::measure("); |
| 195 |
let literal = ["padded-page", "centered-page", "article-page"] |
| 196 |
.iter() |
| 197 |
.any(|name| line.contains(name)); |
| 198 |
if literal && !derived { |
| 199 |
offenders.push(format!("{}:{}", entry.display(), at + 1)); |
| 200 |
} |
| 201 |
} |
| 202 |
} |
| 203 |
assert!(offenders.is_empty(), "{offenders:?}"); |
| 204 |
} |
| 205 |
|
| 206 |
|
| 207 |
fn walk(root: &str) -> Vec<std::path::PathBuf> { |
| 208 |
let mut found = Vec::new(); |
| 209 |
let mut stack = vec![std::path::PathBuf::from(root)]; |
| 210 |
while let Some(at) = stack.pop() { |
| 211 |
let Ok(entries) = std::fs::read_dir(&at) else { |
| 212 |
continue; |
| 213 |
}; |
| 214 |
for entry in entries.flatten() { |
| 215 |
let path = entry.path(); |
| 216 |
if path.is_dir() { |
| 217 |
stack.push(path); |
| 218 |
} else if path.extension().is_some_and(|ext| ext == "html") { |
| 219 |
found.push(path); |
| 220 |
} |
| 221 |
} |
| 222 |
} |
| 223 |
found |
| 224 |
} |
| 225 |
|
| 226 |
#[test] |
| 227 |
fn morph_is_loaded_wherever_a_swap_can_ask_for_it() { |
| 228 |
|
| 229 |
|
| 230 |
assert!(head().contains("idiomorph-ext.min.js")); |
| 231 |
assert!( |
| 232 |
std::path::Path::new(concat!( |
| 233 |
env!("CARGO_MANIFEST_DIR"), |
| 234 |
"/static/idiomorph-ext.min.js" |
| 235 |
)) |
| 236 |
.exists() |
| 237 |
); |
| 238 |
} |
| 239 |
} |
| 240 |
|