I decided to start contributing to Redox OS. While searching for resources to study, I came across the Rustonomicon. Having dismissed it in the past — "I certainly won't be writing unsafe Rust any time soon" — I now felt the time has come.

There was only one problem: I don't enjoy reading on my computer screen. Like, at all. But I do enjoy1 reading on my dated e-reader. As it turns out, you can make mdBook builds spit out .epub files!

Here is how you do it.

Install requirements

  1. Install mdbook and mdbook-pandoc
    $ cargo install mdbook
    $ cargo install mdbook-pandoc --locked
  2. Install pandoc through your package manager:
    $ pacman -S pandoc-cli  # I use arch btw

Build the EPUB file

  1. Clone the Rustonomicon source

    $ git clone https://github.com/rust-lang/nomicon.git
  2. Put custom CSS rules into a new epub.css file

    On my dated e-reader, long lines in code blocks were cut off. These CSS rules make code blocks wrap long lines instead.

    pre.sourceCode > code.sourceCode {
      white-space: pre-wrap !important;
    }
    
    pre.sourceCode > code.sourceCode > span {
      display: block !important;
      white-space: pre-wrap !important;
      max-width: 100% !important;
      overflow: visible !important;
      word-wrap: break-word !important;
      word-break: break-all !important;
    }

    Turns out .epub files are just "special" ZIP archives2. So a simple

    $ unzip book/pandoc/epub/rustonomicon.epub -d /tmp/rustonomicon-epub

    will let you start digging into its contents — like when you want to find existing CSS rules to overwrite them with your own ones.

  3. Add an EPUB profile to book.toml

    [output.pandoc.profile.epub]
    to = "epub"
    output-file = "rustonomicon.epub"
    css = ["epub.css"]
  4. Build the book

    $ mdbook build

Huzzah! Now you have a book/pandoc/epub/rustonomicon.epub file that you can copy to your dated e-reader.

:wq

  1. "Enjoy" may be too strong of a word. Physical books all the way, but since not everything gets printed, I learned to love my e-reader. ↩

  2. https://www.w3.org/TR/epub-overview-33/ ↩