code wiki / _hdl_build / nx_wiki_build.nx
nx_wiki_build.nx source
↩ module page · 104 lines · 5304 B
1// nx_wiki_build.nx -- the AUTONOMOUS wiki builder. Enumerates EVERY workstream record in the memory
2// dir (getdents64) and stamps an S-class anatomy page for each via wp_emit_page, then writes the index
3// that links them all. Because pages are named by record name and [[wikilinks]] resolve to <name>.html,
4// the whole set becomes ONE cross-linked wiki of all our work -- no TSV, no hand-curation. Re-run any
5// time (or on a pulse) and the wiki regenerates from the live records. Sovereign. license_tier: ORIGINAL
6import "nx_syscalls.nx"
7import "nx_wiki_page_lib.nx"
8const WB_MAGIC_131072: i64 = 131072
9const WB_MAGIC_4194304: i64 = 4194304
10const WB_MAGIC_8388608: i64 = 8388608
11const WB_MAGIC_2048: i64 = 2048
12
13const WB_MEMDIR: *u8 = "/mnt/c/Users/elder/.claude/projects/C--Users-elder/memory"
14const WB_OUTDIR: *u8 = "/mnt/c/Users/elder/nishi-core/nxc2/knowledge/wiki_pages"
15
16func wb_is_md(name: *u8) -> i64 {
17 let n: i64 = wp_slen(name)
18 if n < 4 { return 0 }
19 if name[n - 3] == (46 as u8) { if name[n - 2] == (109 as u8) { if name[n - 1] == (100 as u8) { return 1 } } }
20 return 0
21}
22// append `name` minus its last 3 chars (".md") to dst at off; return new off.
23func wb_cat_base(dst: *u8, off: i64, name: *u8) -> i64 {
24 let n: i64 = wp_slen(name); var o: i64 = off; var i: i64 = 0
25 while i < n - 3 { dst[o] = name[i]; o = o + 1; i = i + 1 }
26 return o
27}
28func wb_writefile(path: *u8, buf: *u8, len: i64) -> i64 {
29 let fd: i64 = sys_openat_wr(path, 420)
30 if fd < 0 { return 0 - 1 }
31 sys_write(fd, buf, len); sys_close(fd)
32 return 0
33}
34func wb_pn(buf: *u8, off: i64, v: i64) -> i64 {
35 var o: i64 = off; var m: i64 = v
36 if m == 0 { buf[o] = 48 as u8; return o + 1 }
37 let t: *u8 = sys_mmap(32); var k: i64 = 0
38 while m > 0 { t[k] = ((48 + (m % 10)) as u8); m = m / 10; k = k + 1 }
39 while k > 0 { buf[o] = t[k - 1]; o = o + 1; k = k - 1 }
40 return o
41}
42
43func main(argc: i64, argv: *i64) -> i64 {
44 sys_mkdir(WB_OUTDIR, 511)
45 let fd: i64 = sys_openat_rd(WB_MEMDIR)
46 if fd < 0 { sys_write(2, "WB: memdir open failed\n" as *u8, 23); sys_exit(1); return 1 }
47 let gbuf: *u8 = sys_mmap(WB_MAGIC_131072)
48 let rec: *u8 = sys_mmap(WB_MAGIC_4194304)
49 let out: *u8 = sys_mmap(WB_MAGIC_8388608)
50 let path: *u8 = sys_mmap(WB_MAGIC_2048)
51 let opath: *u8 = sys_mmap(WB_MAGIC_2048)
52 let index: *u8 = sys_mmap(WB_MAGIC_4194304)
53 var ix: i64 = 0
54 ix = skin_open(index, ix, "The Wiki" as *u8, "home" as *u8)
55 ix = sk_cat(index, ix, "<h1>The Wiki — all our work</h1>\n<p class=\"sub\">Every workstream, auto-generated from our live records (no TSV). Pages cross-link via <code>[[wikilinks]]</code>. Regenerated by <code>nx_wiki_build</code>.</p>\n" as *u8)
56 ix = sk_cat(index, ix, "<h2 id=\"all\">All workstreams</h2>\n<ul class=\"feed\" style=\"max-height:none;columns:2;column-gap:26px\">\n" as *u8)
57
58 var count: i64 = 0
59 var nread: i64 = __syscall(217, fd, gbuf, WB_MAGIC_131072, 0, 0, 0)
60 while nread > 0 {
61 var off: i64 = 0
62 while off < nread {
63 let reclen: i64 = (gbuf[off + 16] as i64) | ((gbuf[off + 17] as i64) << 8)
64 if reclen <= 0 { off = nread } else {
65 let name: *u8 = ((gbuf as i64) + off + 19) as *u8
66 if wb_is_md(name) == 1 {
67 // path = memdir/name
68 var po: i64 = sk_cat(path, 0, WB_MEMDIR); path[po] = 47 as u8; po = po + 1
69 var ni: i64 = 0; while name[ni] != (0 as u8) { path[po] = name[ni]; po = po + 1; ni = ni + 1 }
70 path[po] = 0 as u8
71 let slen: i64 = wp_slurp(path, rec, WB_MAGIC_4194304)
72 if slen > 0 {
73 let hl: i64 = wp_emit_page(out, rec, slen)
74 if hl > 0 {
75 // opath = outdir/<base>.html
76 var oo: i64 = sk_cat(opath, 0, WB_OUTDIR); opath[oo] = 47 as u8; oo = oo + 1
77 oo = wb_cat_base(opath, oo, name); oo = sk_cat(opath, oo, ".html" as *u8); opath[oo] = 0 as u8
78 wb_writefile(opath, out, hl)
79 // index entry
80 ix = sk_cat(index, ix, "<li><a href=\"" as *u8); ix = wb_cat_base(index, ix, name)
81 ix = sk_cat(index, ix, ".html\">" as *u8); ix = wb_cat_base(index, ix, name); ix = sk_cat(index, ix, "</a></li>\n" as *u8)
82 count = count + 1
83 }
84 }
85 }
86 off = off + reclen
87 }
88 }
89 nread = __syscall(217, fd, gbuf, WB_MAGIC_131072, 0, 0, 0)
90 }
91 sys_close(fd)
92 ix = sk_cat(index, ix, "</ul>\n" as *u8)
93 ix = skin_close(index, ix, sys_now_realtime_sec())
94 // write index.html
95 var io: i64 = sk_cat(opath, 0, WB_OUTDIR); opath[io] = 47 as u8; io = io + 1
96 io = sk_cat(opath, io, "index.html" as *u8); opath[io] = 0 as u8
97 wb_writefile(opath, index, ix)
98
99 let r: *u8 = sys_mmap(256); var ro: i64 = 0
100 ro = sk_cat(r, ro, "WIKI-BUILD-OK pages=" as *u8); ro = wb_pn(r, ro, count)
101 ro = sk_cat(r, ro, " -> " as *u8); ro = sk_cat(r, ro, WB_OUTDIR); ro = sk_cat(r, ro, "/index.html\n" as *u8)
102 sys_write(1, r, ro)
103 sys_exit(0); return 0
104}