aienos.com
Source of www.aienos.com, served from my own hardware
Brought by drakestapleton
aienos.com / crates / site-builder / src / components / boxlink.rs
| 1 | //! Every box on the site is a link to its evidence on the forge. |
| 2 | //! |
| 3 | //! A box is any card-like element: a machine card, a speed row, a receipt |
| 4 | //! card, a status row, a research card, a ladder step, a hub card, a /now/ |
| 5 | //! card, queue row or timeline entry. Each one carries exactly one |
| 6 | //! `a.box-link` in its footer, reading "Evidence on the forge" with an arrow. |
| 7 | //! The CSS stretches that anchor over the whole box (`.box-link::after` |
| 8 | //! with `inset: 0`), so the whole box is the click target while inline |
| 9 | //! links inside the box stay above it and keep working. There are no |
| 10 | //! nested anchors anywhere. |
| 11 | //! |
| 12 | //! When a box has no evidence of its own on the forge, it links to the source |
| 13 | //! file in aienos.com that holds its text and says "Source on the forge" |
| 14 | //! instead, so the footer never claims evidence it does not have. |
| 15 | //! |
| 16 | //! `BOX_CLASSES` is the list of classes the build treats as boxes. A new |
| 17 | //! component must add its card class here to be checked; `check_boxes` |
| 18 | //! fails the build if any box on any generated page lacks its link. |
| 19 | |
| 20 | use maud::{html, Markup}; |
| 21 | |
| 22 | /// The classes that make an element a box. Every element carrying one of |
| 23 | /// these on a generated page must contain an `a.box-link` to the forge. |
| 24 | pub const BOX_CLASSES: &[&str] = &[ |
| 25 | "mx-card", |
| 26 | "mx-row", |
| 27 | "glow-box", |
| 28 | "lineage-card", |
| 29 | "lineage-role", |
| 30 | "status-row", |
| 31 | "live-row", |
| 32 | "receipt", |
| 33 | "rs-card", |
| 34 | "rs-step", |
| 35 | "rs-impl-item", |
| 36 | "hub-card", |
| 37 | "narr-card", |
| 38 | "blog-card", |
| 39 | "now-card", |
| 40 | "now-queue-row", |
| 41 | "now-tl", |
| 42 | "now-usage", |
| 43 | "om-part", |
| 44 | "om-layer", |
| 45 | "om-receipt", |
| 46 | "om-gap", |
| 47 | "om-bar-row", |
| 48 | "om-beyond", |
| 49 | "cp-hop", |
| 50 | "cp-card", |
| 51 | "cp-row", |
| 52 | "dash-card", |
| 53 | ]; |
| 54 | |
| 55 | /// Where the box link points. |
| 56 | #[derive(Clone, Copy, PartialEq, Eq, Debug)] |
| 57 | pub enum Target { |
| 58 | /// A receipt, run, pull request, commit or file that is the evidence. |
| 59 | Evidence, |
| 60 | /// The source file of this site that holds the box's text. Honest when |
| 61 | /// no evidence exists on the forge yet. |
| 62 | Source, |
| 63 | } |
| 64 | |
| 65 | pub const SITE_REPO_BLOB: &str = "https://interplane.io/drakestapleton/aienos.com/blob/main/"; |
| 66 | |
| 67 | /// The blob URL of a file in this repository, for boxes that have no |
| 68 | /// evidence of their own. |
| 69 | pub fn source_href(path: &str) -> String { |
| 70 | format!("{SITE_REPO_BLOB}{path}") |
| 71 | } |
| 72 | |
| 73 | /// The tree URL of a folder in this repository (a paper's folder). |
| 74 | pub fn source_dir_href(path: &str) -> String { |
| 75 | format!("{FORGE}aienos.com/tree/main/{path}") |
| 76 | } |
| 77 | |
| 78 | /// The footer affordance, which the CSS stretches over the whole box. |
| 79 | pub fn foot(href: &str, target: Target) -> Markup { |
| 80 | let label = match target { |
| 81 | Target::Evidence => "Evidence on the forge", |
| 82 | Target::Source => "Source on the forge", |
| 83 | }; |
| 84 | foot_labelled(href, label, target) |
| 85 | } |
| 86 | |
| 87 | /// The same with a custom label, for boxes whose evidence has a better |
| 88 | /// name ("Ledger line on GitHub"). |
| 89 | pub const FORGE: &str = "https://interplane.io/drakestapleton/"; |
| 90 | |
| 91 | pub fn project_href(href: &str) -> String { |
| 92 | let Some(tail) = href.strip_prefix("https://github.com/aien-dev/") else { return href.to_owned() }; |
| 93 | let (repo, rest) = tail.split_once('/').map_or((tail, ""), |(repo, rest)| (repo, rest)); |
| 94 | let repo = match repo { |
| 95 | "omega" => "omega-mv", "aien-architecture" => "aien-architecture-mv", |
| 96 | "aien-sovereign-core" => "aien-sovereign-core-mv", "aien-protocols" => "aien-protocols-mv", |
| 97 | "benchmarks" => "benchmarks-mv", "physics" => "physics-mv", other => other, |
| 98 | }; |
| 99 | let suffix = if rest.starts_with("pull/") || rest == "pulls" { "patches" } |
| 100 | else if rest.starts_with("issues/") { "issues" } |
| 101 | else if rest.starts_with("releases/") { "releases" } else { rest }; |
| 102 | format!("{FORGE}{repo}{}{}", if suffix.is_empty() { "" } else { "/" }, suffix) |
| 103 | } |
| 104 | |
| 105 | /// Project only owned hyperlink attributes. Every other byte of a retained body is preserved. |
| 106 | /// This independent oracle lets whole-body checks retain their strength after navigation moves. |
| 107 | pub fn project_markup(source: &str) -> String { |
| 108 | let mut result = source.to_owned(); |
| 109 | for quote in ['"', '\''] { |
| 110 | let marker = format!("href={quote}https://github.com/aien-dev/"); |
| 111 | let mut output = String::new(); |
| 112 | let mut remaining = result.as_str(); |
| 113 | while let Some(start) = remaining.find(&marker) { |
| 114 | let url_start = start + 6; |
| 115 | let Some(end) = remaining[url_start..].find(quote).map(|n| url_start + n) else { break }; |
| 116 | output.push_str(&remaining[..url_start]); |
| 117 | output.push_str(&project_href(&remaining[url_start..end])); |
| 118 | remaining = &remaining[end..]; |
| 119 | } |
| 120 | output.push_str(remaining); |
| 121 | result = output; |
| 122 | } |
| 123 | result |
| 124 | } |
| 125 | |
| 126 | pub fn foot_labelled(href: &str, label: &str, target: Target) -> Markup { |
| 127 | let href = project_href(href); |
| 128 | debug_assert!(href.starts_with(FORGE), "box link must point at Interplane: {href}"); |
| 129 | let label = label.replace("GitHub", "Interplane"); |
| 130 | let class = match target { |
| 131 | Target::Evidence => "box-link", |
| 132 | Target::Source => "box-link box-link-source", |
| 133 | }; |
| 134 | html! { |
| 135 | a class=(class) href=(href) target="_blank" rel="noopener noreferrer" { |
| 136 | (label) " " span class="box-arrow" aria-hidden="true" { "\u{2197}" } |
| 137 | } |
| 138 | (crate::forge::receipt_code(&href)) |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | /// Read the value of the first `class="..."` attribute in `tag`. |
| 143 | fn class_value(tag: &str) -> Option<&str> { |
| 144 | let i = tag.find("class=\"")? + 7; |
| 145 | let rest = &tag[i..]; |
| 146 | let j = rest.find('"')?; |
| 147 | Some(&rest[..j]) |
| 148 | } |
| 149 | |
| 150 | fn href_value(tag: &str) -> Option<&str> { |
| 151 | let i = tag.find("href=\"")? + 6; |
| 152 | let rest = &tag[i..]; |
| 153 | let j = rest.find('"')?; |
| 154 | Some(&rest[..j]) |
| 155 | } |
| 156 | |
| 157 | /// Counts the boxes on a rendered page and checks that every one of them |
| 158 | /// contains an `a.box-link` whose href is on the forge. A box's region runs |
| 159 | /// from its opening tag to the next box's opening tag (boxes do not nest), |
| 160 | /// so a box whose link is missing, or whose link points anywhere but |
| 161 | /// the forge, is reported with the first words of its tag. |
| 162 | pub fn check_boxes(page: &str) -> Result<usize, String> { |
| 163 | // Every opening tag, with its position. |
| 164 | let mut tags: Vec<(usize, &str)> = Vec::new(); |
| 165 | let mut i = 0; |
| 166 | while let Some(off) = page[i..].find('<') { |
| 167 | let start = i + off; |
| 168 | let Some(len) = page[start..].find('>') else { break }; |
| 169 | let tag = &page[start..start + len + 1]; |
| 170 | if !tag.starts_with("</") && !tag.starts_with("<!") { |
| 171 | tags.push((start, tag)); |
| 172 | } |
| 173 | i = start + len + 1; |
| 174 | } |
| 175 | let is_box = |tag: &str| { |
| 176 | class_value(tag) |
| 177 | .map(|c| c.split_whitespace().any(|t| BOX_CLASSES.contains(&t))) |
| 178 | .unwrap_or(false) |
| 179 | }; |
| 180 | let box_starts: Vec<usize> = (0..tags.len()).filter(|&k| is_box(tags[k].1)).collect(); |
| 181 | let mut problems = Vec::new(); |
| 182 | for (n, &k) in box_starts.iter().enumerate() { |
| 183 | let end = box_starts.get(n + 1).copied().unwrap_or(tags.len()); |
| 184 | let has = |tag: &str, want: &str| class_value(tag).map(|c| c.split_whitespace().any(|t| t == want)).unwrap_or(false); |
| 185 | // A box naming a repository with no Radicle copy says so (class box-none) instead of linking. |
| 186 | let link = tags[k + 1..end] |
| 187 | .iter() |
| 188 | .find(|(_, tag)| (tag.starts_with("<a ") && has(tag, "box-link")) || has(tag, "box-none")); |
| 189 | if link.is_some_and(|(_, tag)| !tag.starts_with("<a ")) { |
| 190 | continue; |
| 191 | } |
| 192 | match link.map(|(_, tag)| href_value(tag)) { |
| 193 | Some(Some(h)) if h.starts_with(FORGE) || radicle_tree(h) => {} |
| 194 | Some(other) => problems.push(format!("box {} has a box-link off the forge: {other:?}", short(tags[k].1))), |
| 195 | None => problems.push(format!("box without an Interplane link: {}", short(tags[k].1))), |
| 196 | } |
| 197 | } |
| 198 | if problems.is_empty() { |
| 199 | Ok(box_starts.len()) |
| 200 | } else { |
| 201 | Err(problems.join("\n")) |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | /// A rosa radicle.network repository page, bare or at a `/tree/` path: the |
| 206 | /// home of a public repository that the forge does not serve. |
| 207 | fn radicle_tree(h: &str) -> bool { |
| 208 | let Some(rest) = h.strip_prefix(crate::forge::RADNET).and_then(|r| r.strip_prefix("/rad:z")) else { return false }; |
| 209 | let (id, path) = rest.split_once('/').map_or((rest, None), |(id, p)| (id, Some(p))); |
| 210 | !id.is_empty() |
| 211 | && id.bytes().all(|b| b.is_ascii_alphanumeric()) |
| 212 | && path.is_none_or(|p| p.strip_prefix("tree/").is_some_and(|t| !t.is_empty())) |
| 213 | } |
| 214 | |
| 215 | fn short(tag: &str) -> &str { |
| 216 | let n = tag.char_indices().nth(90).map(|(i, _)| i).unwrap_or(tag.len()); |
| 217 | &tag[..n] |
| 218 | } |
| 219 | |
| 220 | #[cfg(test)] |
| 221 | mod tests { |
| 222 | use super::*; |
| 223 | |
| 224 | #[test] |
| 225 | fn foot_projects_source_reference_to_interplane() { |
| 226 | let s = foot("https://github.com/aien-dev/omega-mv/pull/88", Target::Evidence).into_string(); |
| 227 | assert!(s.starts_with("<a class=\"box-link\" href=\"https://interplane.io/drakestapleton/omega-mv/patches\"")); |
| 228 | assert!(s.contains("Evidence on the forge")); |
| 229 | assert!(s.contains('\u{2197}')); |
| 230 | assert_eq!(s.matches("<a ").count(), 1); |
| 231 | assert!(s.ends_with("</a>"), "a patch page names no commit, so no receipt code: {s}"); |
| 232 | let c = foot("https://interplane.io/drakestapleton/omega-mv/commit/1b75aa81bd6f", Target::Evidence).into_string(); |
| 233 | assert!(c.ends_with("</a> <code>rad:z6do9WDBhuSnvopDkgFdFr1yWA62 1b75aa81bd6f</code>"), "{c}"); |
| 234 | let src = foot(&source_href("crates/site-builder/src/components/status.rs"), Target::Source).into_string(); |
| 235 | assert!(src.contains("box-link-source") && src.contains("Source on the forge")); |
| 236 | assert!(src.contains("https://interplane.io/drakestapleton/aienos.com/blob/main/crates/site-builder/src/components/status.rs")); |
| 237 | } |
| 238 | |
| 239 | #[test] |
| 240 | fn check_counts_linked_boxes() { |
| 241 | let page = r#"<div class="x"><article class="mx-card" id="a"><h3>A</h3><a href="/x">in</a><a class="box-link" href="https://interplane.io/drakestapleton/x">Evidence</a></article><li class="hub-card"><a class="box-link box-link-source" href="https://interplane.io/drakestapleton/aienos.com/blob/main/f.rs">Source</a></li></div>"#; |
| 242 | assert_eq!(check_boxes(page), Ok(2)); |
| 243 | assert_eq!(check_boxes("<p>no boxes</p>"), Ok(0)); |
| 244 | } |
| 245 | |
| 246 | #[test] |
| 247 | fn check_rejects_a_box_without_a_link() { |
| 248 | let page = r#"<article class="mx-card"><a href="https://interplane.io/drakestapleton/x">plain</a></article><article class="rs-card"><a class="box-link" href="https://interplane.io/drakestapleton/y">ok</a></article>"#; |
| 249 | let err = check_boxes(page).unwrap_err(); |
| 250 | assert!(err.contains("box without an Interplane link"), "{err}"); |
| 251 | assert!(err.contains("mx-card"), "{err}"); |
| 252 | assert!(!err.contains("rs-card"), "{err}"); |
| 253 | } |
| 254 | |
| 255 | #[test] |
| 256 | fn check_rejects_a_link_off_the_forge() { |
| 257 | let page = r#"<li class="now-tl"><a class="box-link" href="/now/">here</a></li>"#; |
| 258 | let err = check_boxes(page).unwrap_err(); |
| 259 | assert!(err.contains("off the forge"), "{err}"); |
| 260 | let page = r#"<li class="now-tl"><a class="box-link">no href</a></li>"#; |
| 261 | assert!(check_boxes(page).is_err()); |
| 262 | } |
| 263 | |
| 264 | #[test] |
| 265 | fn check_accepts_only_tree_paths_after_a_radicle_id() { |
| 266 | let ok = r#"<li class="now-tl"><a class="box-link" href="https://radicle.network/nodes/rosa.radicle.network/rad:z6do9WDBhuSnvopDkgFdFr1yWA62/tree/main/src">t</a></li>"#; |
| 267 | assert_eq!(check_boxes(ok), Ok(1)); |
| 268 | let bare = r#"<li class="now-tl"><a class="box-link" href="https://radicle.network/nodes/rosa.radicle.network/rad:z6do9WDBhuSnvopDkgFdFr1yWA62">r</a></li>"#; |
| 269 | assert_eq!(check_boxes(bare), Ok(1)); |
| 270 | for bad in ["/blob/main/src", "/", "/commits"] { |
| 271 | let page = format!(r#"<li class="now-tl"><a class="box-link" href="https://radicle.network/nodes/rosa.radicle.network/rad:z6do9WDBhuSnvopDkgFdFr1yWA62{bad}">b</a></li>"#); |
| 272 | assert!(check_boxes(&page).unwrap_err().contains("off the forge"), "{bad} is not a tree path"); |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | #[test] |
| 277 | fn a_link_in_the_next_box_does_not_count_for_this_one() { |
| 278 | let page = r#"<div class="glow-box"></div><div class="glow-box"><a class="box-link" href="https://interplane.io/drakestapleton/z">z</a></div>"#; |
| 279 | assert!(check_boxes(page).is_err()); |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | #[cfg(test)] |
| 284 | mod navigation_projection_tests { |
| 285 | use super::project_markup; |
| 286 | #[test] |
| 287 | fn retained_body_changes_only_owned_href_bytes() { |
| 288 | let source = "<a href=\"https://github.com/aien-dev/omega/blob/abc/source.om\">Source</a><pre>https://github.com/aien-dev/omega\nret add a b</pre>"; |
| 289 | let projected = project_markup(source); |
| 290 | assert_eq!(projected, source.replacen("href=\"https://github.com/aien-dev/omega/", "href=\"https://interplane.io/drakestapleton/omega-mv/", 1)); |
| 291 | assert_eq!(project_markup("<a href='/omega/'>Omega</a>"), "<a href='/omega/'>Omega</a>"); |
| 292 | assert!(projected.contains("ret add a b")); |
| 293 | } |
| 294 | } |