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.

Installing

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

sudo gem install fnando-kitabu -s http://gems.github.com

If you're running Ruby 1.8 and don't need the syntax highlighting feature, you can skip the next steps.

Syntax Highlighting on Ruby 1.8

To have syntax highlighting support on Ruby 1.8, you need to install Oniguruma regular expression library that can be found at http://www.geocities.jp/kosako3/oniguruma/.

You might get this error (specially on Linux):

require 'uv'
LoadError: libonig.so.2: cannot open shared object file: No such file or directory - 
/usr/lib/ruby/gems/1.8/gems/oniguruma-1.1.0/lib/oregexp.so

It means Ruby can’t find libonig.so.2. Add the path to the directory where the file is located to /etc/ld.so.conf:

include /etc/ld.so.conf.d/*.conf
include /usr/local/lib

Save the file and execute the command ldconfig

Syntax Highlighting on Ruby 1.9

You're good to go! Nothing to install! :)

Getting started

After installing Kitabu and its dependencies, you can easily create your PDF! Just run the following command from your terminal window.

$ kitabu mypdf

You can check available layouts and themes with kitabu --help.

The kitabu command creates a directory "mybook" with the following structure:

The config.yml file holds some information about your book; so you'll always change it. This is the default generated file:

title: [Your Book Title]
copyright: Copyright (c) 2008 [Your Name], All Rights Reserved
authors:
  - [Your Name]
  - [Other Name]
subject: [Write down what your book is about]
keywords: [The keywords related to your book]
theme: eiffel

If you're writing on a different language, the user.css file can override all the messages added by the layout.css. Let's take Brazilian Portuguese for instance; just add the following CSS to templates/user.css: "Chapters" will be translated to "Capítulo" and "Figure" to "Figura".

/* translations */
div.chapter h2::before { content: "Capítulo " counter(chapter) ; }
div.chapter h2 { string-set: header "Capítulo " counter(chapter) ": " content(); }
p.figure span.caption:before { content: "Figura " counter(figure) ": "; }

Now it's time to write your 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 Markdown/Textile files. The book will be generated using every folder/file alphabetically. So be sure to use a sequential numbering as the name. Here's a sample:

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

Note that you can use Textile or Markdown at the same time. Just use the .markdown or .textile file extension.

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

There are other rake tasks you can use:

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 enable TOC you should have Nokogiri or Hpricot installed.

$ sudo gem install nokogiri
        $ sudo gem install hpricot

If both libraries are installed, Nokogiri will be used.

If you need to link to a specific chapter, you can use the titles rake task to know what's the permalink that you need. For example, a title Installing Mac OS X will have a permalink installing-mac-os-x and you can link to this chapter by writing "See more on Installing Mac OS X":#installing-mac-os-x on Textile documents.

To generate the TOC, you need to print a variable called toc, using the eRb tag <%= toc %>.

Syntax Highlighting

If you're using Textile, all you need to do is use the tag syntax.. For example, to highlight a code added right into your text, just do something like

syntax(ruby_on_rails). class User < ActiveRecord::Base
  validates_presence_of :login, :password, :email
__
  validates_uniqueness_of :login, :email
end

To keep multiple line breaks into a single code block, add a line __; Kitabu will replace it when generating the HTML file.

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

syntax(ruby_on_rails). some_file.rb

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

syntax(ruby_on_rails 10,17). some_file.rb

You can also specify named blocks to highlight. 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

you can get the code between get_all_h2_tags using

syntax(ruby#get_all_h2_tags). some_file.rb

Note: Makdown uses the same syntax above. You just need to indent your code (as usual) and add the syntax. thing as the first line.

Textile special tags

Kitabu extends Textile with some interesting tags:

Footnote

Ruby on Rails %{"Rails" and "Ruby on Rails" are trademarks of David Heinemeier Hansson}

Figure

figure(This is the caption). some_image.jpg

Note

note. Remember to do something!

Auto URL

<http://simplesideias.com.br>

Link to File

You need to set base_url in your config.yml file.

file. app/models/users.rb

You can define your own tags by creating a Helper module. Just add something like this to your Rakefile.

KITABU_ROOT = File.dirname(__FILE__)
ENV['KITABU_NAME'] ||= File.basename(KITABU_ROOT)

module Helpers
  def textile_custom(tag, atts, cite, content)
    '<p class="custom">%s</p>' % content
  end
end

require "kitabu/tasks"

On your text, just use custom. some text and it will be replaced by your helper.

Markdown Processors

By default, RDiscount is the Markdown processor. However, you can switch to different implementations by adding the markdown option to your config.yml file. The processors that can be used are:

References

Maintainer

This project was created by Nando Vieira.

Check it out another interesting projects at http://fnando.github.com.

Contributors