Permalink
Browse files

decoupling html generation from mobi generation

  • Loading branch information...
1 parent df145be commit a7a060f5c8f185ec0948d144fb38e9ffd39d8aa2 @herval committed Oct 15, 2012
Showing with 26 additions and 8 deletions.
  1. +8 −0 README.markdown
  2. +18 −8 lib/kindlerb.rb
View
@@ -33,6 +33,14 @@ Run the program at the root of the file tree:
The output will be a mobi document.
+You can also use the generator inside a Ruby script - you can use it two ways:
+
+ Kindlerb.run # will pack everything and generate a .mobi in the current directory
+
+ Kindlerb.prepare_content(path) # build all the files required by kindlegen (opf, ncx, etc).
+ # you can customize whatever you want here - for instance, override the 'sections.html' files, etc
+ Kindlerb.generate_mobi(path) # generate the .mobi file.
+
The file tree input structure is
_document.yml
View
@@ -21,7 +21,7 @@ def shorten(max)
module Kindlerb
- VERSION = '0.0.9'
+ VERSION = '0.0.10'
# allows overriding the default mustaches with ones specified under the templates folder
def self.custom_or_default_template(target_dir, name)
@@ -34,10 +34,7 @@ def self.custom_or_default_template(target_dir, name)
end
end
- def self.run
-
- target_dir = Pathname.new(ARGV.first || '.')
-
+ def self.prepare_content(target_dir)
opf_template = custom_or_default_template(target_dir, "opf")
ncx_template = custom_or_default_template(target_dir, "ncx")
contents_template = custom_or_default_template(target_dir, "contents")
@@ -58,7 +55,7 @@ def self.run
abort "Missing _document.yml. Your input file tree is not structured correctly. Please read the README."
end
- document = YAML::load_file("_document.yml")
+ document = YAML::load_file("_document.yml")
document[:spine_items] = []
section_html_files = []
@@ -144,13 +141,26 @@ def self.run
# contents
contents = Mustache.render contents_template, document
File.open("contents.html", "w") {|f| f.puts contents}
- puts "Wrote #{target_dir}/contents.html"
+ puts "Wrote #{target_dir}/contents.html"
+ end
+ end
+
+ def self.generate_mobi(target_dir)
+ Dir.chdir target_dir do
+ document = YAML::load_file("_document.yml")
outfile = document['mobi_outfile']
puts "Writing #{outfile}"
cmd = "kindlegen -verbose -c2 -o #{outfile} kindlerb.opf && echo 'Wrote MOBI to #{outfile}'"
puts cmd
- exec cmd
+ system cmd
end
end
+
+ def self.run
+ target_dir = Pathname.new(ARGV.first || '.')
+
+ self.prepare_content(target_dir)
+ self.generate_mobi(target_dir)
+ end
end

0 comments on commit a7a060f

Please sign in to comment.