2020-09-24 20:43:59 +02:00
|
|
|
#!/usr/bin/env retro
|
|
|
|
|
2020-09-16 20:16:39 +02:00
|
|
|
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
|
|
|
|
|-- mimetype
|
2020-09-25 21:57:20 +02:00
|
|
|
|-- META-INF directory
|
|
|
|
| `-- container.xml
|
2020-09-16 20:16:39 +02:00
|
|
|
|-- content.opf
|
|
|
|
|-- toc.ncx
|
2020-09-24 21:57:52 +02:00
|
|
|
|-- chapters/...
|
2020-09-16 20:16:39 +02:00
|
|
|
|
|
|
|
|
|
|
|
Begin by defining some helper functions for writing to a
|
|
|
|
file.
|
|
|
|
|
|
|
|
~~~
|
2020-09-24 21:48:48 +02:00
|
|
|
'tools/book-chapters.retro include
|
|
|
|
|
2020-09-16 20:16:39 +02:00
|
|
|
'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.
|
|
|
|
|
|
|
|
~~~
|
2020-09-25 21:57:20 +02:00
|
|
|
'Create_directories s:put nl
|
2020-09-25 17:45:41 +02:00
|
|
|
{ '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
|
2020-09-16 20:16:39 +02:00
|
|
|
~~~
|
|
|
|
|
|
|
|
Then create the `mimetype` file. This is supposed to be written
|
|
|
|
without a trailing newline.
|
|
|
|
|
|
|
|
~~~
|
2020-09-25 21:57:20 +02:00
|
|
|
'Generate_`mimetype` s:put nl
|
2020-09-16 20:16:39 +02:00
|
|
|
'application/epub+zip 'epub/mimetype file:spew
|
|
|
|
~~~
|
|
|
|
|
|
|
|
Then create `META-INF/container.xml`.
|
|
|
|
|
|
|
|
~~~
|
2020-09-25 21:57:20 +02:00
|
|
|
'Generate_`container.xml` s:put nl
|
2020-09-16 20:16:39 +02:00
|
|
|
'epub/META-INF/container.xml file:open-for-writing !FID
|
|
|
|
|
2020-09-25 21:57:20 +02:00
|
|
|
{ '<?xml_version="1.0"?>
|
|
|
|
'<container_version="1.0"_xmlns="urn:oasis:names:tc:opendocument:xmlns:container">
|
|
|
|
'<rootfiles>
|
|
|
|
'____<rootfile_full-path="content.opf"_media-type="application/oebps-package+xml"/>
|
|
|
|
'</rootfiles>
|
|
|
|
'</container>
|
|
|
|
} [ file:s:put file:nl ] a:for-each
|
2020-09-16 20:16:39 +02:00
|
|
|
|
|
|
|
@FID file:close
|
|
|
|
~~~
|
|
|
|
|
2020-09-25 21:57:20 +02:00
|
|
|
# Create the TOC NCX file
|
2020-09-16 20:16:39 +02:00
|
|
|
|
2020-09-25 21:57:20 +02:00
|
|
|
~~~
|
|
|
|
'Generate_`TOC.ncx` s:put nl
|
|
|
|
'epub/TOC.ncx file:open-for-writing !FID
|
|
|
|
|
|
|
|
{ '<?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="works.forth.retro-manual"/>
|
|
|
|
'____<meta_name="dtb:depth"_content="1"/>
|
|
|
|
'</head>
|
|
|
|
'<docTitle>
|
|
|
|
'____<text></text>
|
|
|
|
'</docTitle>
|
|
|
|
'<navMap>
|
|
|
|
} [ file:s:put file:nl ] a:for-each
|
2020-09-24 21:48:48 +02:00
|
|
|
|
2020-09-25 21:57:20 +02:00
|
|
|
#0 TOC [ over dup dup '____<navPoint_id="CHAP%n"_playOrder="%n"><navLabel><text>Chapter_%n</text></navLabel><content_src="chapters/%s.html"/></navPoint> s:format file:s:put file:nl n:inc ] a:for-each drop
|
2020-09-16 20:16:39 +02:00
|
|
|
|
2020-09-25 21:57:20 +02:00
|
|
|
{ '</navMap>
|
|
|
|
'</ncx>
|
|
|
|
} [ file:s:put file:nl ] a:for-each
|
2020-09-16 20:16:39 +02:00
|
|
|
|
2020-09-25 21:57:20 +02:00
|
|
|
@FID file:close
|
|
|
|
~~~
|
|
|
|
|
|
|
|
|
|
|
|
<navPoint id="hello" playOrder="1">
|
|
|
|
<navLabel><text>cover</text></navLabel>
|
|
|
|
<content src="1_hello.xhtml" />
|
|
|
|
</navPoint>
|
|
|
|
|
|
|
|
|
|
|
|
# Create the CONTENT.OPF
|
|
|
|
|
|
|
|
Create `content.opf`. This will need to lest all of the files
|
|
|
|
that are used in the book.
|
2020-09-16 20:16:39 +02:00
|
|
|
|
2020-09-24 21:48:48 +02:00
|
|
|
~~~
|
2020-09-25 21:57:20 +02:00
|
|
|
'Generate_`content.opf` s:put nl
|
2020-09-24 21:48:48 +02:00
|
|
|
'epub/content.opf file:open-for-writing !FID
|
2020-09-16 20:16:39 +02:00
|
|
|
|
2020-09-24 21:48:48 +02:00
|
|
|
'<?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
|
2020-09-25 19:05:29 +02:00
|
|
|
'____<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
|
2020-09-29 15:39:36 +02:00
|
|
|
'____<dc:creator_opf:role="aut">Charles_Childers</dc:creator> file:s:put file:nl
|
2020-09-24 21:48:48 +02:00
|
|
|
'__</metadata> file:s:put file:nl
|
2020-09-16 20:16:39 +02:00
|
|
|
|
2020-09-24 21:48:48 +02:00
|
|
|
'__<manifest> file:s:put file:nl
|
2020-09-25 21:57:20 +02:00
|
|
|
'____<item_id="ncx"_href="TOC.ncx"_media-type="application/x-dtbncx+xml"/> file:s:put file:nl
|
2020-09-24 21:57:52 +02:00
|
|
|
#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
|
2020-09-24 21:48:48 +02:00
|
|
|
drop
|
|
|
|
'__</manifest> file:s:put file:nl
|
2020-09-16 20:16:39 +02:00
|
|
|
|
2020-09-24 21:48:48 +02:00
|
|
|
'__<spine_toc="ncx"> file:s:put file:nl
|
2020-09-24 21:57:52 +02:00
|
|
|
#0 TOC [ drop dup '____<itemref_idref="CHAP%n"_linear="yes"/> s:format file:s:put file:nl n:inc ] a:for-each drop
|
2020-09-24 21:48:48 +02:00
|
|
|
'__</spine> file:s:put file:nl
|
2020-09-25 19:05:29 +02:00
|
|
|
~~~
|
2020-09-16 20:16:39 +02:00
|
|
|
|
2020-09-24 21:48:48 +02:00
|
|
|
'__<guide> file:s:put file:nl
|
|
|
|
'__</guide> file:s:put file:nl
|
|
|
|
|
2020-09-25 19:05:29 +02:00
|
|
|
~~~
|
2020-09-24 21:48:48 +02:00
|
|
|
'</package> file:s:put file:nl
|
|
|
|
@FID file:close
|
|
|
|
~~~
|
2020-09-24 20:43:59 +02:00
|
|
|
|
|
|
|
# Generate the Chapters
|
|
|
|
|
|
|
|
~~~
|
2020-09-25 21:57:20 +02:00
|
|
|
'Convert_chapters_to_XHTML s:put nl
|
2020-09-24 20:43:59 +02:00
|
|
|
'retro_tools/book-chapters-to-xhtml.retro unix:system
|
|
|
|
~~~
|
|
|
|
|
2020-09-24 21:48:48 +02:00
|
|
|
# Assemble the pieces
|
2020-09-16 20:16:39 +02:00
|
|
|
|
|
|
|
~~~
|
2020-09-25 21:57:20 +02:00
|
|
|
'Relocate_files s:put nl
|
2020-09-16 20:16:39 +02:00
|
|
|
'epub unix:chdir
|
2020-09-24 21:48:48 +02:00
|
|
|
'cp_-r_../chapters_. unix:system
|
|
|
|
~~~
|
|
|
|
|
|
|
|
# Zip Everything
|
|
|
|
|
|
|
|
~~~
|
2020-09-25 21:57:20 +02:00
|
|
|
'Create_epub s:put nl
|
2020-09-25 17:45:41 +02:00
|
|
|
'zip_-0Xqv_../book.epub_mimetype unix:system
|
|
|
|
'zip_-Xr9Dqv_../book.epub_content.opf unix:system
|
2020-09-25 21:57:20 +02:00
|
|
|
'zip_-Xr9Dqv_../book.epub_TOC.ncx unix:system
|
2020-09-25 17:45:41 +02:00
|
|
|
'zip_-Xr9Dqv_../book.epub_META-INF unix:system
|
|
|
|
'zip_-Xr9Dqv_../book.epub_chapters unix:system
|
2020-09-16 20:16:39 +02:00
|
|
|
'.. unix:chdir
|
|
|
|
~~~
|
|
|
|
|
2020-09-24 21:48:48 +02:00
|
|
|
# Clean Up
|
2020-09-16 20:16:39 +02:00
|
|
|
|
|
|
|
Cleanup is the final step. Remove the temporary epub directory
|
|
|
|
|
|
|
|
~~~
|
2020-09-25 21:57:20 +02:00
|
|
|
'Cleanup s:put nl
|
2020-09-25 19:05:29 +02:00
|
|
|
'cp_-r_chapters_~/atua unix:system
|
2020-09-25 17:45:41 +02:00
|
|
|
'rm_-rf_epub_chapters unix:system
|
2020-09-16 20:16:39 +02:00
|
|
|
~~~
|