retroforth/generate-epub.retro
crc b7a7de19b4 epub: other than the missing ncx file, it now passes validation
FossilOrigin-Name: 2f66a6dd264cd2fbbf7c6dbd4d4228027899629e6a19faa61bf08d2dfd8b96f3
2020-09-25 17:05:29 +00:00

153 lines
3.9 KiB
Text
Executable file

#!/usr/bin/env retro
This tool is intended to be used to create an epub version of the
standard documentation.
An epub is just a .zip file containing a bunch of XHTML files and
a few files containing metadata.
Our structure will look like:
Book Root Directory
|-- META-INF directory
| `-- container.xml
|-- mimetype
|-- content.opf
|-- toc.ncx
|-- stylesheet.css
|-- cover.xhtml
|-- toc.xhtml
|-- chapters/...
Begin by defining some helper functions for writing to a
file.
~~~
'tools/book-chapters.retro include
'FID var
:file:s:put [ @FID file:write ] s:for-each ;
:file:nl ASCII:CR @FID file:write ;
:unix:mkdir 'mkdir_-p_%s s:format unix:system ;
~~~
Create the directories needed.
~~~
'STEP:_Create_directories s:put nl
{ 'chapters
'chapters/building
'chapters/general
'chapters/internals
'chapters/tech-notes
'chapters/techniques
'chapters/toolchain
'chapters/toolchain/info
'chapters/toolchain/man
'epub
'epub/META-INF } [ dup tab s:put nl unix:mkdir ] a:for-each
~~~
Then create the `mimetype` file. This is supposed to be written
without a trailing newline.
~~~
'STEP:_Generate_`mimetype` s:put nl
'application/epub+zip 'epub/mimetype file:spew
~~~
Then create `META-INF/container.xml`.
~~~
'STEP:_Generate_`container.xml` s:put nl
'epub/META-INF/container.xml file:open-for-writing !FID
'<?xml_version="1.0"?> file:s:put file:nl
'<container_version="1.0"_ file:s:put
'xmlns="urn:oasis:names:tc:opendocument:xmlns:container"> file:s:put file:nl
'<rootfiles> file:s:put file:nl
'<rootfile_full-path="content.opf"_ file:s:put
'media-type="application/oebps-package+xml"/> file:s:put file:nl
'</rootfiles> file:s:put file:nl
'</container> file:s:put file:nl
@FID file:close
~~~
Create `content.opf`. This will need to lest all of the files
that are used in the book.
Manifest:
'____<item_href="chap01.xhtml"_id="chap01_page"_media-type="application/xhtml+xml"/> file:s:put file:nl
Spine:
'____<itemref_idref="chap01_page"_linear="yes"/> file:s:put file:nl
~~~
'STEP:_Generate_`content.opf` s:put nl
'epub/content.opf file:open-for-writing !FID
'<?xml_version="1.0"_encoding="utf-8"_standalone="yes"?> file:s:put file:nl
'<package_xmlns="http://www.idpf.org/2007/opf"_unique-identifier="BookID"_version="2.0"> file:s:put file:nl
'__<metadata_xmlns:dc="http://purl.org/dc/elements/1.1/"_xmlns:opf="http://www.idpf.org/2007/opf"> file:s:put file:nl
'____<dc:title>RETRO_Forth_:_User_Manual</dc:title> file:s:put file:nl
'____<dc:identifier_id="BookID"_opf:scheme="CustomID">works.forth.retro-manual</dc:identifier> file:s:put file:nl
'____<dc:language>en</dc:language> file:s:put file:nl
'__</metadata> file:s:put file:nl
'__<manifest> file:s:put file:nl
#0 TOC [ over swap '____<item_href="chapters/%s.html"_id="CHAP%n"_media-type="application/xhtml+xml"/> s:format file:s:put file:nl n:inc ] a:for-each
drop
'__</manifest> file:s:put file:nl
'__<spine_toc="ncx"> file:s:put file:nl
#0 TOC [ drop dup '____<itemref_idref="CHAP%n"_linear="yes"/> s:format file:s:put file:nl n:inc ] a:for-each drop
'__</spine> file:s:put file:nl
~~~
'__<guide> file:s:put file:nl
'__</guide> file:s:put file:nl
~~~
'</package> file:s:put file:nl
@FID file:close
~~~
# Generate the Chapters
~~~
'STEP:_Convert_chapters_to_XHTML s:put nl
'retro_tools/book-chapters-to-xhtml.retro unix:system
~~~
# Assemble the pieces
~~~
'STEP:_Relocate_files s:put nl
'epub unix:chdir
'cp_-r_../chapters_. unix:system
~~~
# Zip Everything
~~~
'STEP:_Create_epub s:put nl
'zip_-0Xqv_../book.epub_mimetype unix:system
'zip_-Xr9Dqv_../book.epub_content.opf unix:system
'zip_-Xr9Dqv_../book.epub_META-INF unix:system
'zip_-Xr9Dqv_../book.epub_chapters unix:system
'.. unix:chdir
~~~
# Clean Up
Cleanup is the final step. Remove the temporary epub directory
~~~
'STEP:_Cleanup s:put nl
'cp_-r_chapters_~/atua unix:system
'rm_-rf_epub_chapters unix:system
~~~