Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/aienos.com/blob/main/crates/site-builder/src/oracle.rs # aienos.com Source of www.aienos.com, served from my own hardware Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `aienos.com / crates / site-builder / src / oracle.rs` 1201 lines ยท 60598 bytes Raw Blame History 1 //! Guards that run in `verify` mode on any generated tree, whatever built it. 2 //! 3 //! Before the Omega site generator, several gates ran only inside `build`: 4 //! the dist/registry comparison, the head checks and the validation of each 5 //! published data file. A tree built by another generator never met them. 6 //! This module holds those gates as pure checks over the files in dist, so 7 //! `site-builder verify DIR` is a complete, independent oracle. 8 //! 9 //! Independence: every expected value here comes from Rust constants and 10 //! Rust parsers ([`seo::PAGES`], [`seo::DATA_FILES`], the component 11 //! validators). Nothing is read from the generator's own registry 12 //! (`om/site/pages.json` is not an input), so a page the generator adds or 13 //! drops on its own is caught rather than trusted. 14 15 use std::collections::BTreeSet; 16 use std::fs; 17 use std::path::Path; 18 use std::sync::LazyLock; 19 20 use regex::Regex; 21 use serde_json::Value; 22 23 use crate::seo::{self, attr, tag_name, DATA_FILES, HOST, PAGES}; 24 25 /// The first opening tag at or after `from` whose class attribute holds 26 /// `token` as a whole word: (tag start, index just past its `>`, tag name). 27 pub fn find_class(html: &str, from: usize, token: &str) -> Option<(usize, usize, String)> { 28 let mut i = from; 29 while let Some(lt) = html.get(i..)?.find('<') { 30 let at = i + lt; 31 let gt = at + html[at..].find('>')?; 32 let tag = &html[at..=gt]; 33 if !tag.starts_with(" Option { 48 let lower = html.to_ascii_lowercase(); 49 let (start, close) = (format!("<{name}"), format!("")); 50 let mut depth = 1usize; 51 let mut i = body; 52 while let Some(lt) = lower[i..].find('<') { 53 let at = i + lt; 54 let tail = &lower[at..]; 55 if tail.starts_with(&close) { 56 depth -= 1; 57 if depth == 0 { 58 return Some(at + close.len()); 59 } 60 } else if tail.starts_with(&start) && tail[start.len()..].starts_with(['>', ' ', '\t', '\n', '\r', '/']) { 61 depth += 1; 62 } 63 i = at + 1; 64 } 65 None 66 } 67 68 /// Elements that have no close tag. 69 const VOID: [&str; 13] = ["area", "base", "br", "col", "embed", "hr", "img", "input", "link", "meta", "source", "track", "wbr"]; 70 71 /// The attribute names of one opening tag, lowercased, skipping quoted 72 /// values, so `title="hidden"` or `class="visually-hidden"` is no attribute 73 /// named `hidden`. 74 pub fn attr_names(tag: &str) -> Vec { 75 let b = tag.as_bytes(); 76 let ws = |c: u8| c.is_ascii_whitespace(); 77 let mut i = 1; 78 while i < b.len() && !ws(b[i]) && b[i] != b'>' && b[i] != b'/' { 79 i += 1; 80 } 81 let mut out = Vec::new(); 82 loop { 83 while i < b.len() && (ws(b[i]) || b[i] == b'/') { 84 i += 1; 85 } 86 if i >= b.len() || b[i] == b'>' { 87 break; 88 } 89 let start = i; 90 while i < b.len() && !ws(b[i]) && b[i] != b'=' && b[i] != b'>' && b[i] != b'/' { 91 i += 1; 92 } 93 if i > start { 94 out.push(tag[start..i].to_ascii_lowercase()); 95 } 96 while i < b.len() && ws(b[i]) { 97 i += 1; 98 } 99 if i < b.len() && b[i] == b'=' { 100 i += 1; 101 while i < b.len() && ws(b[i]) { 102 i += 1; 103 } 104 if i < b.len() && (b[i] == b'"' || b[i] == b'\'') { 105 let q = b[i]; 106 i += 1; 107 while i < b.len() && b[i] != q { 108 i += 1; 109 } 110 i += 1; 111 } else { 112 while i < b.len() && !ws(b[i]) && b[i] != b'>' { 113 i += 1; 114 } 115 } 116 } 117 } 118 out 119 } 120 121 /// One pass over `html` that drops comments, the listed elements (through 122 /// their close) and, when `hidden` is set, every element whose opening tag 123 /// carries the `hidden` attribute or `aria-hidden="true"`. Anything that 124 /// never closes is dropped to the end, so malformed markup can only remove 125 /// content from the checks, never add it. 126 fn strip(html: &str, drop: &[&str], hidden: bool) -> String { 127 let mut out = String::with_capacity(html.len()); 128 let mut i = 0; 129 while let Some(lt) = html[i..].find('<') { 130 let at = i + lt; 131 out.push_str(&html[i..at]); 132 let rest = &html[at..]; 133 if rest.starts_with("").map_or(html.len(), |e| at + e + 3); 135 out.push(' '); 136 continue; 137 } 138 let name = tag_name(rest); 139 let Some(gt) = rest.find('>').map(|g| at + g) else { 140 i = html.len(); 141 break; 142 }; 143 let tag = &html[at..=gt]; 144 let opening = !rest.starts_with("") { 150 gt + 1 151 } else { 152 element_end(html, gt + 1, &name).unwrap_or(html.len()) 153 }; 154 out.push(' '); 155 continue; 156 } 157 out.push_str(tag); 158 i = gt + 1; 159 } 160 out.push_str(&html[i.min(html.len())..]); 161 out 162 } 163 164 /// The real markup of a page: comments, scripts, styles and templates 165 /// removed, so an id or link written inside them is not an element. 166 pub fn markup(html: &str) -> String { 167 strip(html, &["script", "style", "template"], false) 168 } 169 170 /// The part of a page a reader reaches with the page as served: the real 171 /// markup, minus `