/kitabu

A framework for creating e-books from Markdown/Textile text markup using Ruby. Using the Prince PDF generator, you'll be able to get high quality PDFs.

Primary LanguageRuby

kitabu

While Prince is too expensive (495USD for a single user license), the free version available at www.princexml.com/download generates a PDF with a small logo on the first page, which is removed when sent to a printer.

Features

  • Write using Markdown, Textile or plain HTML

  • Book layout support

  • Syntax highlight

  • Generate HTML, PDF, e-Pub, Mobi and Text files

  • Table of Contents automatically generated from chapter titles

Installation

To install Kitabu, you’ll need a working Ruby 1.9+ installation. If you’re cool with it, just run the following command to install it.

gem install kitabu

After installing Kitabu, run the following command to check your external dependencies.

$ kitabu check

Prince XML: Converts HTML files into PDF files.
Installed.

KindleGen: Converts ePub e-books into .mobi files.
Installed.

html2text: Converts HTML documents into plain text.
Not installed.

pygments.rb: A generic syntax highlight. If installed, replaces CodeRay.
Not installed.

There’s no requirements here; just make sure you cleared the correct dependency based on the formats you want to export to.

Usage

To create a new e-book, just run

$ kitabu new mybook

This command creates a directory mybook with the following structure:

mybook
├── code
├── config
│   ├── helper.rb
│   └── kitabu.yml
├── images
├── output
├── templates
│   ├── epub
│   │   ├── cover.erb
│   │   ├── cover.png
│   │   ├── page.erb
│   │   └── user.css
│   └── html
│       ├── layout.css
│       ├── layout.erb
│       ├── syntax.css
│       └── user.css
└── text
    └── 01_Welcome.md

The config/kitabu.yml file holds some information about your book; so you’ll always change it.

Now it’s time to write your e-book. All your book content will be placed on the text directory. Kitabu requires you to separate your book into chapters. A chapter is nothing but a directory that holds lots of text files. The e-book will be generated using every folder/file alphabetically. So be sure to use a sequential numbering as the name. Here’s a sample:

* text
  * 01_Introduction
    * 01_introduction.markdown
  * 02_What_is_Ruby_on_Rails
    * 01_MVC.html
    * 02_DRY.html
    * 03_Convention_Over_Configuration.markdown
  * 03_Installing_Ruby_on_Rails
    * 01_Installing.textile
    * 02_Mac_OS_X_instructions.textile
    * 03_Windows_instructions.markdown
    * 04_Ubuntu_Linux_instructions.markdown

If you prefer, you can add a chapter per file:

* text
  * 01_Introduction.markdown
  * 02_What_is_Ruby_on_Rails.markdown
  * 03_Installing_Ruby_on_Rails.markdown

Note that you can use any format you want at the same time. Just use one of the following extensions: .html, .markdown, .mkdn or .textile.

You’ll want to see your progress eventually; it’s time for you to generate the book PDF. Just run the command kitabu export and your book will be created on the output directory.

Kitabu can generate a Table of Contents (TOC) based on your h2-h6 tags. The h1 tag is discarded because it’s meant to be the book title.

To print the TOC, you need to print a variable called toc, using the eRb tag.

<%= toc %>

Syntax Highlighting

We use CodeRay for syntax highlighting. To highlight a given code snippet, just do something like

@@@ ruby
class User < ActiveRecord::Base
  validates_presence_of :login, :password, :email
  validates_uniqueness_of :login, :email
end
@@@

If can replace CodeRay by Pygments; just install the gem pygments.rb.

Don’t indent the source block or will end up with unexpected results.

If you want to highlight a file, you need to place it into the code directory and call it like this:

@@@ ruby some_file.rb @@@

You can specify the lines you want to highlight; the example below will highlight lines 10-17 from some_file.rb.

@@@ ruby some_file.rb:10,15 @@@

You can also specify named blocks. Named blocks are identified by @begin and @end marks. If some_file.rb has the following code

require "rubygems"
require "hpricot"
require "open"

# @begin: get_all_h2_tags
doc = Hpricot(open('http://simplesideias.com.br'))
(doc/"h2").each {|h2| puts h2.inner_text }
# @end: get_all_h2_tags

and you can get the code between get_all_h2_tags using

@@@ ruby some_file#get_all_h2_tags @@@

NOTE: Any named block annotation will be removed from @@@ ruby file.rb usage.

Markdown Processors

By default, RDiscount is the Markdown processor. However, you can switch to different implementations by simply installing any of the following processors:

Note: RDiscount will always be installed as Kitabu’s dependency but only used when no alternative library is available.

References

Samples

I published two PDFs (portuguese only) exploring lots of features. Check it out:

There’s also an (probably) outdated Rails Guide section in the examples directory.

Upgrading from previous version

To upgrade a e-book created with Kitabu < 1.0.0, create a new e-book.

$ kitabu new mybook

Then copy all your configuration info from config.yml to config/kitabu.yml.

Any Textile extension need to be ported to config/helper.rb. Check commented source for examples.

Move directories text, code, and images to your new e-book directory.

The last thing you need to upgrade is the syntax highlighting. You have to move from

syntax. some_file.rb

to

@@@ ruby some_file.rb @@@

Check the section “Syntax Highlighting” for detailed info.

Maintainer

License

(The MIT License)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the ‘Software’), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED ‘AS IS’, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.