scripts/book: minimal ebook reader

This commit is contained in:
Michele Guerini Rocco 2020-11-18 00:50:21 +01:00
parent d6a74394fd
commit cfa3c06dbb
Signed by: rnhmjoj
GPG Key ID: BFBAF4C975F76450

27
scripts/book Executable file
View File

@ -0,0 +1,27 @@
#!/bin/sh
# create temporary dir...
title=$(basename "$1")
temp="/tmp/book-$(echo "$title" | tr -- '[A-Z] ' '[a-z]-')"
mkdir "$temp"
# ...and delete it on exit
trap 'rm -rf "$temp"' EXIT INT HUP
# extract book to it
unzip -q "$1" -d "$temp"
# locate the path of the manifest
content_file=$temp/$(sed -n 's/.*full-path="\([^"]*\)".*/\1/p' "$temp/META-INF/container.xml")
content_dir="$(dirname "$content_file")"
# get the list of chapter files
filenames=$(sed -n '/media-type="application/s/.*href="\([^"]*\)".*/'"\1/p" "$content_file")
filepaths=$(printf "$content_dir/%s " $filenames)
# strip filepaths from links
fix_links=$(printf "s/%s//g;" $filenames)
# set title to basename
fix_title="s/<title>[^<]*/<title>$title/"
# join all the files and open in w3m
sed -e $fix_links -e "$fix_title" $filepaths > "$temp/book.html"
w3m "$temp/book.html"