code wiki / _hdl_build / nx_mobi_chapters_gate.nx

nx_mobi_chapters_gate.nx source

↩ module page · 74 lines · 5681 B

1// nx_mobi_chapters_gate.nx -- gate MOBI chapter-splitting (rung 2 = a real navigable TOC, not one giant chapter). 2// A 3-chapter MOBI (<mbp:pagebreak> delimited) -> book.json with 3 chapters + 3 TOC entries (titles from <h1>) + 3 3// chap<i>.txt files, EACH holding only its own chapter's text (the split is real, not all-in-one). The zero-JS 4// reader already renders chapters[]/toc[] (EPUB path), so multi-chapter book.json => a working TOC for free. 5// expect_exit: 0 license_tier: ORIGINAL 6import "nx_syscalls.nx" 7 8func gp(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 } 9func 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 } 10func ghas(hay: *u8, hl: i64, needle: *u8) -> i64 { 11 var nl: i64=0; while needle[nl]!=(0 as u8){nl=nl+1} 12 if nl == 0 { return 0 } 13 var i: i64 = 0 14 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 } 15 return 0 16} 17func rd(path: *u8, lenp: *i64) -> *u8 { lenp[0]=0; return sys_read_file(path, lenp) } 18func spawn(path: *u8, a0: *u8, a1: *u8, redir: *u8) -> i64 { 19 let pid: i64 = sys_fork() 20 if pid == 0 { 21 let fd: i64 = sys_openat_wr(redir, 420) 22 if fd >= 0 { sys_dup3(fd, 1, 0); sys_dup3(fd, 2, 0) } 23 let argv: *i64 = sys_mmap(64) as *i64 24 var n: i64 = 0 25 argv[0] = path as i64; n = 1 26 if (a0 as i64) != 0 { argv[n] = a0 as i64; n = n + 1 } 27 if (a1 as i64) != 0 { argv[n] = a1 as i64; n = n + 1 } 28 argv[n] = 0 29 let envp: *i64 = sys_mmap(16) as *i64 30 envp[0] = "PATH=/usr/bin:/bin\x00" as *u8 as i64; envp[1] = 0 31 sys_execve(path, argv, envp) 32 sys_exit(127) 33 } 34 let st: *i64 = sys_mmap(16) as *i64 35 sys_wait4(pid, st, 0) 36 return (st[0] >> 8) & 0xff 37} 38 39func main() -> i64 { 40 gp("=== nx_mobi_chapters_gate: <mbp:pagebreak> -> real chapters + navigable TOC ===\n" as *u8) 41 let RUNNER: *u8 = "_offc/nx_sov_build_run.elf\x00" as *u8 42 let MB: *u8 = "_offc/nx_mobi_book.elf\x00" as *u8 43 let SC: *u8 = "knowledge/status/mobi_chapters_scratch.log\x00" as *u8 44 45 spawn(RUNNER, "nx_mobi_fixture_build\x00" as *u8, 0 as *u8, SC) 46 let rc: i64 = spawn(MB, "knowledge/fixtures/nishi_mobi_chapters.mobi\x00" as *u8, "nishi_mobi_chapters_fixture\x00" as *u8, SC) 47 48 let lj: *i64 = sys_mmap(8) as *i64; let bj: *u8 = rd("knowledge/staging/media/reader/nishi_mobi_chapters_fixture/book.json\x00" as *u8, lj); let bn: i64 = lj[0] 49 let l0: *i64 = sys_mmap(8) as *i64; let c0: *u8 = rd("knowledge/staging/media/reader/nishi_mobi_chapters_fixture/chap0.txt\x00" as *u8, l0); let n0: i64 = l0[0] 50 let l1: *i64 = sys_mmap(8) as *i64; let c1: *u8 = rd("knowledge/staging/media/reader/nishi_mobi_chapters_fixture/chap1.txt\x00" as *u8, l1); let n1: i64 = l1[0] 51 let l2: *i64 = sys_mmap(8) as *i64; let c2: *u8 = rd("knowledge/staging/media/reader/nishi_mobi_chapters_fixture/chap2.txt\x00" as *u8, l2); let n2: i64 = l2[0] 52 53 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) 54 55 var pass: i64 = 0; var fail: i64 = 0 56 if rc == 0 { pass=pass+1 } else { fail=fail+1; gp(" FAIL decode-nonzero\n" as *u8) } 57 if (bj as i64)==0 { gp("MOBI-CHAPTERS-GATE fail=1 verdict=RED no-book.json\n" as *u8); sys_exit(1); return 1 } 58 if ghas(bj, bn, "\"nchapters\":3" as *u8)==1 { pass=pass+1 } else { fail=fail+1; gp(" FAIL not 3 chapters\n" as *u8) } 59 if ghas(bj, bn, "\"ntoc\":3" as *u8)==1 { pass=pass+1 } else { fail=fail+1; gp(" FAIL not 3 TOC entries\n" as *u8) } 60 if ghas(bj, bn, "\"toc_source\":\"mobi-pagebreak\"" as *u8)==1 { pass=pass+1 } else { fail=fail+1; gp(" FAIL toc_source not pagebreak\n" as *u8) } 61 if ghas(bj, bn, "Chapter One" as *u8)==1 { pass=pass+1 } else { fail=fail+1; gp(" FAIL no Chapter One title\n" as *u8) } 62 if ghas(bj, bn, "Chapter Three" as *u8)==1 { pass=pass+1 } else { fail=fail+1; gp(" FAIL no Chapter Three title\n" as *u8) } 63 if ghas(bj, bn, "chap2.txt" as *u8)==1 { pass=pass+1 } else { fail=fail+1; gp(" FAIL no chap2.txt\n" as *u8) } 64 // content landed in the RIGHT chapter file 65 if (c0 as i64)!=0 { if ghas(c0, n0, "rabbit hole" as *u8)==1 { pass=pass+1 } else { fail=fail+1; gp(" FAIL ch0 missing its text\n" as *u8) } } else { fail=fail+1; gp(" FAIL no chap0\n" as *u8) } 66 if (c1 as i64)!=0 { if ghas(c1, n1, "pocket watch" as *u8)==1 { pass=pass+1 } else { fail=fail+1; gp(" FAIL ch1 missing its text\n" as *u8) } } else { fail=fail+1; gp(" FAIL no chap1\n" as *u8) } 67 if (c2 as i64)!=0 { if ghas(c2, n2, "tea party" as *u8)==1 { pass=pass+1 } else { fail=fail+1; gp(" FAIL ch2 missing its text\n" as *u8) } } else { fail=fail+1; gp(" FAIL no chap2\n" as *u8) } 68 // LIAR-KILL: the split is REAL -> chapter 1's text must NOT be in chapter 2's file (not all-in-one) 69 if (c1 as i64)!=0 { if ghas(c1, n1, "rabbit hole" as *u8)==0 { pass=pass+1 } else { fail=fail+1; gp(" FAIL ch1 contains ch0 text (split is fake)\n" as *u8) } } 70 71 gp("MOBI-CHAPTERS-GATE pass=" as *u8); gnum(pass); gp(" fail=" as *u8); gnum(fail) 72 if fail == 0 { gp(" verdict=GREEN (real chapters from <mbp:pagebreak>: 3 chapters, 3 TOC entries, each chap file holds only its own text)\n" as *u8); sys_exit(0); return 0 } 73 gp(" verdict=RED\n" as *u8); sys_exit(1); return 1 74}