code wiki / _hdl_build / nx_epub_fixture_build.nx
nx_epub_fixture_build.nx source
↩ module page · 51 lines · 9230 B
1// nx_epub_fixture_build.nx -- build a deterministic, real-format EPUB fixture via the sovereign OPC zip writer
2// (opc_write, STORED + real CRC-32s). The personal NAS library isn't always mounted and a public fetch can fail;
3// this gives the reader rungs (R-TOC nav-doc parse + R-ART artwork extract) a controlled REAL EPUB to verify
4// against end-to-end (rule 7), with KNOWN expected values for the liar-kill gates.
5// Structure exercises every path: manifest item properties="nav" (EPUB3 nav location) + an NCX (EPUB2 fallback) +
6// a nested TOC (Part Two > 2.1 / Chapter Three) + a cover declared BOTH ways (<meta name=cover> AND
7// properties="cover-image") + an inline <img> in ch2 (embedded illustration). Image bytes are distinctive
8// placeholders (extraction/CID is byte-exact regardless; real PNGs come from real books).
9// expect_exit: 0 license_tier: ORIGINAL (prose = public-domain Alice in Wonderland)
10import "nx_syscalls.nx"
11import "nx_opc.nx"
12
13func fb_slen(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} return n }
14func fb_p(s: *u8) -> i64 { sys_write(1, s, fb_slen(s)); return 0 }
15func fb_n(v: i64) -> i64 { let bb: *u8=sys_mmap(28); var m: i64=v; if m<0{m=0-m}; let t: *u8=sys_mmap(28); var k: i64=0; if m==0{t[0]=48 as u8;k=1}; while m>0{t[k]=(48+(m%10)) as u8;m=m/10;k=k+1}; var i: i64=0; while i<k{bb[i]=t[k-1-i];i=i+1}; sys_write(1,bb,k); return 0 }
16func ensure_dir(path: *u8) -> i64 { __syscall(258, 0-100, path, 0x1ed, 0, 0, 0); return 0 }
17
18func main() -> i64 {
19 ensure_dir("knowledge/fixtures" as *u8)
20
21 let names: *i64 = sys_mmap(8 * 16) as *i64
22 let datap: *i64 = sys_mmap(8 * 16) as *i64
23 let lens: *i64 = sys_mmap(8 * 16) as *i64
24
25 let mimetype: *u8 = "application/epub+zip" as *u8
26 let container: *u8 = "<?xml version=\"1.0\"?>\n<container version=\"1.0\" xmlns=\"urn:oasis:names:tc:opendocument:xmlns:container\">\n<rootfiles><rootfile full-path=\"OEBPS/content.opf\" media-type=\"application/oebps-package+xml\"/></rootfiles>\n</container>\n" as *u8
27 let opf: *u8 = "<?xml version=\"1.0\"?>\n<package xmlns=\"http://www.idpf.org/2007/opf\" version=\"3.0\" unique-identifier=\"bookid\">\n<metadata xmlns:dc=\"http://purl.org/dc/elements/1.1/\">\n<dc:title>Alice (Nishi Fixture)</dc:title>\n<dc:creator>Lewis Carroll</dc:creator>\n<dc:identifier id=\"bookid\">nishi-fixture-1</dc:identifier>\n<meta name=\"cover\" content=\"cover-img\"/>\n</metadata>\n<manifest>\n<item id=\"nav\" href=\"nav.xhtml\" media-type=\"application/xhtml+xml\" properties=\"nav\"/>\n<item id=\"ncx\" href=\"toc.ncx\" media-type=\"application/x-dtbncx+xml\"/>\n<item id=\"ch1\" href=\"ch1.xhtml\" media-type=\"application/xhtml+xml\"/>\n<item id=\"ch2\" href=\"ch2.xhtml\" media-type=\"application/xhtml+xml\"/>\n<item id=\"ch3\" href=\"ch3.xhtml\" media-type=\"application/xhtml+xml\"/>\n<item id=\"cover-img\" href=\"cover.png\" media-type=\"image/png\" properties=\"cover-image\"/>\n<item id=\"fig1\" href=\"fig1.png\" media-type=\"image/png\"/>\n</manifest>\n<spine toc=\"ncx\">\n<itemref idref=\"ch1\"/>\n<itemref idref=\"ch2\"/>\n<itemref idref=\"ch3\"/>\n</spine>\n</package>\n" as *u8
28 let nav: *u8 = "<?xml version=\"1.0\"?>\n<html xmlns=\"http://www.w3.org/1999/xhtml\" xmlns:epub=\"http://www.idpf.org/2007/ops\"><head><title>Contents</title></head><body>\n<nav epub:type=\"toc\" id=\"toc\"><h2>Table of Contents</h2>\n<ol>\n<li><a href=\"ch1.xhtml\">Chapter One</a></li>\n<li><a href=\"ch2.xhtml\">Part Two</a>\n<ol>\n<li><a href=\"ch2.xhtml#s1\">2.1 The Pool of Tears</a></li>\n<li><a href=\"ch3.xhtml\">Chapter Three</a></li>\n</ol>\n</li>\n</ol>\n</nav>\n</body></html>\n" as *u8
29 let ncx: *u8 = "<?xml version=\"1.0\"?>\n<ncx xmlns=\"http://www.daisy.org/z3986/2005/ncx/\" version=\"2005-1\"><docTitle><text>Alice (Nishi Fixture)</text></docTitle>\n<navMap>\n<navPoint id=\"np1\" playOrder=\"1\"><navLabel><text>Chapter One</text></navLabel><content src=\"ch1.xhtml\"/></navPoint>\n<navPoint id=\"np2\" playOrder=\"2\"><navLabel><text>Part Two</text></navLabel><content src=\"ch2.xhtml\"/>\n<navPoint id=\"np2a\" playOrder=\"3\"><navLabel><text>2.1 The Pool of Tears</text></navLabel><content src=\"ch2.xhtml#s1\"/></navPoint>\n</navPoint>\n</navMap>\n</ncx>\n" as *u8
30 let ch1: *u8 = "<?xml version=\"1.0\"?>\n<html xmlns=\"http://www.w3.org/1999/xhtml\"><head><title>Chapter One</title></head><body>\n<h1>Chapter One</h1>\n<p>Alice was beginning to get very tired of sitting by her sister on the bank, and of having nothing to do: once or twice she had peeped into the book her sister was reading, but it had no pictures or conversations in it, and what is the use of a book, thought Alice, without pictures or conversations?</p>\n<p>So she was considering in her own mind, as well as she could, for the hot day made her feel very sleepy and stupid, whether the pleasure of making a daisy-chain would be worth the trouble of getting up and picking the daisies, when suddenly a White Rabbit with pink eyes ran close by her.</p>\n<p>There was nothing so very remarkable in that; nor did Alice think it so very much out of the way to hear the Rabbit say to itself, oh dear, oh dear, I shall be late! But when the Rabbit actually took a watch out of its waistcoat-pocket, and looked at it, and then hurried on, Alice started to her feet.</p>\n</body></html>\n" as *u8
31 let ch2: *u8 = "<?xml version=\"1.0\"?>\n<html xmlns=\"http://www.w3.org/1999/xhtml\"><head><title>Part Two</title></head><body>\n<h1>Part Two</h1>\n<p id=\"s1\">In another moment down went Alice after the White Rabbit, never once considering how in the world she was to get out again. The rabbit-hole went straight on like a tunnel for some way, and then dipped suddenly down, so suddenly that Alice had not a moment to think about stopping herself.</p>\n<img src=\"fig1.png\" alt=\"Alice falling down the rabbit hole\"/>\n<p>Either the well was very deep, or she fell very slowly, for she had plenty of time as she went down to look about her, and to wonder what was going to happen next. She tried to look down and make out what she was coming to, but it was too dark to see anything.</p>\n<p>Down, down, down. Would the fall never come to an end? I wonder how many miles I have fallen by this time, she said aloud, for she had learnt several things of this sort in her lessons in the schoolroom.</p>\n</body></html>\n" as *u8
32 let ch3: *u8 = "<?xml version=\"1.0\"?>\n<html xmlns=\"http://www.w3.org/1999/xhtml\" xmlns:epub=\"http://www.idpf.org/2007/ops\"><head><title>Chapter Three</title></head><body>\n<h1>Chapter Three</h1>\n<p>There seemed to be no use in waiting by the little door, so she went back to the table, half hoping she might find another key on it, or at any rate a book of rules for shutting people up like telescopes.<a epub:type=\"noteref\" href=\"#fn1\" id=\"ref-fn1\">1</a></p>\n<p>This time she found a little bottle on it, and round the neck of the bottle was a paper label, with the words DRINK ME beautifully printed on it in large letters. It was all very well to say drink me, but the wise little Alice was not going to do that in a hurry.</p>\n<p>No, I will look first, she said, and see whether it is marked poison or not; for she had read several nice little histories about children who had got burnt, and eaten up by wild beasts, and other unpleasant things.</p>\n<aside epub:type=\"footnote\" id=\"fn1\"><p>A telescope here is the collapsible spyglass kind that folds shut into itself.</p></aside>\n</body></html>\n" as *u8
33 let coverpng: *u8 = "NISHI-FIXTURE-COVER-IMAGE-BYTES-v1-DISTINCTIVE-PLACEHOLDER-FOR-CID-ROUNDTRIP-0123456789abcdef" as *u8
34 let fig1png: *u8 = "NISHI-FIXTURE-FIG1-INLINE-ILLUSTRATION-BYTES-v1-DISTINCTIVE-PLACEHOLDER-fedcba9876543210" as *u8
35
36 names[0] = ("mimetype" as *u8) as i64; datap[0] = mimetype as i64; lens[0] = fb_slen(mimetype)
37 names[1] = ("META-INF/container.xml" as *u8) as i64; datap[1] = container as i64; lens[1] = fb_slen(container)
38 names[2] = ("OEBPS/content.opf" as *u8) as i64; datap[2] = opf as i64; lens[2] = fb_slen(opf)
39 names[3] = ("OEBPS/nav.xhtml" as *u8) as i64; datap[3] = nav as i64; lens[3] = fb_slen(nav)
40 names[4] = ("OEBPS/toc.ncx" as *u8) as i64; datap[4] = ncx as i64; lens[4] = fb_slen(ncx)
41 names[5] = ("OEBPS/ch1.xhtml" as *u8) as i64; datap[5] = ch1 as i64; lens[5] = fb_slen(ch1)
42 names[6] = ("OEBPS/ch2.xhtml" as *u8) as i64; datap[6] = ch2 as i64; lens[6] = fb_slen(ch2)
43 names[7] = ("OEBPS/ch3.xhtml" as *u8) as i64; datap[7] = ch3 as i64; lens[7] = fb_slen(ch3)
44 names[8] = ("OEBPS/cover.png" as *u8) as i64; datap[8] = coverpng as i64; lens[8] = fb_slen(coverpng)
45 names[9] = ("OEBPS/fig1.png" as *u8) as i64; datap[9] = fig1png as i64; lens[9] = fb_slen(fig1png)
46
47 let w: i64 = opc_write("knowledge/fixtures/nishi_fixture.epub" as *u8, names, datap, lens, 10)
48 fb_p("FIXTURE epub bytes=" as *u8); fb_n(w); fb_p(" parts=10 -> knowledge/fixtures/nishi_fixture.epub\n" as *u8)
49 if w <= 0 { fb_p("FIXTURE-FAIL\n" as *u8); sys_exit(1); return 1 }
50 fb_p("FIXTURE-OK\n" as *u8); sys_exit(0); return 0
51}