-- Leo's gemini proxy

-- Connecting to nader.pm:1965...

-- Connected

-- Sending request

-- Meta line: 20 text/gemini;lang=en

My DIY solution for reading EPUB books


Nader K. Rad, 2021-02-21



I've tried every EPUB reader I could find, and I don't like any of them. What I expect is


to be able to style it the way I want, and

have a continuous text without pages (unlike, e.g., MuPDF), and

have a simple user interface without extra elements.


So, I came up with a solution, and I'm happy with it. So, I'm going to explain it because you may like it as well, or you may be able to think of something that is ten times better than this and you may want to let me know about it!


First, I convert the book to a temporary HTML file:


pandoc --self-contained "$@" -o ~/epub.html

I've used $@ instead of $1 because some file names have spaces in them.


First, I tried to keep the embedded styles of books and add my own style to them, but then I decided to remove the whole original style and only add my own:


sed -i 's|<style>|<!-- |' ~/epub.html
sed -i 's|</style>| -->|' ~/epub.html
sed -i "s|</head>|<style>$style</style></head>|" ~/epub.html

It's not perfect because some ebooks are so badly formatted that, for example, use a paragraph with a special class to format code instead of using the code tag. I use a monospace font globally, so something like that is not a big deal, but generally, I have to cop with the other issues similar to this.


Initially, I had my own style in the SED command above, but then I moved it to a separate file to keep it clean. That file's content is read as follows, and that's the $style variable in the previous command.


style=$(tr '\n' ' ' < /path/epub.css)

I've used the TR command to remove the new lines from the CSS file, because I couldn't get the SED command to work when the replacement string had new lines.


In case you're wondering, this is the CSS file:


body,
html {
  background-color: #282828;
  color: #aaa;
}
body {
  max-width: 650px;
  margin: 0 auto;
  font-family: "Ubuntu Mono";
  font-size: 20px;
  line-height: 150%;
}
a {
  text-decoration: none;
  color: #ff793f;
}
h1,
h2,
h3,
h4,
h5,
h6 {
  color: #e1b12c;
}
img {
  max-width: 100%;
  height: auto;
}
code {
  background-color: #000;
  border-radius: 4px;
  color: #00b894;
}
pre {
  background-color: #000;
  padding: 0 10px;
}

So, at this point, the temporary file is ready, and all I have to do is open it in a browser window. I didn't want to use Lynx because of the images in books, and my qutebrowser hasn't been working for a while for some unknown reason, and surf doesn't like me very much, so I've created a profile in Firefox that doesn't have any buttons, address bar, status bar, and so on; just a simple window. I open the ebook in that:


firefox -P "epub" -new-window ~/epub.html

And voilĂ ! There's a simple, relatively well-formatted ebook in a clean window that doesn't have any extra elements. I've changed the default EPUB viewer to this application, and I'm very happy with it. This is the whole script:


#!/bin/bash

pandoc --self-contained "$@" -o ~/epub.html

sed -i 's|<html.*>|<html>|' ~/epub.html
sed -i 's|<style>|<!-- |' ~/epub.html
sed -i 's|</style>| --> |' ~/epub.html

style=$(tr '\n' ' ' < /path/epub.css)

sed -i "s|</head>|<style>$style</style></head>|" ~/epub.html

firefox -P "epub" -new-window ~/epub.html

This solution is not perfect for multiple reasons:


It's not fast; it takes 1 to 5 seconds to open a book. (Maybe I'll cache the results in case the same book is opened again.)

The table of contents is not as accessible as it could be; I have to go back to the top of the file, and then scroll down to reach the TOC.

Some of the crazy styles don't exist, which may be a problem in some books.


Overall, I prefer this to every EPUB reader I've tried.


-- Response ended

-- Page fetched on Fri Apr 19 18:11:36 2024