2012-11-28 11:25:32 -05:00
|
|
|
#!/usr/bin/env ruby
|
2017-08-13 09:02:48 -04:00
|
|
|
# frozen_string_literal: true
|
2012-11-28 11:25:32 -05:00
|
|
|
|
2016-12-28 21:53:51 -05:00
|
|
|
require "kindlerb"
|
2016-08-06 13:21:59 -04:00
|
|
|
require "nokogiri"
|
|
|
|
require "fileutils"
|
|
|
|
require "yaml"
|
|
|
|
require "date"
|
2012-11-28 11:25:32 -05:00
|
|
|
|
|
|
|
module Kindle
|
|
|
|
extend self
|
|
|
|
|
|
|
|
def generate(output_dir, mobi_outfile, logfile)
|
|
|
|
output_dir = File.absolute_path(output_dir)
|
2016-08-06 13:55:02 -04:00
|
|
|
Dir.chdir output_dir do
|
2012-11-28 11:25:32 -05:00
|
|
|
puts "=> Using output dir: #{output_dir}"
|
|
|
|
puts "=> Arranging html pages in document order"
|
|
|
|
toc = File.read("toc.ncx")
|
2016-08-06 13:21:59 -04:00
|
|
|
doc = Nokogiri::XML(toc).xpath("//ncx:content", "ncx" => "http://www.daisy.org/z3986/2005/ncx/")
|
2016-08-16 03:30:11 -04:00
|
|
|
html_pages = doc.select { |c| c[:src] }.map { |c| c[:src] }.uniq
|
2016-08-06 13:55:02 -04:00
|
|
|
|
2012-11-28 11:25:32 -05:00
|
|
|
generate_front_matter(html_pages)
|
|
|
|
|
|
|
|
generate_sections(html_pages)
|
|
|
|
|
|
|
|
generate_document_metadata(mobi_outfile)
|
|
|
|
|
2015-11-19 08:05:47 -05:00
|
|
|
puts "Creating MOBI document with kindlegen. This may take a while."
|
2016-12-18 10:13:50 -05:00
|
|
|
if Kindlerb.run(output_dir)
|
|
|
|
puts "MOBI document generated at #{File.expand_path(mobi_outfile, output_dir)}"
|
|
|
|
end
|
2012-11-28 11:25:32 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def generate_front_matter(html_pages)
|
|
|
|
frontmatter = []
|
2016-08-16 03:30:11 -04:00
|
|
|
html_pages.delete_if { |x|
|
2018-07-28 17:37:17 -04:00
|
|
|
if /(toc|welcome|copyright).html/.match?(x)
|
2012-11-28 11:25:32 -05:00
|
|
|
frontmatter << x unless x =~ /toc/
|
|
|
|
true
|
|
|
|
end
|
|
|
|
}
|
2016-08-16 03:30:11 -04:00
|
|
|
html = frontmatter.map { |x|
|
2012-11-28 11:25:32 -05:00
|
|
|
Nokogiri::HTML(File.open(x)).at("body").inner_html
|
|
|
|
}.join("\n")
|
|
|
|
|
|
|
|
fdoc = Nokogiri::HTML(html)
|
|
|
|
fdoc.search("h3").each do |h3|
|
2016-08-06 13:21:59 -04:00
|
|
|
h3.name = "h4"
|
2012-11-28 11:25:32 -05:00
|
|
|
end
|
2016-08-06 13:55:02 -04:00
|
|
|
fdoc.search("h2").each do |h2|
|
2016-08-06 13:21:59 -04:00
|
|
|
h2.name = "h3"
|
|
|
|
h2["id"] = h2.inner_text.gsub(/\s/, "-")
|
2012-11-28 11:25:32 -05:00
|
|
|
end
|
|
|
|
add_head_section fdoc, "Front Matter"
|
2016-10-28 23:05:58 -04:00
|
|
|
File.open("frontmatter.html", "w") { |f| f.puts fdoc.to_html }
|
2012-11-28 11:25:32 -05:00
|
|
|
html_pages.unshift "frontmatter.html"
|
|
|
|
end
|
|
|
|
|
|
|
|
def generate_sections(html_pages)
|
2018-02-21 18:40:21 -05:00
|
|
|
FileUtils.rm_rf("sections/")
|
2012-11-28 11:25:32 -05:00
|
|
|
html_pages.each_with_index do |page, section_idx|
|
2018-02-21 18:40:21 -05:00
|
|
|
FileUtils.mkdir_p("sections/%03d" % section_idx)
|
2012-11-28 11:25:32 -05:00
|
|
|
doc = Nokogiri::HTML(File.open(page))
|
2016-08-06 13:21:59 -04:00
|
|
|
title = doc.at("title").inner_text.gsub("Ruby on Rails Guides: ", "")
|
|
|
|
title = page.capitalize.gsub(".html", "") if title.strip == ""
|
2016-08-16 03:30:11 -04:00
|
|
|
File.open("sections/%03d/_section.txt" % section_idx, "w") { |f| f.puts title }
|
2016-10-28 23:05:58 -04:00
|
|
|
doc.xpath("//h3[@id]").each_with_index do |h3, item_idx|
|
2012-11-28 11:25:32 -05:00
|
|
|
subsection = h3.inner_text
|
2016-08-16 03:30:11 -04:00
|
|
|
content = h3.xpath("./following-sibling::*").take_while { |x| x.name != "h3" }.map(&:to_html)
|
2012-11-28 11:25:32 -05:00
|
|
|
item = Nokogiri::HTML(h3.to_html + content.join("\n"))
|
2016-08-06 13:55:02 -04:00
|
|
|
item_path = "sections/%03d/%03d.html" % [section_idx, item_idx]
|
2012-11-28 11:25:32 -05:00
|
|
|
add_head_section(item, subsection)
|
|
|
|
item.search("img").each do |img|
|
2016-08-06 13:21:59 -04:00
|
|
|
img["src"] = "#{Dir.pwd}/#{img['src']}"
|
2012-11-28 11:25:32 -05:00
|
|
|
end
|
2016-08-16 03:30:11 -04:00
|
|
|
item.xpath("//li/p").each { |p| p.swap(p.children); p.remove }
|
|
|
|
File.open(item_path, "w") { |f| f.puts item.to_html }
|
2012-11-28 11:25:32 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def generate_document_metadata(mobi_outfile)
|
|
|
|
puts "=> Generating _document.yml"
|
|
|
|
x = Nokogiri::XML(File.open("rails_guides.opf")).remove_namespaces!
|
|
|
|
cover_jpg = "#{Dir.pwd}/images/rails_guides_kindle_cover.jpg"
|
2016-08-06 13:21:59 -04:00
|
|
|
cover_gif = cover_jpg.sub(/jpg$/, "gif")
|
2012-11-28 11:25:32 -05:00
|
|
|
puts `convert #{cover_jpg} #{cover_gif}`
|
|
|
|
document = {
|
2016-08-06 13:21:59 -04:00
|
|
|
"doc_uuid" => x.at("package")["unique-identifier"],
|
|
|
|
"title" => x.at("title").inner_text.gsub(/\(.*$/, " v2"),
|
|
|
|
"publisher" => x.at("publisher").inner_text,
|
|
|
|
"author" => x.at("creator").inner_text,
|
|
|
|
"subject" => x.at("subject").inner_text,
|
|
|
|
"date" => x.at("date").inner_text,
|
|
|
|
"cover" => cover_gif,
|
|
|
|
"masthead" => nil,
|
|
|
|
"mobi_outfile" => mobi_outfile
|
2012-11-28 11:25:32 -05:00
|
|
|
}
|
|
|
|
puts document.to_yaml
|
2016-08-16 03:30:11 -04:00
|
|
|
File.open("_document.yml", "w") { |f| f.puts document.to_yaml }
|
2012-11-28 11:25:32 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def add_head_section(doc, title)
|
|
|
|
head = Nokogiri::XML::Node.new "head", doc
|
|
|
|
title_node = Nokogiri::XML::Node.new "title", doc
|
|
|
|
title_node.content = title
|
|
|
|
title_node.parent = head
|
|
|
|
css = Nokogiri::XML::Node.new "link", doc
|
2016-08-06 13:21:59 -04:00
|
|
|
css["rel"] = "stylesheet"
|
|
|
|
css["type"] = "text/css"
|
|
|
|
css["href"] = "#{Dir.pwd}/stylesheets/kindle.css"
|
2012-11-28 11:25:32 -05:00
|
|
|
css.parent = head
|
|
|
|
doc.at("body").before head
|
|
|
|
end
|
|
|
|
end
|