This page permanently redirects to gemini://nader.pm/my-diy-solution-for-reading-epub-books/.

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

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|| -->|' ~/epub.html 
sed -i "s|||" ~/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|||' ~/epub.html 
sed -i 's|| --> |' ~/epub.html 

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

sed -i "s|||" ~/epub.html 

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

This solution is not perfect for multiple reasons:

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

Proxy Information
Original URL
gemini://nader.pm/my-diy-solution-for-reading-epub-books
Status Code
Success (20)
Meta
text/gemini;lang=en
Capsule Response Time
238.012996 milliseconds
Gemini-to-HTML Time
0.529648 milliseconds

This content has been proxied by September (3851b).