build content.opf from index gmi files

This commit is contained in:
sejo 2021-12-13 19:07:14 -06:00
parent 56d84833f6
commit 73c81f15da
5 changed files with 81 additions and 5 deletions

View File

@ -2,3 +2,5 @@ clean:
rm example-book.gpub
gpub:
cd example-gpub; zip -r ../example-book.gpub *; cd -
example:
awk -f g2e.awk example-gpub/

View File

@ -4,4 +4,6 @@ gpubVersion: 1.0.0
index: book/index.gmi
language: en
published: 2021
publishDate: 2021-12-13
version: 1
description: this is an example book!

43
g2e.awk
View File

@ -1,6 +1,8 @@
# awk -f g2e.awk example-gpub/
BEGIN{
gpubdir = ARGV[1]
epubtdir = "template-epub/"
epubodir = "out/"
metadatafile = gpubdir "metadata.txt"
# read metadata file fields
@ -25,12 +27,47 @@ BEGIN{
while( getline < (gpubdir meta["index"]) ){
if($1~"=>") ARGV[ARGC++] = gpubdir indexdirname $2
}
}
# index file
ARGIND==1{
# setup epub
system( "mkdir -p " epubodir "OEBPS" )
system( "cp -ru " epubtdir "META-INF " epubodir )
system( "cp -u " epubtdir "mimetype " epubodir )
content = ""
content = write_template( epubtdir "OEBPS/content-header.opf" )
spinetoc = " <spine toc=\"ncx\">\n"
}
# write the template into output, replacing {keys} with contents of meta["keys"]
function write_template( templatepath ){
output = ""
while( getline < templatepath ){
if(match($0,/\{[^{}]+\}/)){ # has {key}
key = substr($0,RSTART+1,RLENGTH-2)
output = output substr($0, 1, RSTART-1) # before {key}
output = output meta[key]
output = output substr($0, RSTART+RLENGTH) "\n"
}
else{ # normal line
output = output $0 "\n"
}
}
return output
}
# index file links
ARGIND==1 && /^=>/{
id = $2
sub(/.gmi$/,"",id)
content = content " <item id=\""id"\" href=\""id".xhtml\" media-type=\"application/xhtml+xml\"/>\n"
spinetoc = spinetoc " <itemref idref=\""id"\" />\n"
}
# content files
ARGIND==2 && FNR==1{
content = content " </manifest>\n\n" spinetoc " </spine>\n\n</package>"
printf content > epubodir "OEBPS/content.opf"
}
ARGIND>1{
print $0
}

View File

@ -6,7 +6,10 @@
<dc:language>{language}</dc:language>
<dc:identifier id="BookId" opf:scheme="ISBN">123456789X</dc:identifier>
<dc:creator opf:role="aut">{author}</dc:creator>
<dc:description>{description}</dc:description>
<dc:date>{publishDate}</dc:date>
<dc:description>{description}</dc:description>
<dc:date>{publishDate}</dc:date>
</metadata>
<manifest>
<item id="stylesheet" href="style.css" media-type="text/css"/>
<item id="ncx" href="toc.ncx" media-type="application/x-dtbncx+xml"/>

View File

@ -0,0 +1,32 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE ncx PUBLIC "-//NISO//DTD ncx 2005-1//EN"
"http://www.daisy.org/z3986/2005/ncx-2005-1.dtd">
<ncx version="2005-1" xml:lang="en" xmlns="http://www.daisy.org/z3986/2005/ncx/">
<head>
<!-- The following four metadata items are required for all NCX documents,
including those that conform to the relaxed constraints of OPS 2.0 -->
<meta name="dtb:uid" content="123456789X"/> <!-- same as in .opf -->
<meta name="dtb:depth" content="1"/> <!-- 1 or higher -->
<meta name="dtb:totalPageCount" content="0"/> <!-- must be 0 -->
<meta name="dtb:maxPageNumber" content="0"/> <!-- must be 0 -->
</head>
<docTitle>
<text>Pride and Prejudice</text>
</docTitle>
<docAuthor>
<text>Austen, Jane</text>
</docAuthor>
<navMap>
<navPoint class="chapter" id="chapter1" playOrder="1">
<navLabel><text>Chapter 1</text></navLabel>
<content src="chapter1.xhtml"/>
</navPoint>
</navMap>
</ncx>