From d6abdd4173298ae17a8fb12ff0afa05d7ce6dcfe Mon Sep 17 00:00:00 2001 From: sejo Date: Mon, 13 Dec 2021 19:34:01 -0600 Subject: [PATCH] generate toc.ncx from index --- example-gpub/metadata.txt | 1 + g2e.awk | 57 ++++++++++++------- template-epub/OEBPS/content-header.opf | 2 +- .../OEBPS/{toc.ncx => toc-header.ncx} | 14 +---- template-epub/OEBPS/toc-navmap.ncx | 6 ++ 5 files changed, 49 insertions(+), 31 deletions(-) rename template-epub/OEBPS/{toc.ncx => toc-header.ncx} (66%) create mode 100644 template-epub/OEBPS/toc-navmap.ncx diff --git a/example-gpub/metadata.txt b/example-gpub/metadata.txt index c3e7803..f0d1c45 100644 --- a/example-gpub/metadata.txt +++ b/example-gpub/metadata.txt @@ -7,3 +7,4 @@ published: 2021 publishDate: 2021-12-13 version: 1 description: this is an example book! +id: 123456789X diff --git a/g2e.awk b/g2e.awk index 4589339..ad127b9 100644 --- a/g2e.awk +++ b/g2e.awk @@ -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 = " \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 " \n" - spinetoc = spinetoc " \n" + name = $3 + for(i=4;i<=NF;i++) name = name " " $i + + ch["id"] = id + ch["name"] = name + ch["num"]++ + + content = content " \n" + spinetoc = spinetoc " \n" + + toc = toc write_template( "toc-navmap.ncx", ch ) + } -# content files +# when finished reading the index: ARGIND==2 && FNR==1{ content = content " \n\n" spinetoc " \n\n" printf content > epubodir "OEBPS/content.opf" + + toc = toc "" + printf toc > epubodir "OEBPS/toc.ncx" } + +# content files ARGIND>1{ - print $0 +# print $0 } END{ -# for(k in meta) print k, meta[k] + } diff --git a/template-epub/OEBPS/content-header.opf b/template-epub/OEBPS/content-header.opf index da00585..93b2339 100644 --- a/template-epub/OEBPS/content-header.opf +++ b/template-epub/OEBPS/content-header.opf @@ -4,7 +4,7 @@ {title} {language} - 123456789X + {id} {author} {description} {publishDate} diff --git a/template-epub/OEBPS/toc.ncx b/template-epub/OEBPS/toc-header.ncx similarity index 66% rename from template-epub/OEBPS/toc.ncx rename to template-epub/OEBPS/toc-header.ncx index ca1522a..636d3b2 100644 --- a/template-epub/OEBPS/toc.ncx +++ b/template-epub/OEBPS/toc-header.ncx @@ -8,25 +8,17 @@ - + - Pride and Prejudice + {title} - Austen, Jane + {author} - - - Chapter 1 - - - - - \ No newline at end of file diff --git a/template-epub/OEBPS/toc-navmap.ncx b/template-epub/OEBPS/toc-navmap.ncx new file mode 100644 index 0000000..c5f439e --- /dev/null +++ b/template-epub/OEBPS/toc-navmap.ncx @@ -0,0 +1,6 @@ + + + {name} + + +