aienos.com
Source of www.aienos.com, served from my own hardware
Brought by drakestapleton
aienos.com / crates / site-builder / src / components / narrative.rs
| 1 | //! Shared pieces for the research narrative pages (/machine/ and /discovery/). |
| 2 | //! |
| 3 | //! Every status word on these pages (levels, PASS/FAIL style verdicts and |
| 4 | //! implementation classes) comes from the one canonical record, |
| 5 | //! data/research_status.json, through components::research_status. This |
| 6 | //! module only turns a data row into a badge; it never names a status itself. |
| 7 | |
| 8 | use maud::{html, Markup}; |
| 9 | |
| 10 | use super::research_status::{ImplementationRow, TuringRow}; |
| 11 | |
| 12 | /// The research doctrine line (synthesis handoff sections 2 and 22). |
| 13 | pub const SLOGAN: &str = |
| 14 | "Weights suggest. Programs explain. Verification decides. Evidence teaches. The Turing keeps score."; |
| 15 | |
| 16 | /// The Turing definition (synthesis handoff section 3). |
| 17 | pub const TURING_FORMULA: &str = "T(M; B, D, P) = [L(B) + L(D | B)] - [L(M) + L(D | M)]"; |
| 18 | |
| 19 | fn slug(s: &str) -> String { |
| 20 | let mut out = String::new(); |
| 21 | for ch in s.chars() { |
| 22 | if ch.is_ascii_alphanumeric() { |
| 23 | out.push(ch.to_ascii_lowercase()); |
| 24 | } else if !out.ends_with('-') { |
| 25 | out.push('-'); |
| 26 | } |
| 27 | } |
| 28 | out.trim_matches('-').to_string() |
| 29 | } |
| 30 | |
| 31 | /// Badge for a Turing or experiment row. Text and modifier both come from |
| 32 | /// the row's status, so the shared rs-badge styles apply unchanged. |
| 33 | pub fn status_badge(row: &TuringRow) -> Markup { |
| 34 | html! { |
| 35 | span class=(format!("rs-badge rs-badge--{}", slug(&row.status))) data-row=(row.id) { |
| 36 | span class="visually-hidden" { "Status: " } |
| 37 | (row.status) |
| 38 | } |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | /// Badge for an implementation row. The text is the row's class from the |
| 43 | /// data; the modifier gives each class its own border style, so the class |
| 44 | /// never depends on colour alone. |
| 45 | pub fn class_badge(row: &ImplementationRow) -> Markup { |
| 46 | html! { |
| 47 | span class=(format!("rs-badge rs-class--{}", slug(&row.class))) data-impl=(row.id) { |
| 48 | span class="visually-hidden" { "Status: " } |
| 49 | (row.class) |
| 50 | } |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | #[cfg(test)] |
| 55 | pub mod check { |
| 56 | //! Test helper: find status words in page markup that the data-driven |
| 57 | //! components did not produce. |
| 58 | |
| 59 | /// Status words that only the data may supply. Upper-case verdicts are |
| 60 | /// matched exactly; class labels are matched as words in either case. |
| 61 | const VERDICTS: [&str; 7] = |
| 62 | ["PASS", "FAIL", "BLOCKED", "INCOMPLETE", "PARTIAL", "NOT STARTED", "NOT ESTABLISHED"]; |
| 63 | const CLASS_WORDS: [&str; 5] = ["implemented", "planned", "experimental", "hypothesis", "achieved"]; |
| 64 | /// Lower-case uses allowed in fixed prose: the mission line calls AienOS |
| 65 | /// "an owned experimental machine", which describes the project, not a |
| 66 | /// status; Cortex keeps "experimental conditions" as evidence; the Zeta |
| 67 | /// exam names the Riemann hypothesis; the nature section says "Science |
| 68 | /// runs hypothesis, experiment and theory". None of these is a status. |
| 69 | const ALLOWED_LOWER: [&str; 4] = |
| 70 | ["owned experimental machine", "experimental conditions", "Riemann hypothesis", "Science runs hypothesis"]; |
| 71 | |
| 72 | fn strip_tags(s: &str) -> String { |
| 73 | let mut out = String::new(); |
| 74 | let mut in_tag = false; |
| 75 | for ch in s.chars() { |
| 76 | match ch { |
| 77 | '<' => in_tag = true, |
| 78 | '>' => { |
| 79 | in_tag = false; |
| 80 | out.push(' '); |
| 81 | } |
| 82 | _ if !in_tag => out.push(ch), |
| 83 | _ => {} |
| 84 | } |
| 85 | } |
| 86 | out |
| 87 | } |
| 88 | |
| 89 | /// Remove every occurrence of each component rendering from `page`, then |
| 90 | /// return the hand-written status words that remain. |
| 91 | pub fn stray_status_words(page: &str, components: &[String]) -> Vec<String> { |
| 92 | let mut rest = page.to_string(); |
| 93 | for c in components.iter().filter(|c| !c.is_empty()) { |
| 94 | rest = rest.replace(c.as_str(), " "); |
| 95 | } |
| 96 | let mut text = strip_tags(&rest); |
| 97 | for a in ALLOWED_LOWER { |
| 98 | text = text.replace(a, " "); |
| 99 | } |
| 100 | let mut found = Vec::new(); |
| 101 | for v in VERDICTS { |
| 102 | if text.contains(v) { |
| 103 | found.push(v.to_string()); |
| 104 | } |
| 105 | } |
| 106 | for w in text.split(|c: char| !c.is_ascii_alphanumeric()) { |
| 107 | let lw = w.to_ascii_lowercase(); |
| 108 | if CLASS_WORDS.contains(&lw.as_str()) { |
| 109 | found.push(w.to_string()); |
| 110 | } |
| 111 | let b = w.as_bytes(); |
| 112 | if b.len() == 2 && b[0] == b'L' && b[1].is_ascii_digit() { |
| 113 | found.push(w.to_string()); |
| 114 | } |
| 115 | } |
| 116 | found |
| 117 | } |
| 118 | } |