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:
- mypdf
- config.yml
- images
- output
- Rakefile
- templates
- layout.css
- layout.html
- syntax.css
- user.css
- text
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:
- text
- 01_Introduction
- 01_introduction.markdown
- 02_What_is_Ruby_on_Rails
- 01_MVC.markdown
- 02_DRY.markdown
- 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
- 01_Introduction
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 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:
html
- generate a html from your contentsyntaxes
- list all available syntaxesthemes
- list all available themestitles
- list all titles and its permalinksauto
- watch your text for changes and automatically generate html & PDF
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:
- peg_markdown (PEGMarkdown)
- bluecloth (BlueCloth)
- maruku (Maruku)
- rdiscount
References
- Textile: http://hobix.com/textile/
- Markdown: http://daringfireball.net/projects/markdown/syntax
Maintainer
This project was created by Nando Vieira.
Check it out another interesting projects at http://fnando.github.com.
Contributors
- Arthur Zapparoli (http://arthurgeek.net)
- Clinton R. Nixon (http://crnixon.org/)