generate toc.ncx from index

This commit is contained in:
sejo 2021-12-13 19:34:01 -06:00
parent 73c81f15da
commit d6abdd4173
5 changed files with 49 additions and 31 deletions

View File

@ -7,3 +7,4 @@ published: 2021
publishDate: 2021-12-13
version: 1
description: this is an example book!
id: 123456789X

57
g2e.awk
View File

@ -33,26 +33,30 @@ BEGIN{
system( "cp -ru " epubtdir "META-INF " epubodir )
system( "cp -u " epubtdir "mimetype " epubodir )
content = ""
content = write_template( epubtdir "OEBPS/content-header.opf" )
RS = "\f" # to get the whole file in one getline
getline templates["content-header.opf"] < (epubtdir "OEBPS/content-header.opf")
getline templates["toc-header.ncx"] < (epubtdir "OEBPS/toc-header.ncx")
getline templates["toc-navmap.ncx"] < (epubtdir "OEBPS/toc-navmap.ncx")
RS = "\n" # return to normal
content = write_template( "content-header.opf", meta )
toc = write_template( "toc-header.ncx", meta )
spinetoc = " <spine toc=\"ncx\">\n"
}
# write the template into output, replacing {keys} with contents of meta["keys"]
function write_template( templatepath ){
# write the template into output, replacing {keys} with contents of values["keys"]
function write_template( templatek, values ){
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"
}
line = templates[templatek]
while(match(line,/\{[^{}]+\}/)){ # has {key}
key = substr(line,RSTART+1,RLENGTH-2)
output = output substr(line, 1, RSTART-1) # before {key}
output = output values[key]
line = substr(line, RSTART+RLENGTH)
}
output = output line "\n"
return output
}
@ -60,19 +64,34 @@ function write_template( templatepath ){
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"
name = $3
for(i=4;i<=NF;i++) name = name " " $i
ch["id"] = id
ch["name"] = name
ch["num"]++
content = content " <item id=\"" id "\" href=\"" id ".xhtml\" media-type=\"application/xhtml+xml\"/>\n"
spinetoc = spinetoc " <itemref idref=\"" id "\" />\n"
toc = toc write_template( "toc-navmap.ncx", ch )
}
# content files
# when finished reading the index:
ARGIND==2 && FNR==1{
content = content " </manifest>\n\n" spinetoc " </spine>\n\n</package>"
printf content > epubodir "OEBPS/content.opf"
toc = toc "</ncx>"
printf toc > epubodir "OEBPS/toc.ncx"
}
# content files
ARGIND>1{
print $0
# print $0
}
END{
# for(k in meta) print k, meta[k]
}

View File

@ -4,7 +4,7 @@
<metadata xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:opf="http://www.idpf.org/2007/opf">
<dc:title>{title}</dc:title>
<dc:language>{language}</dc:language>
<dc:identifier id="BookId" opf:scheme="ISBN">123456789X</dc:identifier>
<dc:identifier id="BookId" opf:scheme="ISBN">{id}</dc:identifier>
<dc:creator opf:role="aut">{author}</dc:creator>
<dc:description>{description}</dc:description>
<dc:date>{publishDate}</dc:date>

View File

@ -8,25 +8,17 @@
<!-- 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:uid" content="{id}"/> <!-- 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>
<text>{title}</text>
</docTitle>
<docAuthor>
<text>Austen, Jane</text>
<text>{author}</text>
</docAuthor>
<navMap>
<navPoint class="chapter" id="chapter1" playOrder="1">
<navLabel><text>Chapter 1</text></navLabel>
<content src="chapter1.xhtml"/>
</navPoint>
</navMap>
</ncx>

View File

@ -0,0 +1,6 @@
<navMap>
<navPoint class="chapter" id="{id}" playOrder="{num}">
<navLabel><text>{name}</text></navLabel>
<content src="{id}.xhtml"/>
</navPoint>
</navMap>