Creating an EPUB file with the ZIP module

I'm using the built-in ZIP module to try and build a minimal EPUB file like so:

Rebol [
    Title: "Package The Test Book"
    Date: 5-Feb-2019
    Author: "Christopher Ross-Gill"
    Rights: http://opensource.org/licenses/Apache-2.0
    Notes: [
        https://ebooks.stackexchange.com/a/1184
        {Source of Minimal eBook}
    ]
]

zip %Test.epub [
    %mimetype {application/epub+zip}

    %META-INF/container.xml {
        <?xml version="1.0"?>
        <container version="1.0" xmlns="urn:oasis:names:tc:opendocument:xmlns:container">
            <rootfiles>
                <rootfile full-path="OEBPS/content.opf" media-type="application/oebps-package+xml"/>
            </rootfiles>
        </container>
    }

    %OEBPS/content.opf {
        <?xml version="1.0" encoding="UTF-8" ?>
        <package xmlns="http://www.idpf.org/2007/opf" xmlns:dc="http://purl.org/dc/elements/1.1/" unique-identifier="db-id" version="3.0">

        <metadata>
            <dc:title id="t1">Title</dc:title>
            <dc:identifier id="db-id">isbn</dc:identifier>
            <meta property="dcterms:modified">2014-03-27T09:14:09Z</meta>
            <dc:language>en</dc:language>
        </metadata>

        <manifest>
            <item id="toc" properties="nav" href="toc.xhtml" media-type="application/xhtml+xml" />
            <item id="ncx" href="toc.ncx" media-type="application/x-dtbncx+xml" />
            <item id="template_css" href="template.css" media-type="text/css" />
            <item id="hello" href="1_hello.xhtml" media-type="application/xhtml+xml" />
        </manifest>

        <spine toc="ncx">
            <itemref idref="hello" />
        </spine>

        </package>
    }

    %OEBPS/toc.xhtml {
        <?xml version="1.0" encoding="utf-8"?>
        <html xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops">
        <head>
        <title>toc.xhtml</title>
        <link href="template.css" rel="stylesheet" type="text/css" />
        </head>

        <body>

            <nav id="toc" epub:type="toc">
                <h1 class="frontmatter">Table of Contents</h1>
                <ol class="contents">
                       <li><a href="1_hello.xhtml">Hello</a></li>
                </ol>

            </nav>

        </body>

        </html>
    }

    %OEBPS/1_hello.xhtml {
        <?xml version="1.0" encoding="utf-8"?>
        <html xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops">
        <head>
        <title>1_hello.xhtml</title>
        <link href="template.css" rel="stylesheet" type="text/css" />
        </head>

        <body>

            <h1>Hello World!</h1>

        </body>
        </html>
    }

    %OEBPS/toc.ncx {
        <?xml version="1.0" encoding="UTF-8" ?>
        <ncx version="2005-1" xml:lang="en" xmlns="http://www.daisy.org/z3986/2005/ncx/">

        <head>
            <meta name="dtb:uid" content="isbn"/>
            <meta name="dtb:depth" content="1"/>
        </head>

        <docTitle>
            <text></text>
        </docTitle>

        <navMap>
            <navPoint id="hello" playOrder="1">
                <navLabel><text>cover</text></navLabel>
                <content src="1_hello.xhtml" />
            </navPoint>
        </navMap>

        </ncx>
    }

    %OEBPS/template.css {
        h1 {
          text-align: center;
        }
    }
]

Caveats

Currently it doesn't validate.

$ epubcheck Test.epub
FATAL(PKG-008): Test.epub/META-INF/container.xml(-1,-1): Unable to read file 'META-INF/container.xml'.
ERROR(RSC-003): Test.epub/META-INF/container.xml(-1,-1): No rootfile tag with media type 'application/oebps-package+xml' was found in the container.

Check finished with errors
Messages: 1 fatal / 1 errors / 0 warnings / 0 info

epubcheck completed

Nor does the archive unpack as a .zip on MacOS Finder.

1 Like

Was the result of a simple bug in the call to the zlib wrapper that DEFLATE and INFLATE were doing. Fixed!

1 Like

I will point out to anyone reading this thread that the EPUB maker is demonstrated in this video:

It would be neat if such a thing could be turned into a drag & drop app powered by the WebAssembly build, that ran on the user's machine. The privacy aspect of not uploading your files to a server could be touted as a feature--and it would be using the user's own machine's CPU and memory to do the work.

(Hint, hint... looking to see more Wasm Rebol apps that are written up and kept in working order via headless testing on Travis, as the REPL now is... my offer is to keep some number of apps up and running if they are set up at a fix point of functionality with a test demonstrating that function.)

2 Likes