support for images

This commit is contained in:
sejo 2021-12-14 14:09:25 -06:00
parent 28c422f1f3
commit d3245db368
10 changed files with 65 additions and 10 deletions

View File

@ -1,5 +1,5 @@
clean:
rm -r out/
rm -r out/ out.epub
example-gpub:
cd example-gpub; zip -r ../example-book.gpub *; cd -

View File

@ -26,15 +26,36 @@ awk -f g2e.awk example-gpub/
# notes
the converter works with gempub archives that have a flat file structure: all the `.gmi` files, including `index.gmi`, have to be in the same directory. see the provided `example-gpub/`.
## file structure
the converter works with gempub archives that have a flat file structure: all the `.gmi` files, including `index.gmi`, have to be in the same directory.
furthermore, the image assets for the book have to be inside that directory, within a directory called `img/`.
for example, the structure for `example-gpub` is:
```
example-gpub/
metadata.txt
book/
index.gmi
cover.gmi
chapter1.gmi
chapter2.gmi
chapter3.gmi
img/
screencap_uxn-pong.gif
screenshot_uxn-pong-paddle.png
screenshot_uxn-pong-paddles-and-ball.png
screenshot_uxn-pong-paddles.png
```
note that only `.gmi`, `.png`, `.jpg` and `.gif` files are handled.
## raw html
you can insert raw html using the corresponding `.gmo` syntax: the contents of a line that starts with `+` will be inserted as is.
# TODO
* handle non-gmi files in the manifest
* handle links to images
# references
* [EPUB - Wikipedia](https://en.wikipedia.org/wiki/EPUB#Implementation)

View File

@ -2,6 +2,8 @@
hello!
=> img/screencap_uxn-pong.gif animated pong
> not everything
## this is a second-level heading

View File

@ -0,0 +1,5 @@
# an example book
a gpub archive that gets converted to an epub file
=> img/screenshot_uxn-pong-paddles-and-ball.png screenshot of pong

Binary file not shown.

After

Width:  |  Height:  |  Size: 76 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 892 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 963 B

View File

@ -2,6 +2,7 @@
hello!
=> cover.gmi an example book
=> chapter1.gmi chapter 1: an example
=> chapter2.gmi chapter 2: another example
=> chapter3.gmi chapter 3: yet another

32
g2e.awk
View File

@ -35,10 +35,13 @@ BEGIN{
# setup epub
system( "mkdir -p " epubodir "META-INF" )
system( "mkdir -p " epubodir indexdirname )
system( "mkdir -p " epubodir indexdirname "img" )
system( "cp -u " tdir "container.xml " epubodir "META-INF/" )
system( "cp -u " tdir "mimetype " epubodir )
system( "cp -u " tdir "style.css " epubodir )
# copy images
# system( "cp -u " gpubdir indexdirname "img/* " epubodir indexdirname "img/" )
# read templates
templatefiles = "find " tdir " -type f -not -name '.*'"
@ -94,6 +97,21 @@ ARGIND==1{ # skip other lines of the index
# when finished reading the index:
ARGIND==2 && FNR==1{
line = $0
# add images to manifest
while( "find " gpubdir indexdirname "img/ -type f -regextype awk -iregex \".+(png|jpg|gif)$\"" | getline){
path = $0
dest = path; sub(gpubdir, "", dest)
fortoc = dest
system( "cp -u " $0 " " epubodir dest) # copy images
mediatype = fortoc~/gif$/ ? "image/gif" : fortoc~/jpg$/ ? "image/jpeg" : "image/png"
id = fortoc
sub(/^.+\//,"",id)
content = content " <item id=\"" id "\" href=\"" fortoc "\" media-type=\"" mediatype "\"/>\n"
}
$0 = line
# finalize metadata files
content = content " </manifest>\n\n" spinetoc " </spine>\n\n</package>"
printf content > epubodir "content.opf"
@ -175,10 +193,18 @@ sub(/^>[[:space:]]*/,""){
# links?
sub(/^=>[[:space:]]*/,""){
link = $1
if(link~/gmi$/) sub(/gmi$/,"xhtml",link)
text = $2
for(i=3;i<=NF;i++) text = text " " $i
append("<p><a href=\"" link "\">" text "</a></p>")
if(link~/gmi$/){
sub(/gmi$/,"xhtml",link)
append("<p><a href=\"" link "\">" text "</a></p>")
}
else if(link~/(gif|jpg|png)$/){
append("<img src=\"" link "\" alt=\"" text "\" />")
}
else{
append("<p><a href=\"" link "\">" text "</a></p>")
}
next
}