https://github.com/fnando/epub
Create epub files using Ruby (EPUB 3.3).
https://github.com/fnando/epub
epub epub3 ruby
Last synced: 3 months ago
JSON representation
Create epub files using Ruby (EPUB 3.3).
- Host: GitHub
- URL: https://github.com/fnando/epub
- Owner: fnando
- License: mit
- Created: 2024-01-25T23:49:46.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2025-12-12T19:07:44.000Z (6 months ago)
- Last Synced: 2026-02-18T06:42:59.823Z (4 months ago)
- Topics: epub, epub3, ruby
- Language: Ruby
- Homepage:
- Size: 120 KB
- Stars: 5
- Watchers: 1
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: CONTRIBUTING.md
- Funding: .github/FUNDING.yml
- License: LICENSE.md
- Code of conduct: CODE_OF_CONDUCT.md
- Codeowners: .github/CODEOWNERS
Awesome Lists containing this project
README
# epub-rb
[](https://github.com/fnando/epub)
[](https://rubygems.org/gems/epub-rb)
[](https://rubygems.org/gems/epub-rb)
[](https://tldrlegal.com/license/mit-license)
Create epub files using Ruby.
## Installation
```bash
gem install epub-rb
```
Or add the following line to your project's Gemfile:
```ruby
gem "epub-rb"
```
## Usage
```ruby
require "epub"
epub = Epub.new(
root_dir: "./book",
title: "Hello World",
subtitle: "A Complete Beginner’s Guide to Ruby Programming",
creators: ["John Doe"],
publisher: "epub-rb",
date: "2024-01-24",
id: "d05f92ea-62f7-44d6-bb48-d94c11e660db",
copyright: "Copyright 2024 by John Doe",
identifiers: ["915869090000000000DD", "urn:isbn:9780000000001"],
contributors: ["Jane Doe"],
files: [
"./book/book.css",
"./book/cover.png",
"./book/cover.html",
"./book/toc.html",
"./book/ch01.html",
"./book/ch02.html",
"./book/images/terminal.svg"
]
)
epub.save("hello-word.epub")
```
The epub file is compliant with the EPUB 3.3 specification. You can check it by
using [epubcheck](https://www.w3.org/publishing/epubcheck/).
> [!NOTE]
>
> epub-rb makes a few assumptions that you need to follow.
>
> 1. You need to have a file name `toc.{xhtml,html}`. This file must be
> compliant with the EPUB 3 spec.
> 2. You'll also need a cover image named as `cover.{png,jpg,gif}`.
> 3. You'll also need a companion file called `cover.{xhtml,html}`.
### Generating the Table of Contents (navigation file)
You can use the methods `Epub::Navigation.extract(files, root_dir:)` and
`Epub::Navigation.extract_html(files, root_dir:)` to generate the `toc.html`
file. A simple way would be using something like this:
```ruby
navigation = Epub::Navigation.extract_html(
Dir["./book/**/*.html"],
root_dir: "./book"
)
File.open("toc.html", "w") do |file|
file << <<~HTML
Table of Contents
#{navigation}
HTML
end
```
> [!NOTE]
>
> Notice that the order of `.html`/`.xhtml` files is important. You need to sort
> files how you'd like them to show up. If you add `toc.html` lastly, then it'll
> show up at the end of the ebook.
>
> Consider adding files in this order: `cover.html`, `toc.html`, all other html
> files your epub will have, then other assets (images, css, javascript, etc).
## Maintainer
- [Nando Vieira](https://github.com/fnando)
## Contributors
- https://github.com/fnando/epub/contributors
## Contributing
For more details about how to contribute, please read
https://github.com/fnando/epub/blob/main/CONTRIBUTING.md.
## License
The gem is available as open source under the terms of the
[MIT License](https://opensource.org/licenses/MIT). A copy of the license can be
found at https://github.com/fnando/epub/blob/main/LICENSE.md.
## Code of Conduct
Everyone interacting in the epub-rb project's codebases, issue trackers, chat
rooms and mailing lists is expected to follow the
[code of conduct](https://github.com/fnando/epub/blob/main/CODE_OF_CONDUCT.md).