code wiki / _hdl_build / nx_nmz_author.nx

nx_nmz_author.nx source

↩ module page · 78 lines · 6688 B

1// nx_nmz_author.nx -- NATIVE authoring into .nmz (operator: "make sure our format isnt just a translation layer but 2// actually a SUPERIOR format on its own if people want to use it natively like ourselves for GENERATED content"). 3// This authors content DIRECTLY into .nmz from raw generated text -- NO lock-in source format involved -- and adds 4// the features that make .nmz superior to EPUB/MOBI/PDF as a native format, especially for generated content: 5// @kind -- the media kind (first-class) 6// @provenance -- FIRST-CLASS generation lineage (generator/method/origin/license). EPUB/MOBI have NO native 7// provenance; for AI/generated content this is the authenticity record. 8// content-addressed CID (sha256, uxf_cid) -- tamper-evident integrity. EPUB/MOBI can't detect a flipped byte. 9// DETERMINISTIC -- same inputs -> byte-identical -> same CID. (zip-based formats vary by timestamp/order.) 10// Also writes reader/<slug>/{book.json,chap0.txt} so native content renders in the SAME zero-JS reader (one pipeline 11// for liberated AND generated). Usage: nx_nmz_author [slug]. expect_exit: 0 license_tier: ORIGINAL 12import "nx_syscalls.nx" 13import "nx_canon_bin.nx" 14import "nx_uxf_cid.nx" 15const K_MAGIC_8192: i64 = 8192 16const K_MAGIC_1024: i64 = 1024 17const K_MAGIC_1048576: i64 = 1048576 18 19func au_slen(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} return n } 20func au_p(s: *u8) -> i64 { sys_write(1, s, au_slen(s)); return 0 } 21func au_pn(v: i64) -> i64 { let b: *u8=sys_mmap(24); var m: i64=v; if m<0{m=0-m}; let t: *u8=sys_mmap(24); var k: i64=0; if m==0{t[0]=48 as u8;k=1}; while m>0{t[k]=(48+(m%10)) as u8;m=m/10;k=k+1}; var i: i64=0; while i<k{b[i]=t[k-1-i];i=i+1}; sys_write(1,b,k); return 0 } 22func ensure_dir(path: *u8) -> i64 { __syscall(258, 0-100, path, 0x1ed, 0, 0, 0); return 0 } 23func cat(d: *u8, o: i64, s: *u8) -> i64 { var i: i64=0; while s[i]!=(0 as u8){d[o+i]=s[i];i=i+1} return o+i } 24func catn(d: *u8, o0: i64, v: i64) -> i64 { var o: i64=o0; var m: i64=v; let t: *u8=sys_mmap(24); var k: i64=0; if m==0{t[0]=48 as u8;k=1}; while m>0{t[k]=(48+(m%10)) as u8;m=m/10;k=k+1}; var i: i64=0; while i<k{d[o+i]=t[k-1-i];i=i+1} return o+k } 25func writef(path: *u8, buf: *u8, n: i64) -> i64 { let fd: i64=sys_openat_wr(path, 0x1a4); if fd<0 {return 0-1} sys_write(fd, buf, n); sys_close(fd); return 0 } 26 27func main(argc: i64, argv: *i64) -> i64 { 28 var slug: *u8 = "nishi_native_demo" as *u8 29 if argc >= 2 { slug = argv[1] as *u8 } 30 31 // ----- the NATIVELY GENERATED content (no lock-in source) ----- 32 let title: *u8 = "The Sovereign Spore" as *u8 33 let author: *u8 = "Nishi Generative Engine" as *u8 34 let body: *u8 = "The spore did not ask permission to germinate.\nIt carried, folded inside its own canonical record, the proof of where it came from and the means to verify that nothing had changed it.\nA reader on the far side of the world opened it and, without trusting any server, recomputed its address from its bytes -- and the address matched.\nThat was the whole trick: the content WAS the name.\nNo lock could be added that the content itself would not betray, because to change the lock was to change the name." as *u8 35 let bodyn: i64 = au_slen(body) 36 // @provenance: the generation lineage -- the first-class native feature lock-in formats lack 37 let prov: *u8 = "{\"generator\":\"nishi-generative-engine\",\"method\":\"native-author\",\"organ\":\"nx_nmz_author/1\",\"origin\":\"original-generated\",\"source\":\"none (not a translation of any lock-in format)\",\"license\":\"sovereign-owned\",\"deterministic\":true}" as *u8 38 let provn: i64 = au_slen(prov) 39 40 // book.json -- the SAME schema the zero-JS reader renders (one pipeline for liberated + generated) 41 let bj: *u8 = sys_mmap(K_MAGIC_8192) 42 var bo: i64 = cat(bj, 0, "{\"title\":\"" as *u8); bo = cat(bj, bo, title) 43 bo = cat(bj, bo, "\",\"author\":\"" as *u8); bo = cat(bj, bo, author) 44 bo = cat(bj, bo, "\",\"format\":\"nishi-native\",\"slug\":\"" as *u8); bo = cat(bj, bo, slug) 45 bo = cat(bj, bo, "\",\"chapters\":[{\"idx\":0,\"title\":\"" as *u8); bo = cat(bj, bo, title) 46 bo = cat(bj, bo, "\",\"file\":\"chap0.txt\",\"chars\":" as *u8); bo = catn(bj, bo, bodyn) 47 bo = cat(bj, bo, ",\"is_nav\":0,\"fn\":[]}],\"nchapters\":1,\"toc\":[{\"depth\":0,\"title\":\"" as *u8); bo = cat(bj, bo, title) 48 bo = cat(bj, bo, "\",\"idx\":0,\"href\":\"\"}],\"ntoc\":1,\"toc_source\":\"native\",\"assets\":[],\"nassets\":0,\"cover\":\"\"}" as *u8) 49 bj[bo] = 0 as u8 50 51 // ----- write reader/<slug>/ so native content RENDERS in the same reader ----- 52 ensure_dir("knowledge/staging/media/reader" as *u8) 53 let dir: *u8 = sys_mmap(512); var dl: i64 = cat(dir, 0, "knowledge/staging/media/reader/" as *u8); dl = cat(dir, dl, slug); dir[dl]=0 as u8 54 ensure_dir(dir) 55 let cp: *u8 = sys_mmap(K_MAGIC_1024); var cl: i64 = cat(cp, 0, dir as *u8); cl = cat(cp, cl, "/chap0.txt" as *u8); cp[cl]=0 as u8; writef(cp, body, bodyn) 56 let jp: *u8 = sys_mmap(K_MAGIC_1024); var jl: i64 = cat(jp, 0, dir as *u8); jl = cat(jp, jl, "/book.json" as *u8); jp[jl]=0 as u8; writef(jp, bj, bo) 57 let pp: *u8 = sys_mmap(K_MAGIC_1024); var pl: i64 = cat(pp, 0, dir as *u8); pl = cat(pp, pl, "/provenance.json" as *u8); pp[pl]=0 as u8; writef(pp, prov, provn) 58 59 // ----- build the .nmz NATIVELY: @kind + @provenance + book.json + chap0.txt ----- 60 let keys: *i64 = sys_mmap(64) as *i64 61 let vals: *i64 = sys_mmap(64) as *i64 62 let vlens: *i64 = sys_mmap(64) as *i64 63 keys[0] = "@kind" as *u8 as i64; vals[0] = "book" as *u8 as i64; vlens[0] = 4 64 keys[1] = "@provenance" as *u8 as i64; vals[1] = prov as i64; vlens[1] = provn 65 keys[2] = "book.json" as *u8 as i64; vals[2] = bj as i64; vlens[2] = bo 66 keys[3] = "chap0.txt" as *u8 as i64; vals[3] = body as i64; vlens[3] = bodyn 67 68 let out: *u8 = sys_mmap(K_MAGIC_1048576) 69 let nbytes: i64 = canon_encode_bin(keys, vals, vlens, 4, out) 70 let cid: *u8 = sys_mmap(96); uxf_cid_profiled(UXF_ARCHIVE, out, nbytes, cid) 71 72 ensure_dir("knowledge/staging/media/nmz" as *u8) 73 let op: *u8 = sys_mmap(K_MAGIC_1024); var ol: i64 = cat(op, 0, "knowledge/staging/media/nmz/" as *u8); ol = cat(op, ol, slug); ol = cat(op, ol, ".nmz" as *u8); op[ol]=0 as u8 74 if writef(op, out, nbytes) != 0 { au_p("NMZ-AUTHOR-FAIL write\n" as *u8); sys_exit(1); return 1 } 75 76 au_p("NMZ-AUTHOR slug=" as *u8); au_p(slug); au_p(" native @kind=book +@provenance bytes=" as *u8); au_pn(nbytes); au_p(" cid=" as *u8); au_p(cid); au_p(" -> " as *u8); au_p(op); au_p("\n" as *u8) 77 au_p("NMZ-AUTHOR-OK\n" as *u8); sys_exit(0); return 0 78}