code wiki / _hdl_build / nx_mobi_headings_gate.nx

nx_mobi_headings_gate.nx source

↩ module page · 73 lines · 5626 B

1// nx_mobi_headings_gate.nx -- gate the HEADING-FALLBACK chapter split: a MOBI with <h1> chapter titles but NO 2// <mbp:pagebreak> still gets a real navigable TOC (the lib falls back to splitting on top-level <h1> when there are 3// no pagebreaks; 2+ headings -> chapters; the empty bit before the first heading is skipped). expect_exit: 0 4import "nx_syscalls.nx" 5 6func gp(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 } 7func gnum(v0: i64) -> i64 { var v: i64=v0; if v<0 { sys_write(1,"-" as *u8,1); v=0-v } let b: *u8=sys_mmap(24); var k: i64=0; if v==0 {b[0]=48 as u8;k=1} while v>0 {b[k]=(48+(v%10)) as u8; v=v/10; k=k+1} let o: *u8=sys_mmap(24); var j: i64=0; while j<k {o[j]=b[k-1-j];j=j+1} sys_write(1,o,k); return 0 } 8func ghas(hay: *u8, hl: i64, needle: *u8) -> i64 { 9 var nl: i64=0; while needle[nl]!=(0 as u8){nl=nl+1} 10 if nl == 0 { return 0 } 11 var i: i64 = 0 12 while i + nl <= hl { var k: i64=0; var hit: i64=1; while k<nl { if hay[i+k]!=needle[k]{hit=0;k=nl}else{k=k+1} } if hit==1 {return 1} i=i+1 } 13 return 0 14} 15func rd(path: *u8, lenp: *i64) -> *u8 { lenp[0]=0; return sys_read_file(path, lenp) } 16func spawn(path: *u8, a0: *u8, a1: *u8, redir: *u8) -> i64 { 17 let pid: i64 = sys_fork() 18 if pid == 0 { 19 let fd: i64 = sys_openat_wr(redir, 420) 20 if fd >= 0 { sys_dup3(fd, 1, 0); sys_dup3(fd, 2, 0) } 21 let argv: *i64 = sys_mmap(64) as *i64 22 var n: i64 = 0 23 argv[0] = path as i64; n = 1 24 if (a0 as i64) != 0 { argv[n] = a0 as i64; n = n + 1 } 25 if (a1 as i64) != 0 { argv[n] = a1 as i64; n = n + 1 } 26 argv[n] = 0 27 let envp: *i64 = sys_mmap(16) as *i64 28 envp[0] = "PATH=/usr/bin:/bin\x00" as *u8 as i64; envp[1] = 0 29 sys_execve(path, argv, envp) 30 sys_exit(127) 31 } 32 let st: *i64 = sys_mmap(16) as *i64 33 sys_wait4(pid, st, 0) 34 return (st[0] >> 8) & 0xff 35} 36 37func main() -> i64 { 38 gp("=== nx_mobi_headings_gate: <h1> heading-fallback -> real chapters + TOC (no pagebreaks) ===\n" as *u8) 39 let RUNNER: *u8 = "_offc/nx_sov_build_run.elf\x00" as *u8 40 let MB: *u8 = "_offc/nx_mobi_book.elf\x00" as *u8 41 let SC: *u8 = "knowledge/status/mobi_headings_scratch.log\x00" as *u8 42 43 spawn(RUNNER, "nx_mobi_fixture_build\x00" as *u8, 0 as *u8, SC) 44 let rc: i64 = spawn(MB, "knowledge/fixtures/nishi_mobi_headings.mobi\x00" as *u8, "nishi_mobi_headings_fixture\x00" as *u8, SC) 45 46 let lj: *i64 = sys_mmap(8) as *i64; let bj: *u8 = rd("knowledge/staging/media/reader/nishi_mobi_headings_fixture/book.json\x00" as *u8, lj); let bn: i64 = lj[0] 47 let l0: *i64 = sys_mmap(8) as *i64; let c0: *u8 = rd("knowledge/staging/media/reader/nishi_mobi_headings_fixture/chap0.txt\x00" as *u8, l0); let n0: i64 = l0[0] 48 let l1: *i64 = sys_mmap(8) as *i64; let c1: *u8 = rd("knowledge/staging/media/reader/nishi_mobi_headings_fixture/chap1.txt\x00" as *u8, l1); let n1: i64 = l1[0] 49 let l2: *i64 = sys_mmap(8) as *i64; let c2: *u8 = rd("knowledge/staging/media/reader/nishi_mobi_headings_fixture/chap2.txt\x00" as *u8, l2); let n2: i64 = l2[0] 50 51 gp(" rc=" as *u8); gnum(rc); gp(" book.json=" as *u8); gnum(bn); gp("B chap0/1/2=" as *u8); gnum(n0); gp("/" as *u8); gnum(n1); gp("/" as *u8); gnum(n2); gp("\n" as *u8) 52 53 var pass: i64 = 0; var fail: i64 = 0 54 if rc == 0 { pass=pass+1 } else { fail=fail+1; gp(" FAIL decode-nonzero\n" as *u8) } 55 if (bj as i64)==0 { gp("MOBI-HEADINGS-GATE fail=1 verdict=RED no-book.json\n" as *u8); sys_exit(1); return 1 } 56 if ghas(bj, bn, "\"nchapters\":3" as *u8)==1 { pass=pass+1 } else { fail=fail+1; gp(" FAIL not 3 chapters\n" as *u8) } 57 if ghas(bj, bn, "\"toc_source\":\"mobi-heading\"" as *u8)==1 { pass=pass+1 } else { fail=fail+1; gp(" FAIL toc_source not heading-fallback\n" as *u8) } 58 // titles came from the <h1>s 59 if ghas(bj, bn, "\"title\":\"Alpha\"" as *u8)==1 { pass=pass+1 } else { fail=fail+1; gp(" FAIL no Alpha title\n" as *u8) } 60 if ghas(bj, bn, "\"title\":\"Beta\"" as *u8)==1 { pass=pass+1 } else { fail=fail+1; gp(" FAIL no Beta title\n" as *u8) } 61 if ghas(bj, bn, "\"title\":\"Gamma\"" as *u8)==1 { pass=pass+1 } else { fail=fail+1; gp(" FAIL no Gamma title\n" as *u8) } 62 // content landed in the right chapter file 63 if (c0 as i64)!=0 { if ghas(c0, n0, "alpha wolves" as *u8)==1 { pass=pass+1 } else { fail=fail+1; gp(" FAIL ch0 wrong content\n" as *u8) } } else { fail=fail+1; gp(" FAIL no chap0\n" as *u8) } 64 if (c1 as i64)!=0 { if ghas(c1, n1, "beta fish" as *u8)==1 { pass=pass+1 } else { fail=fail+1; gp(" FAIL ch1 wrong content\n" as *u8) } } else { fail=fail+1; gp(" FAIL no chap1\n" as *u8) } 65 if (c2 as i64)!=0 { if ghas(c2, n2, "gamma rays" as *u8)==1 { pass=pass+1 } else { fail=fail+1; gp(" FAIL ch2 wrong content\n" as *u8) } } else { fail=fail+1; gp(" FAIL no chap2\n" as *u8) } 66 // LIAR-KILL: the empty pre-heading segment was skipped (chap0 is Alpha, not blank) + the split is real (ch1 != ch0 text) 67 if (c0 as i64)!=0 { if n0 > 5 { pass=pass+1 } else { fail=fail+1; gp(" FAIL chap0 blank (empty leading segment not skipped)\n" as *u8) } } 68 if (c1 as i64)!=0 { if ghas(c1, n1, "alpha wolves" as *u8)==0 { pass=pass+1 } else { fail=fail+1; gp(" FAIL ch1 contains ch0 text (split is fake)\n" as *u8) } } 69 70 gp("MOBI-HEADINGS-GATE pass=" as *u8); gnum(pass); gp(" fail=" as *u8); gnum(fail) 71 if fail == 0 { gp(" verdict=GREEN (no pagebreaks -> heading-fallback gives 3 chapters w/ <h1> titles, empty leading segment skipped, real split)\n" as *u8); sys_exit(0); return 0 } 72 gp(" verdict=RED\n" as *u8); sys_exit(1); return 1 73}