nx_block_density_cli.nx source
↩ module page · 71 lines · 3368 B
1// nx_block_density_cli.nx -- CLI over nx_block_density (the /compare/webscraping R6 lib): the calibration
2// and inspection surface. `map` prints the per-block table the conf rows were calibrated from; `fit` and
3// `full` print the two texts so a reader can diff what the filter removed; `stats` prints the one-line
4// partition for a page. Reads a local file (a page fetched by nx_https_get or a knowledge/fetched mirror).
5// usage: nx_block_density_cli map|fit|full|stats <html-file>
6// exit: 0 ok | 2 usage | 3 unreadable input
7// license_tier: ORIGINAL No hw writes (Rule 26).
8import "nx_syscalls.nx"
9import "nx_block_density.nx"
10
11func bc_len(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } return n }
12func bc_puts(s: *u8) -> i64 { sys_write(1, s, bc_len(s)); return 0 }
13func bc_num(v: i64) -> i64 {
14 let t: *u8 = sys_mmap(32)
15 let n: i64 = bd_map_num(t, 32, 0, v)
16 sys_write(1, t, n)
17 sys_munmap(t, 32)
18 return 0
19}
20func bc_streq(a: *u8, b: *u8) -> i64 { var i: i64 = 0; while a[i] == b[i] { if a[i] == (0 as u8) { return 1 } i = i + 1 } return 0 }
21
22func main(argc: i64, argv: *i64) -> i64 {
23 if argc < 3 { bc_puts("usage: nx_block_density_cli map|fit|full|stats <html-file>\n" as *u8); return 2 }
24 let verb: *u8 = argv[1] as *u8
25 let path: *u8 = argv[2] as *u8
26 let lenbox: *i64 = sys_mmap(16) as *i64
27 lenbox[0] = 0
28 let src: *u8 = sys_read_file(path, lenbox)
29 if (src as i64) == 0 { bc_puts("UNREADABLE " as *u8); bc_puts(path); bc_puts("\n" as *u8); return 3 }
30 let n: i64 = lenbox[0]
31 // output buffer derived from the input: text never exceeds the HTML, the map is bounded by blocks * one line
32 let cap: i64 = n + 4096 + bd_count_lt(src, n) * 160
33 let out: *u8 = sys_mmap(cap)
34 if bc_streq(verb, "map" as *u8) == 1 {
35 let m: i64 = bd_map(src, n, out, cap)
36 sys_write(1, out, m)
37 return 0
38 }
39 if bc_streq(verb, "full" as *u8) == 1 {
40 let m: i64 = nx_html_to_text(src, n, out, cap)
41 sys_write(1, out, m)
42 bc_puts("\n" as *u8)
43 return 0
44 }
45 if bc_streq(verb, "fit" as *u8) == 1 {
46 let m: i64 = bd_fit_text(src, n, out, cap)
47 sys_write(1, out, m)
48 bc_puts("\n" as *u8)
49 return 0
50 }
51 if bc_streq(verb, "stats" as *u8) == 1 {
52 let fulln: i64 = nx_html_to_text(src, n, out, cap)
53 let fitn: i64 = bd_fit_text(src, n, out, cap)
54 bc_puts("BLOCK-DENSITY html_bytes=" as *u8); bc_num(n)
55 bc_puts(" full_text=" as *u8); bc_num(fulln)
56 bc_puts(" fit_text=" as *u8); bc_num(fitn)
57 bc_puts(" fit_html=" as *u8); bc_num(bd_last_fit_html_g)
58 bc_puts(" blocks=" as *u8); bc_num(bd_last_blocks_g)
59 bc_puts(" kept=" as *u8); bc_num(bd_last_kept_g)
60 bc_puts(" semantic=" as *u8); bc_num(bd_last_semantic_g)
61 bc_puts(" structural=" as *u8); bc_num(bd_last_structural_g)
62 bc_puts(" dropped=" as *u8); bc_num(bd_last_dropped_g)
63 bc_puts(" fallback=" as *u8); bc_num(bd_last_fallback_g)
64 bc_puts(" conf_src=" as *u8); if bd_conf_src_g == 1 { bc_puts("file" as *u8) } else { bc_puts("defaults" as *u8) }
65 bc_puts(" ld_curr_max=" as *u8); bc_num(bd_ld_curr_max_g)
66 bc_puts("\n" as *u8)
67 return 0
68 }
69 bc_puts("usage: nx_block_density_cli map|fit|full|stats <html-file>\n" as *u8)
70 return 2
71}