Skip to content

Instantly share code, notes, and snippets.

@skoji
Created February 21, 2012 21:15
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save skoji/1878995 to your computer and use it in GitHub Desktop.
Save skoji/1878995 to your computer and use it in GitHub Desktop.
GEPUB::Builder API example
# -*- coding: utf-8 -*-
# GEPUB::Builder example.
require 'ruby gem'
require 'gepub'
builder = GEPUB::Builder.new {
# In the root block, you can define metadata.
# You can define title, creator(s), contributor(s), publisher(s), date, unique_identifier, identifier, language.
# Title can be specified with title, subtitle, collection, short_title, expanded_title, edition.
# You can also define description, format, relation, right, source, subject, type.
# You can 'refine' last-defined metadata by refiner/attributes methods
# Refiner methods contains : file_as, alt
language 'ja'
title 'タイトル'
alt 'en' => 'main title'
file_as 'main title'
subtitle 'サブタイトル'
alt 'en' => 'subtitle'
# collection title and position in the collection:
collection 'gepub sample book series', 2
#specifying creator
creator 'author1','aut'
alt 'ja' =>'日本語名' ,'en' =>'english name for author1'
id 'the_first_author'
#specifying multiple creator
creators 'author1', 'author2', ['editor1', 'edt']
contributor 'contributor'
contributors 'contributor1', 'contributor2'
# easy way to write alt {'ja' =>'日本語 for contributor1'}, {'ja' => '日本語 for contributor2'}
alts 'ja' => ['日本語 for contributor1','日本語 for contributor2']
publisher '出版社'
alt 'en' => 'ThePublisher'
date '2012-02-21T00:00:00Z'
unique_identifier 'the_unique_id_in_uuid', 'uuid'
identifier 'http://other_id','url'
identifier 'http://another_id','url'
# You can add optional file using optional_file.
optional_file 'META-INF/com.apple.ibooks.display-options.xml' => '~/external_file/com.apple.ibooks.display-options.xml'
# in resources block, you can define resources by its relative path and datasource.
# item creator methods are: files, file.
resources(:workdir => '~/epub_source') {
# Reads from file. in EPUB container, they are placed at the same path.
file 'img/image0.jpg'
files('img/image.jpg','img/image2.jpg')
glob 'img/*.jpg' # means files(Dir.glob('img/*.jpg'))
# Reads from file. will be placed at path indicated by key.
files('img/image.jpg' => 'imgage.jpg')
# Read from IO object.
files('img/image.png' => supplied_io, 'img/image2.png' => supplied_io2)
# this will be end in error:
# files(io1, io2)
# specify remote resource.
# only referenced from the EPUB package.
file 'http://example.com/video/remote_video.qt'
media_type('video/quicktime')
# specify media type.
file 'resources/pv.mp4'
media_type('video/mp4')
files('audio/voice1.mp4','audio/music1.mp4')
media_type('audio/mp4') # applied to all items in the line above.
# media_type to some file
with_media_type('video/mp4') {
file 'resources/v1.mp4'
file 'resources/v2.mp4'
file 'resources/v3.mp4'
}
# with_media_type and media_type
with_media_type('video/mp4') {
file 'resources/v1.mp4'
file 'resources/v2.mp4'
file 'resources/a4.mp4'
media_type 'audio/mp4' # override with_media_type
}
# Read from IO object: loop
# supplied_IOs = { 'path' => io, 'path' => io... }
supplied_IOs.each {
|name, io|
file name => io
}
file 'css/default.css'
# indicate property.
# this is cover image.
cover_image 'img/cover.jpg'
# this is navigation document.
nav 'text/toc.xhtml'
# ordered item. will be added to spine.
ordered {
# specify texts on table of contents for auto-generated toc.
# (if you supply navigation document with method 'nav', 'heading' has no effect.)
file('text/chap1.xhtml')
heading 'Chapter 1'
file 'text/chap2.xhtml'
# fallback chain: style 1
fallback_group {
file 'chap3_docbook.xhtml'
mimetype('application/docbook+xml')
file 'chap3.xml'
mimetype "application/z3986-auth+xml"
file 'chap3.xhtml'
}
# fallback chain: style 2
fallback_chain_files 'chap4_docbook.xhtml', 'chap4.xml', 'chap4.xhtml'
mimetype('application/docbook+xml','application/z3986-auth+xml' 'application/xhtml+xml')
# fallback chain: style 3 + with_mimetype
with_mimetype('application/docbook+xml','application/z3986-auth+xml' 'application/xhtml+xml') {
fallback_chain_files 'chap5_docbook.xhtml', 'chap5.xml', 'chap5.xhtml'
fallback_chain_files 'chap6_docbook.xhtml', 'chap6.xml', 'chap6.xhtml'
fallback_chain_files 'chap7_docbook.xhtml', 'chap7.xml', 'chap7.xhtml'
}
}
}
}
builder.generate_epub('sample.epub')
# -*- coding: utf-8 -*-
require 'rubygem'
require 'gepub'
workdir = 'epub/example/'
builder = GEPUB::Builder.new {
unique_identifier 'http:/example.jp/bookid_in_url', 'BookID', 'URL'
language 'en'
title 'GEPUB Sample Book'
subtitle 'This book is just a sample'
alt 'ja' => 'これはあくまでサンプルです'
creator 'KOJIMA Satoshi'
alt 'ja' => '小嶋智'
contributors 'Denshobu', 'Asagaya Densho', 'Shonan Densho Teidan', 'eMagazine Torutaru'
alts 'ja' => ['電書部','アサガヤデンショ','湘南電書鼎談','電子雑誌トルタル']
date '2012-02-29T00:00:00Z'
resources(:workdir => workdir) {
cover_image 'img/image1.jpg' => 'image1.jpg'
ordered {
file 'text/chap1.xhtml'
heading 'Chapter 1'
file 'text/chap1-1.xhtml'
file 'text/chap2.html'
heading 'Chapter 2'
}
}
}
epubname = File.join(File.dirname(__FILE__), 'example_test_with_builder.epub')
builder.generate_epub(epubname)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment