Skip to content

Commit 86269b4

Browse files
committed
Merge pull request rails#8348 from danchoi/master
Kindle ebook generation with working section navigation
2 parents ea86fa3 + e8fe1d1 commit 86269b4

File tree

4 files changed

+128
-5
lines changed

4 files changed

+128
-5
lines changed

guides/Rakefile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ namespace :guides do
1313

1414
desc "Generate .mobi file. The kindlegen executable must be in your PATH. You can get it for free from http://www.amazon.com/kindlepublishing"
1515
task :kindle do
16+
unless `kindlerb -v 2> /dev/null` =~ /kindlerb 0.1.1/
17+
abort "Please `gem install kindlerb`"
18+
end
19+
unless `convert` =~ /convert/
20+
abort "Please install ImageMagick`"
21+
end
1622
ENV['KINDLE'] = '1'
1723
Rake::Task['guides:generate:html'].invoke
1824
end

guides/rails_guides/generator.rb

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,11 +112,9 @@ def check_for_kindlegen
112112
end
113113

114114
def generate_mobi
115-
opf = "#{output_dir}/rails_guides.opf"
115+
require 'rails_guides/kindle'
116116
out = "#{output_dir}/kindlegen.out"
117-
118-
system "kindlegen #{opf} -o #{mobi} > #{out} 2>&1"
119-
puts "Guides compiled as Kindle book to #{mobi}"
117+
Kindle.generate(output_dir, mobi, out)
120118
puts "(kindlegen log at #{out})."
121119
end
122120

guides/rails_guides/kindle.rb

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
#!/usr/bin/env ruby
2+
3+
unless `which kindlerb`
4+
abort "Please gem install kindlerb"
5+
end
6+
7+
require 'nokogiri'
8+
require 'fileutils'
9+
require 'yaml'
10+
require 'date'
11+
12+
module Kindle
13+
extend self
14+
15+
def generate(output_dir, mobi_outfile, logfile)
16+
output_dir = File.absolute_path(output_dir)
17+
Dir.chdir output_dir do
18+
puts "=> Using output dir: #{output_dir}"
19+
puts "=> Arranging html pages in document order"
20+
toc = File.read("toc.ncx")
21+
doc = Nokogiri::XML(toc).xpath("//ncx:content", 'ncx' => "http://www.daisy.org/z3986/2005/ncx/")
22+
html_pages = doc.select {|c| c[:src]}.map {|c| c[:src]}.uniq
23+
24+
generate_front_matter(html_pages)
25+
26+
generate_sections(html_pages)
27+
28+
generate_document_metadata(mobi_outfile)
29+
30+
puts "Creating MOBI document with kindlegen. This make take a while."
31+
cmd = "kindlerb . > #{File.absolute_path logfile} 2>&1"
32+
puts cmd
33+
system(cmd)
34+
puts "MOBI document generated at #{File.expand_path(mobi_outfile, output_dir)}"
35+
end
36+
end
37+
38+
def generate_front_matter(html_pages)
39+
frontmatter = []
40+
html_pages.delete_if {|x|
41+
if x =~ /(toc|welcome|credits|copyright).html/
42+
frontmatter << x unless x =~ /toc/
43+
true
44+
end
45+
}
46+
html = frontmatter.map {|x|
47+
Nokogiri::HTML(File.open(x)).at("body").inner_html
48+
}.join("\n")
49+
50+
fdoc = Nokogiri::HTML(html)
51+
fdoc.search("h3").each do |h3|
52+
h3.name = 'h4'
53+
end
54+
fdoc.search("h2").each do |h2|
55+
h2.name = 'h3'
56+
h2['id'] = h2.inner_text.gsub(/\s/, '-')
57+
end
58+
add_head_section fdoc, "Front Matter"
59+
File.open("frontmatter.html",'w') {|f| f.puts fdoc.to_html}
60+
html_pages.unshift "frontmatter.html"
61+
end
62+
63+
def generate_sections(html_pages)
64+
FileUtils::rm_rf("sections/")
65+
html_pages.each_with_index do |page, section_idx|
66+
FileUtils::mkdir_p("sections/%03d" % section_idx)
67+
doc = Nokogiri::HTML(File.open(page))
68+
title = doc.at("title").inner_text.gsub("Ruby on Rails Guides: ", '')
69+
title = page.capitalize.gsub('.html', '') if title.strip == ''
70+
File.open("sections/%03d/_section.txt" % section_idx, 'w') {|f| f.puts title}
71+
doc.xpath("//h3[@id]").each_with_index do |h3,item_idx|
72+
subsection = h3.inner_text
73+
content = h3.xpath("./following-sibling::*").take_while {|x| x.name != "h3"}.map {|x| x.to_html}
74+
item = Nokogiri::HTML(h3.to_html + content.join("\n"))
75+
item_path = "sections/%03d/%03d.html" % [section_idx, item_idx]
76+
add_head_section(item, subsection)
77+
item.search("img").each do |img|
78+
img['src'] = "#{Dir.pwd}/#{img['src']}"
79+
end
80+
item.xpath("//li/p").each {|p| p.swap(p.children); p.remove}
81+
File.open(item_path, 'w') {|f| f.puts item.to_html}
82+
end
83+
end
84+
end
85+
86+
def generate_document_metadata(mobi_outfile)
87+
puts "=> Generating _document.yml"
88+
x = Nokogiri::XML(File.open("rails_guides.opf")).remove_namespaces!
89+
cover_jpg = "#{Dir.pwd}/images/rails_guides_kindle_cover.jpg"
90+
cover_gif = cover_jpg.sub(/jpg$/, 'gif')
91+
puts `convert #{cover_jpg} #{cover_gif}`
92+
document = {
93+
'doc_uuid' => x.at("package")['unique-identifier'],
94+
'title' => x.at("title").inner_text.gsub(/\(.*$/, " v2"),
95+
'publisher' => x.at("publisher").inner_text,
96+
'author' => x.at("creator").inner_text,
97+
'subject' => x.at("subject").inner_text,
98+
'date' => x.at("date").inner_text,
99+
'cover' => cover_gif,
100+
'masthead' => nil,
101+
'mobi_outfile' => mobi_outfile
102+
}
103+
puts document.to_yaml
104+
File.open("_document.yml", 'w'){|f| f.puts document.to_yaml}
105+
end
106+
107+
def add_head_section(doc, title)
108+
head = Nokogiri::XML::Node.new "head", doc
109+
title_node = Nokogiri::XML::Node.new "title", doc
110+
title_node.content = title
111+
title_node.parent = head
112+
css = Nokogiri::XML::Node.new "link", doc
113+
css['rel'] = 'stylesheet'
114+
css['type'] = 'text/css'
115+
css['href'] = "#{Dir.pwd}/stylesheets/kindle.css"
116+
css.parent = head
117+
doc.at("body").before head
118+
end
119+
end

guides/source/kindle/rails_guides.opf.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232

3333
<item id="toc" media-type="application/x-dtbncx+xml" href="toc.ncx" />
3434

35-
<item id="cover" media-type="image/jpeg" href="images/rails_guides_kindle_cover.jpg"/>
35+
<item id="cover" media-type="image/jpg" href="images/rails_guides_kindle_cover.jpg"/>
3636
</manifest>
3737

3838
<spine toc="toc">

0 commit comments

Comments
 (0)