Skip to content

Instantly share code, notes, and snippets.

@labocho
Created June 5, 2011 17:40
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save labocho/1009210 to your computer and use it in GitHub Desktop.
Save labocho/1009210 to your computer and use it in GitHub Desktop.
Change language of EPUB file for looking up dictionary in iBooks
require "rubygems"
require "zipruby" # gem install zipruby
require "rexml/document"
require "fileutils"
class Zip::Archive
def index
i = 0
each do |f|
if yield f
return i
end
i += 1
end
nil
end
end
unless filename = ARGV.shift
puts "File name required. usage: ruby change_lang_of_epub.rb somebook.epub"
exit
end
lang = ARGV.shift || "ja"
new_filename = filename.dup
new_filename[-File.extname(new_filename).length, 0] = ".#{lang}"
FileUtils.cp filename, new_filename
Zip::Archive.open(new_filename) do |ar|
unless opf_index = ar.index{|f| f.name =~ /\.opf\z/}
puts "OPF not found"
exit
end
doc = nil
ar.fopen(opf_index) do |f|
doc = REXML::Document.new(f.read)
end
doc.elements["/package/metadata/dc:language"].text = lang
ar.replace_buffer(opf_index, doc.to_s)
end
puts "done"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment