|
| 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 |
0 commit comments