code wiki / _hdl_build / nx_site_autofix.nx
nx_site_autofix.nx source
↩ module page · 109 lines · 5446 B
1// nx_site_autofix.nx -- table-driven accessibility AUTOFIX (the finding->fix->re-gate loop of the
2// CMS arc). Applies targeted splices for the quality gate's REAL findings on andelinwest.html:
3// skip-link after <body>, <nav> around the header CTA, <main id="main"> after </header>, </main>
4// before <footer>. Atomic write (.new + rename -- the live daemon hot-swaps on next request).
5// Idempotent: each fix is skipped if its marker already exists. license_tier: ORIGINAL
6import "nx_syscalls.nx"
7const K_MAGIC_4194320: i64 = 4194320
8const K_MAGIC_2097152: i64 = 2097152
9const K_MAGIC_8192: i64 = 8192
10func _p(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 }
11func _pn(v: i64) -> i64 { let bb: *u8=sys_mmap(28); var m: i64=v; if m<0{m=0-m;sys_write(1,"-" as *u8,1)}; let t: *u8=sys_mmap(28); var k: i64=0; if m==0{t[0]=48;k=1}; while m>0{t[k]=48+(m%10);m=m/10;k=k+1}; var i: i64=0; while i<k{bb[i]=t[k-1-i];i=i+1}; sys_write(1,bb,k); return 0 }
12func af_slen(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } return n }
13func af_find(buf: *u8, n: i64, from: i64, pat: *u8) -> i64 {
14 let pl: i64 = af_slen(pat)
15 var i: i64 = from
16 while i + pl <= n {
17 var k: i64 = 0
18 var hit: i64 = 1
19 while k < pl { if buf[i+k] != pat[k] { hit = 0; k = pl } else { k = k + 1 } }
20 if hit == 1 { return i }
21 i = i + 1
22 }
23 return 0 - 1
24}
25// insert s at pos: shift tail right
26func af_insert(buf: *u8, n: i64, pos: i64, s: *u8) -> i64 {
27 let sl: i64 = af_slen(s)
28 var i: i64 = n - 1
29 while i >= pos { buf[i + sl] = buf[i]; i = i - 1 }
30 var k: i64 = 0
31 while k < sl { buf[pos + k] = s[k]; k = k + 1 }
32 return n + sl
33}
34func main() -> i64 {
35 _p("=== SITE AUTOFIX: applying the quality gate's findings (skip-link, nav, main) ===\n" as *u8)
36 let buf: *u8 = sys_mmap(K_MAGIC_4194320)
37 let fd: i64 = sys_openat_rd("knowledge/andelinwest.html" as *u8)
38 if fd < 0 { sys_exit(1); return 1 }
39 var n: i64 = 0
40 var go: i64 = 1
41 while go == 1 {
42 let base: i64 = buf as i64
43 let r: i64 = sys_read(fd, (base + n) as *u8, K_MAGIC_2097152 - n)
44 if r <= 0 { go = 0 } else { n = n + r }
45 }
46 sys_close(fd)
47 var fixes: i64 = 0
48 // fix 1: skip link right after <body> (idempotent on class="skip")
49 if af_find(buf, n, 0, "class=\"skip\"" as *u8) < 0 {
50 let p1: i64 = af_find(buf, n, 0, "<body>" as *u8)
51 if p1 >= 0 { n = af_insert(buf, n, p1 + 6, "<a class=\"skip\" href=\"#main\">Skip to content</a>" as *u8); fixes = fixes + 1 }
52 }
53 // fix 2: nav around the header CTA (idempotent on <nav)
54 if af_find(buf, n, 0, "<nav" as *u8) < 0 {
55 let p2: i64 = af_find(buf, n, 0, "<a class=\"cta\" href=\"/contact" as *u8)
56 if p2 >= 0 {
57 n = af_insert(buf, n, p2, "<nav aria-label=\"Primary\">" as *u8)
58 let p3: i64 = af_find(buf, n, p2, "</a>" as *u8)
59 if p3 >= 0 { n = af_insert(buf, n, p3 + 4, "</nav>" as *u8); fixes = fixes + 1 }
60 }
61 }
62 // fix 3: <main id="main"> after </header>, </main> before <footer (idempotent on <main)
63 if af_find(buf, n, 0, "<main" as *u8) < 0 {
64 let p4: i64 = af_find(buf, n, 0, "</header>" as *u8)
65 if p4 >= 0 {
66 n = af_insert(buf, n, p4 + 9, "<main id=\"main\">" as *u8)
67 let p5: i64 = af_find(buf, n, 0, "<footer" as *u8)
68 if p5 >= 0 { n = af_insert(buf, n, p5, "</main>" as *u8); fixes = fixes + 1 }
69 }
70 }
71 // fix 4: RELOCATE a mid-page search form into the header (NN/g placement convention; operator's
72 // "weirdly positioned search bar" case). Idempotent: skip if search already precedes </header>.
73 let psr: i64 = af_find(buf, n, 0, "role=\"search\"" as *u8)
74 let phd: i64 = af_find(buf, n, 0, "</header>" as *u8)
75 if psr >= 0 { if phd >= 0 { if psr > phd {
76 // block = enclosing <form ... </form>
77 var fs: i64 = psr
78 var scan: i64 = 0
79 while scan == 0 {
80 if fs <= 0 { scan = 1 }
81 else { if buf[fs] == (60 as u8) { if buf[fs+1] == (102 as u8) { if buf[fs+2] == (111 as u8) { scan = 1 } else { fs = fs - 1 } } else { fs = fs - 1 } } else { fs = fs - 1 } }
82 }
83 let fe0: i64 = af_find(buf, n, psr, "</form>" as *u8)
84 if fe0 >= 0 { if fs > 0 {
85 let fe: i64 = fe0 + 7
86 let blen: i64 = fe - fs
87 let tmp: *u8 = sys_mmap(K_MAGIC_8192)
88 var q: i64 = 0
89 while q < blen { tmp[q] = buf[fs + q]; q = q + 1 }
90 tmp[blen] = 0 as u8
91 // delete [fs, fe)
92 var d: i64 = fs
93 while d + blen < n { buf[d] = buf[d + blen]; d = d + 1 }
94 n = n - blen
95 // re-find </header> (offsets shifted) and insert the form before it
96 let ph2: i64 = af_find(buf, n, 0, "</header>" as *u8)
97 if ph2 >= 0 { n = af_insert(buf, n, ph2, tmp); fixes = fixes + 1 }
98 } }
99 } } }
100 // atomic write: .new then rename (the hot-swap law)
101 let ofd: i64 = sys_openat_wr("knowledge/andelinwest.html.new" as *u8, 0x1a4)
102 if ofd < 0 { sys_exit(1); return 1 }
103 sys_write(ofd, buf, n)
104 sys_close(ofd)
105 sys_renameat("knowledge/andelinwest.html.new" as *u8, "knowledge/andelinwest.html" as *u8)
106 _p(" fixes applied=" as *u8); _pn(fixes); _p(" bytes=" as *u8); _pn(n); _p(" (atomic rename done)\n" as *u8)
107 sys_exit(0)
108 return 0
109}