code wiki / _hdl_build / nx_md.nx

nx_md.nx source

↩ module page · 44 lines · 2443 B

1// nx_md.nx -- sovereign Markdown -> HTML viewer CLI. The renderer itself lives in nx_md_lib.nx (rule 15 2// DRY: shared with the NishiOS app layer, which LINKS renderers instead of exec'ing them). Same 3// `html <in> <out> [dlurl]` interface as nx_docx/nx_csv/nx_pdf, so the nx_doc_view dispatcher routes to it 4// by registry with zero special-casing. license_tier: ORIGINAL 5import "nx_syscalls.nx" 6import "nx_itoa_lib.nx" // shared MSB-first emitter (zero-alloc) 7import "nx_md_lib.nx" 8const K_MAGIC_2097168: i64 = 2097168 9const K_MAGIC_2097152: i64 = 2097152 10const K_MAGIC_8388608: i64 = 8388608 11 12func md_slen(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8) { n=n+1 } return n } 13func md_puts(s: *u8) -> i64 { sys_write(1, s, md_slen(s)); return 0 } 14// MIGRATED to the shared emitter (debt 1785563586). The old body mmapped a scratch buffer 15// per call and never freed it. At PAGE granularity that is 4096B leaked PER CALL -- the 16// defect that took 28.5GB of a 36GB host in nx_ts_lumadiff (2MB input, ~3.66M calls). 17// nxi_* is MSB-first, allocates NOTHING, and emits identical bytes including the sign. 18func md_num(v: i64) -> i64 { nxi_out(v); return 0 } 19 20func md_readfile(path: *u8, buf: *u8, cap: i64) -> i64 { 21 let fd: i64=sys_openat_rd(path); if fd<0 { return 0-1 } 22 var tot: i64=0; while tot<cap { let r: i64=sys_read(fd,(buf as i64+tot) as *u8,cap-tot); if r<=0 { break } tot=tot+r } 23 sys_close(fd); return tot 24} 25 26func main(argc: i64, argv: *i64) -> i64 { 27 if argc<4 { md_puts("usage: nx_md html <in.md> <out.html> [download_url]\n" as *u8); sys_exit(2); return 2 } 28 let cmd: *u8=argv[1] as *u8 29 if cmd[0]!=(104 as u8) { md_puts("nx_md: unknown command (html)\n" as *u8); sys_exit(2); return 2 } 30 let inp: *u8=argv[2] as *u8 31 let outp: *u8=argv[3] as *u8 32 var dlu: *u8=0 as *u8 33 if argc>4 { dlu=argv[4] as *u8 } 34 let src: *u8=sys_mmap(K_MAGIC_2097168) 35 let slen: i64=md_readfile(inp, src, K_MAGIC_2097152) 36 if slen<0 { md_puts("MD-HTML-FAIL cannot-read\n" as *u8); sys_exit(1); return 1 } 37 let out: *u8=sys_mmap(K_MAGIC_8388608) 38 let n: i64=md_to_html(src, slen, out, dlu) 39 let fd: i64=sys_openat_wr(outp, 0x1a4) 40 if fd<0 { md_puts("MD-HTML-FAIL cannot-write\n" as *u8); sys_exit(1); return 1 } 41 sys_write(fd, out, n); sys_close(fd) 42 md_puts("MD-HTML-OK path=" as *u8); md_puts(outp); md_puts(" bytes=" as *u8); md_num(n); md_puts("\n" as *u8) 43 sys_exit(0); return 0 44}