Skip to content

Instantly share code, notes, and snippets.

@mojavelinux
Last active April 18, 2017 22:18
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mojavelinux/8b27b77fc2ad5745173c93b5a7163a12 to your computer and use it in GitHub Desktop.
Save mojavelinux/8b27b77fc2ad5745173c93b5a7163a12 to your computer and use it in GitHub Desktop.
Rake build tasks that use Asciidoctor APIs to produce common output formats for a book (or other type of publication) written in AsciiDoc.
=begin
To use this build script, first install the bundler gem:
$ gem install bundler
Then use the `bundle` command to install the dependencies into the project:
$ rm -rf Gemfile.lock .bundle
bundle config --local git.allow_insecure true
bundle config --local build.nokogiri --use-system-libraries
bundle --path=.bundle/gems
NOTE The gems are installed under the path .bundle/gems in the project.
Finally, run a named task using the `bundle exec rake` command:
$ bundle exec rake pdf
You can get a list of tasks using the following command:
$ bundle exec rake -T
=end
MASTER_FILENAME='master.adoc'
BUILD_DIR='build'
autoload :FileUtils, 'fileutils'
desc 'Build the HTML5 format'
task :html do
# TODO move logic to images task
((FileList.new '**/*.{jpg,png,svg}').exclude %(#{BUILD_DIR}/**/*)).each do |img_path|
target_dir = File.join BUILD_DIR, (File.dirname img_path)
FileUtils.mkdir_p target_dir
FileUtils.cp img_path, target_dir
end
require 'asciidoctor'
Asciidoctor.convert_file MASTER_FILENAME,
safe: :unsafe,
to_dir: BUILD_DIR,
mkdirs: true
end
desc 'Build the PDF format'
task :pdf do
require 'asciidoctor-pdf'
Asciidoctor.convert_file MASTER_FILENAME,
safe: :unsafe,
backend: 'pdf',
to_dir: BUILD_DIR,
mkdirs: true
end
# TIP invoke using epub[ebook-validate] to validate
desc 'Build the EPUB3 format'
task :epub, [:attrs] do |_, args|
require 'asciidoctor-epub3'
Asciidoctor.convert_file MASTER_FILENAME,
safe: :unsafe,
backend: 'epub3',
attributes: [args[:attrs]].compact * ' ',
to_dir: BUILD_DIR,
mkdirs: true
end
desc 'Build the MOBI format'
task :mobi do
require 'asciidoctor-epub3'
Asciidoctor.convert_file MASTER_FILENAME,
safe: :unsafe,
backend: 'epub3',
attributes: 'ebook-format=kf8',
to_dir: BUILD_DIR,
mkdirs: true
# NOTE remove the temporary -kf8.epub file so we don't get confused
File.unlink %(#{BUILD_DIR}/#{File.basename MASTER_FILENAME, '.*'}-kf8.epub)
end
task kf8: :mobi
desc 'Build the EPUB3 and MOBI formats'
task ebook: [:epub, :mobi]
desc 'Build all formats'
task default: [:html, :ebook, :pdf]
desc 'Clean the build directory'
task :clean do
FileUtils.remove_entry_secure BUILD_DIR if File.exist? BUILD_DIR
end
source 'https://rubygems.org'
gem 'rake', '12.0.0'
gem 'asciidoctor-pdf', '1.5.0.alpha.15'
gem 'asciidoctor-epub3', '1.5.0.alpha.7'
# NOTE refer to https://github.com/packetmonkey/prawn-gmagick#amazon-ec2--elastic-beanstalk-installation for prerequisites
gem 'prawn-gmagick', '0.0.8'
gem 'pygments.rb', '1.1.1'
gem 'kindlegen', '3.0.3'
gem 'epubcheck', '3.0.1'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment