code wiki / _hdl_build / nx_html_head_fd.nx

nx_html_head_fd.nx source

↩ module page · 46 lines · 2210 B

1// nx_html_head_fd.nx -- the fd-STREAMING half of the canonical accessible head (seq702/seq703). 2// 3// WHY A SECOND FILE: emitters come in TWO shapes, discovered while migrating them. 4// (a) BUFFER-ACCUMULATING -- pd_cat / shf_cat / fb_puts / wc_cat build into (out, offset) 5// and write once at the end. Those use nx_html_head.nx, which stays deliberately 6// DEPENDENCY-FREE (no imports at all) so anything can adopt it. 7// (b) fd-STREAMING -- hw(fd, s) writes each literal straight out via sys_write. The 16 8// nx_wasm_*_html.nx GAME packagers are all this shape. 9// Writing to an fd needs sys_write, so the fd half lives here rather than contaminating the 10// dependency-free core. Every emitter already imports nx_syscalls, so this costs adopters nothing. 11// 12// USAGE -- mirrors the buffer API exactly: 13// nxh_fd_head_open(fd, "My Title" as *u8) // doctype+lang+charset+viewport+title 14// hw(fd, "<style>...</style>" as *u8) // emitter keeps its own CSS, unchanged 15// nxh_fd_body_open(fd) // </head><body><main> 16// ... page content ... 17// nxh_fd_close(fd) // </main></body></html> 18// 19// Guarantees the three checks no stylesheet can supply: lang= on <html>, meta viewport, 20// and a <main> landmark. For the games the viewport line is the load-bearing one -- they 21// pair a fixed-px canvas with no viewport, which is why they do not scale on a phone. 22 23import "nx_syscalls.nx" 24 25// write one NUL-terminated literal to fd (same contract as the hw() every emitter defines) 26func nxh_fd_lit(fd: i64, s: *u8) -> i64 { 27 var n: i64 = 0 28 while s[n] != (0 as u8) { n = n + 1 } 29 sys_write(fd, s, n) 30 return 0 31} 32 33func nxh_fd_head_open(fd: i64, title: *u8) -> i64 { 34 nxh_fd_lit(fd, "<!doctype html><html lang=\"en\"><head><meta charset=\"utf-8\"><meta name=\"viewport\" content=\"width=device-width,initial-scale=1\"><title>" as *u8) 35 nxh_fd_lit(fd, title) 36 nxh_fd_lit(fd, "</title>" as *u8) 37 return 0 38} 39 40func nxh_fd_body_open(fd: i64) -> i64 { 41 return nxh_fd_lit(fd, "</head><body><main>" as *u8) 42} 43 44func nxh_fd_close(fd: i64) -> i64 { 45 return nxh_fd_lit(fd, "</main></body></html>" as *u8) 46}