BUPE (BUPE v0.6.3)
View SourceBUPE is an Elixir ePub generator and parser, it supports EPUB v2 and v3.
Installation
First, add :bupe to your list of dependencies in mix.exs:
def deps do
[
{:bupe, "~> 0.6"}
]
endTo find out the latest release available on Hex, you can run mix hex.info bupe
in your shell, or by going to the bupe page on Hex.pm
Then, update your dependencies:
mix deps.get
Usage
Builder
If you want to create an EPUB file you can do the following:
pages = "~/book/*.xhtml" |> Path.expand() |> Path.wildcard()
config = BUPE.Config.new(%{
title: "Sample",
language: "en",
creator: "John Doe",
publisher: "Sample",
pages: pages
})
BUPE.build(config, "sample.epub")
# {:ok, '/Users/dev/sample.epub'}If you prefer, you can build the EPUB document in memory doing the following:
BUPE.build(config, "sample.epub", [:memory])If you want more control over the pages configuration, instead of
passing a list of strings, you can provide a list of BUPE.Item struct like this:
pages = [%BUPE.Item{href: "/Users/dev/book/bacon.xhtml", description: "Ode to Bacon"}]The given description will be used in the Table of Contents of EPUB
document, otherwise BUPE will provide a default description based on the file
name.
If your page include JavaScript, is recommended that you use the properties
field from BUPE.Item like this:
pages = [%BUPE.Item{href: "/Users/dev/book/bacon.xhtml", description: "Ode to Bacon", properties: "scripted"}]Keep in mind that if you put the scripted property on a page that does not
have any JavaScript, you will get warnings from validation tools such as
EPUBCheck.
See BUPE.build/3, BUPE.Config, and BUPE.Item for more details.
Using the builder via command line
You can build EPUB documents using the command line as follows:
- Install
BUPEas an escript:
mix escript.install hex bupe
- Then you are ready to use it in your projects:
bupe "EPUB_TITLE" -p egg.xhtml -p bacon.xhtml -l path/to/logo.png
For more details about using the command line tool, review the usage guide:
bupe --help
Parser
If you want to parse an EPUB file you can do the following:
BUPE.parse("Elixir.epub")See BUPE.parse/1 for more details.
Summary
Functions
EPUB builder
Options
:memory- Instead of a file, it will produce a tuple{file_name, binary()}. The binary is a full zip archive with header and can be extracted with, for example,:zip.unzip/2.
Example
iex(1)> files = Enum.map(~w(bacon.xhtml egg.xhtml ham.xhtml), &Path.join("/Users/dev/book", &1))
["/Users/dev/book/bacon.xhtml", "/Users/dev/book/egg.xhtml", "/Users/dev/book/ham.xhtml"]
iex(2)> get_id = &Path.basename(&1, ".xhtml")
iex(3)> pages = Enum.map(files, fn file ->
...(3)> %BUPE.Item{href: file, id: get_id.(file), description: file |> get_id.() |> String.capitalize()}
...(3)> end)
iex(4)> config = %{
...(4)> title: "Sample",
...(4)> language: "en",
...(4)> creator: "John Doe",
...(4)> publisher: "Sample",
...(4)> date: "2016-06-23T06:00:00Z",
...(4)> unique_identifier: "EXAMPLE",
...(4)> identifier: "http://example.com/book/jdoe/1",
...(4)> pages: pages
...(4)> }
iex(5)> BUPE.Config.new(config)Once you have the %Bupe.Config{} struct ready, you can execute BUPE.build/3, e.g., BUPE.build(config, "example.epub")
An EPUB 3 conforming parser. This implementation should support also EPUB 2 too.
Example
BUPE.parse("sample.epub")
@spec version() :: String.t()
Returns the BUPE version (used in templates)