code wiki / _hdl_build / nx_docx_html_gate.nx
nx_docx_html_gate.nx source
↩ module page · 132 lines · 6981 B
1// nx_docx_html_gate.nx -- gate for the .docx -> HTML PREVIEW renderer (nx_docx html). Browsers can't render a
2// raw .docx (they download it); this proves nx_docx turns OOXML into a clean, sovereign, 0-JS HTML page the Nishi
3// browser shows INLINE -- headings, bold/italic runs, tables, and a "Download .docx" interop link. Evidence: it
4// writes a rich .docx (writerich), renders it (html), and greps the produced HTML for real structural markup.
5// Rows: 1 render-ok 2 valid-html 3 heading 4 bold+italic 5 table+cell 6 download-link 7 sovereign-0-JS 8 neg-missing[neg]
6// license_tier: ORIGINAL
7import "nx_syscalls.nx"
8
9func hg_slen(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8) { n=n+1 } return n }
10func hg_p(s: *u8) -> i64 { sys_write(1, s, hg_slen(s)); return 0 }
11
12func hg_cat(dst: *u8, off: i64, s: *u8) -> i64 { var i: i64=0; while s[i]!=(0 as u8) { dst[off+i]=s[i]; i=i+1 } return off+i }
13func hg_catn(dst: *u8, off: i64, v: i64) -> i64 {
14 var o: i64=off; var m: i64=v; if m<0 { dst[o]=45 as u8; o=o+1; m=0-m }
15 let t: *u8=sys_mmap(28); var k: i64=0; if m==0 { t[0]=48 as u8; k=1 }
16 while m>0 { t[k]=(48+(m%10)) as u8; m=m/10; k=k+1 }
17 var i: i64=0; while i<k { dst[o+i]=t[k-1-i]; i=i+1 } return o+k
18}
19
20func hg_write(path: *u8, content: *u8) -> i64 {
21 let fd: i64=sys_openat_wr(path, 0x1a4); if fd<0 { return 0-1 }
22 sys_write(fd, content, hg_slen(content)); sys_close(fd); return 0
23}
24
25func hg_readall(path: *u8, szout: *i64) -> *u8 {
26 let fd: i64=sys_openat_rd(path); if fd<0 { szout[0]=0-1; return 0 as *u8 }
27 let buf: *u8=sys_mmap(4194304); var got: i64=0; var n: i64=1
28 while n>0 { n=sys_read(fd,(buf as i64+got) as *u8,131072); if n>0 { got=got+n } }
29 sys_close(fd); szout[0]=got; return buf
30}
31
32func hg_runv(elf: *u8, args: *i64, outpath: *u8) -> i64 {
33 let pid: i64=sys_fork()
34 if pid==0 {
35 if (outpath as i64)!=0 { let ofd: i64=sys_openat_wr(outpath,0x1a4); if ofd>=0 { sys_dup3(ofd,1,0); sys_dup3(ofd,2,0) } }
36 let argv: *i64=sys_mmap(128) as *i64
37 argv[0]=elf as i64; var i: i64=0; var go: i64=1
38 while go==1 { if args[i]==0 { go=0 } else { argv[i+1]=args[i]; i=i+1 } }
39 argv[i+1]=0
40 let envp: *i64=sys_mmap(16) as *i64; envp[0]=0
41 sys_execve(elf, argv, envp); sys_exit(127)
42 }
43 let st: *i64=sys_mmap(16) as *i64; sys_wait4(pid, st, 0)
44 let sig: i64=st[0]&0x7f; if sig!=0 { return 128+sig }
45 return (st[0]>>8)&0xff
46}
47
48func hg_has(path: *u8, needle: *u8) -> i64 {
49 let szp: *i64=sys_mmap(16) as *i64; let b: *u8=hg_readall(path, szp); let sz: i64=szp[0]
50 let n: i64=hg_slen(needle); if sz<n { return 0 }
51 var i: i64=0
52 while i+n<=sz { var ok: i64=1; var j: i64=0; while j<n { if b[i+j]!=needle[j] { ok=0; j=n } else { j=j+1 } } if ok==1 { return 1 } i=i+1 }
53 return 0
54}
55
56func hg_args4(a1: *u8, a2: *u8, a3: *u8, a4: *u8) -> *i64 {
57 let a: *i64=sys_mmap(64) as *i64; a[0]=a1 as i64; a[1]=a2 as i64; a[2]=a3 as i64; a[3]=a4 as i64; a[4]=0; return a
58}
59
60func hg_row(name: *u8, pass: i64) -> i64 {
61 hg_p("ROW " as *u8); hg_p(name); if pass==1 { hg_p(" PASS\n" as *u8) } else { hg_p(" FAIL\n" as *u8) } return pass
62}
63
64func main(argc: i64, argv: *i64) -> i64 {
65 hg_p("=== DOCX -> HTML PREVIEW GATE: inline document viewer (headings/bold/italic/tables) + download link ===\n" as *u8)
66 let docx: *u8="/tmp/nx_docx.sov.elf" as *u8
67 let pr: i64=sys_openat_rd(docx)
68 if pr>=0 { sys_close(pr) } else {
69 hg_p(" instrument missing -> rebuilding via durable runner\n" as *u8)
70 hg_runv("_offc/nx_sov_build_run.elf" as *u8, hg_args4("nx_docx" as *u8, "--build-only" as *u8, 0 as *u8, 0 as *u8), "/tmp/dh_rebuild.out" as *u8)
71 }
72
73 hg_write("/tmp/dh_spec.txt" as *u8, "H Venue Booking Agreement\nB Parties\nI confidential draft\nP Nishi Family and Lakeside Pavilion\nT Field|Value\nT Date|2026-08-15\nT Cost|USD 350.00\nP Signed on the day of booking.\n" as *u8)
74
75 var pass: i64=0; var r: i64=0
76
77 // build the rich .docx to preview
78 hg_runv(docx, hg_args4("writerich" as *u8, "/tmp/dh.docx" as *u8, "/tmp/dh_spec.txt" as *u8, 0 as *u8), "/tmp/dh_w.txt" as *u8)
79
80 // row 1: render to HTML (with a download link)
81 let rc1: i64=hg_runv(docx, hg_args4("html" as *u8, "/tmp/dh.docx" as *u8, "/tmp/dh.html" as *u8, "/vizsla_agreement.docx" as *u8), "/tmp/dh_r1.txt" as *u8)
82 r=0; if rc1==0 { if hg_has("/tmp/dh_r1.txt" as *u8, "DOCX-HTML-OK path=/tmp/dh.html bytes=" as *u8)==1 { r=1 } }
83 pass=pass+hg_row("html-render-ok" as *u8, r)
84
85 // row 2: valid HTML document
86 r=0; if hg_has("/tmp/dh.html" as *u8, "<!DOCTYPE html>" as *u8)==1 { if hg_has("/tmp/dh.html" as *u8, "</html>" as *u8)==1 { r=1 } }
87 pass=pass+hg_row("valid-html" as *u8, r)
88
89 // row 3: heading rendered as <h2> (b + sz=32 -> heading)
90 r=hg_has("/tmp/dh.html" as *u8, "<h2>Venue Booking Agreement</h2>" as *u8)
91 pass=pass+hg_row("heading-h2" as *u8, r)
92
93 // row 4: bold + italic runs
94 r=0; if hg_has("/tmp/dh.html" as *u8, "<b>Parties</b>" as *u8)==1 { if hg_has("/tmp/dh.html" as *u8, "<i>confidential draft</i>" as *u8)==1 { r=1 } }
95 pass=pass+hg_row("bold-italic-runs" as *u8, r)
96
97 // row 5: table + cell content
98 r=0; if hg_has("/tmp/dh.html" as *u8, "<table>" as *u8)==1 { if hg_has("/tmp/dh.html" as *u8, "<tr>" as *u8)==1 { if hg_has("/tmp/dh.html" as *u8, "<td>USD 350.00</td>" as *u8)==1 { r=1 } } }
99 pass=pass+hg_row("table-cells" as *u8, r)
100
101 // row 6: interop download link
102 r=0; if hg_has("/tmp/dh.html" as *u8, "href=\"/vizsla_agreement.docx\"" as *u8)==1 { if hg_has("/tmp/dh.html" as *u8, "Download .docx" as *u8)==1 { r=1 } }
103 pass=pass+hg_row("download-link" as *u8, r)
104
105 // row 7: sovereign 0-JS (no script, no external http load)
106 r=1; if hg_has("/tmp/dh.html" as *u8, "<script" as *u8)==1 { r=0 } if hg_has("/tmp/dh.html" as *u8, "http" as *u8)==1 { r=0 }
107 pass=pass+hg_row("sovereign-0js" as *u8, r)
108
109 // row 8 (neg): missing .docx must loud-fail
110 let rc8: i64=hg_runv(docx, hg_args4("html" as *u8, "/tmp/dh_nope.docx" as *u8, "/tmp/dh_x.html" as *u8, 0 as *u8), "/tmp/dh_r8.txt" as *u8)
111 r=0; if rc8==1 { r=1 }
112 pass=pass+hg_row("loud-fail-missing" as *u8, r)
113
114 let permil: i64=(pass*1000)/8
115 let logfd: i64=sys_openat_append("knowledge/status/vizsla_gate.log" as *u8, 0x1a4)
116 var fdi: i64=0
117 while fdi<2 {
118 var fd: i64=1; if fdi==1 { fd=logfd }
119 if fd>0 {
120 let line: *u8=sys_mmap(256); var o: i64=0
121 o=hg_cat(line,o,"DOCX-HTML-GATE epoch=" as *u8); o=hg_catn(line,o,sys_now_realtime_sec())
122 o=hg_cat(line,o," rows=8 pass=" as *u8); o=hg_catn(line,o,pass)
123 o=hg_cat(line,o," permil=" as *u8); o=hg_catn(line,o,permil)
124 if pass==8 { o=hg_cat(line,o," verdict=GREEN\n" as *u8) } else { o=hg_cat(line,o," verdict=RED\n" as *u8) }
125 sys_write(fd, line, o)
126 }
127 fdi=fdi+1
128 }
129 if logfd>0 { sys_close(logfd) }
130 if pass==8 { return 0 }
131 return 1
132}