Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

epub-rb

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

epub-rb

  • 0.0.2
  • Rubygems
  • Socket score

Version published
Maintainers
1
Created
Source

epub-rb

Tests Gem Gem MIT License

Create epub files using Ruby.

Installation

gem install epub-rb

Or add the following line to your project's Gemfile:

gem "epub-rb"

Usage

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.

[!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:

navigation = Epub::Navigation.extract_html(
  Dir["./book/**/*.html"],
  root_dir: "./book"
)

File.open("toc.html", "w") do |file|
  file << <<~HTML
    <?xml version='1.0' encoding='utf-8'?>
    <html xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" xml:lang="en" lang="en">
      <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
        <title>Table of Contents</title>
      </head>
      <body>
        #{navigation}
      </body>
    </html>

  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

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. 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.

FAQs

Package last updated on 26 Jan 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc