header parsing

This commit is contained in:
sejo 2021-12-13 21:54:19 -06:00
parent 90f46b545f
commit 383aa09ba0
4 changed files with 77 additions and 10 deletions

View File

@ -1,3 +1,23 @@
# chapter 1: an example
hello!
## this is a second-level heading
yay!
### and this is the third level
here we have a list:
* an item
* another one
* and yet another one
and some code:
```
awk -f g2e.awk example-gpub/
```
so meta!

54
g2e.awk
View File

@ -1,15 +1,19 @@
# g2e - an opinionated gempub to epub converter written in awk
# THIS IS CHAOTIC SOFTWARE BEWARE <//xj-ix.luxe/wiki/chaotic-software/>
# usage:
# awk -f g2e.awk example-gpub/
# where example-gpub/ is a directory containing an uncompressed gempub file
BEGIN{
gpubdir = ARGV[1]
tdir = "templates/" # templates dir
epubtdir = "template-epub/"
epubodir = "out/"
metadatafile = gpubdir "metadata.txt"
# read metadata file fields
while( getline < metadatafile ){
key = $1
sub(":","",key)
key = $1; sub(":","",key)
value = $2
for(i=3;i<=NF;i++) value = value " " $i
@ -34,16 +38,16 @@ BEGIN{
system( "mkdir -p " epubodir "META-INF" )
system( "cp -u " tdir "container.xml " epubodir "META-INF/" )
system( "cp -u " tdir "mimetype " epubodir )
system( "cp -u " tdir "style.css " epubodir "OEBPS" )
# read templates
templatefiles ="find " tdir " -type f -not -name '.*'"
templatefiles = "find " tdir " -type f -not -name '.*'"
while( (templatefiles | getline )>0 ){
tpath = $0
tkey = tpath
sub(tdir, "", tkey)
tkey = tpath; sub(tdir, "", tkey)
RS = "\f" # to get the whole file in one getline
getline templates[tkey] < tpath
getline templates[tkey] < tpath # read template
RS = "\n"
}
@ -93,12 +97,42 @@ ARGIND==2 && FNR==1{
}
# content files
ARGIND>1{
# print $0
function finishfile(){
# finish writing the previous content file
out = out "\t</main>\n </body>\n</html>"
printf out > nameout
}
function append(line){
out = out "\t\t" line "\n"
}
ARGIND>2 && FNR==1{
finishfile()
}
# setup the writing for this content file
ARGIND>1 && FNR==1 {
id=FILENAME
match(id,/[^/]+.gmi$/)
filenamestart = RSTART
match(id,/.gmi$/)
name = substr(id,filenamestart,RSTART-filenamestart)
nameout = epubodir "OEBPS/" name ".xhtml"
sub(/#{1,3}\s+/,"", $0)
m["title"] = $0
out = write_template("header.xhtml",m)
next
}
ARGIND>1 && match($0,/^#{1,3}+/){
sub(/^#{1,3}[[:space:]]+/,"",$0)
append( "<h" RLENGTH ">" $0 "</h" RLENGTH ">")
next
}
END{
finishfile()
}

13
templates/header.xhtml Normal file
View File

@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="application/xhtml+xml; charset=utf-8" />
<title>{title}<title>
<link rel="stylesheet" href="style.css" type="text/css" />
</head>
<body>
<header>
<h1>{title}</h1>
<header>
<main>

0
templates/style.css Normal file
View File