Skip to content

Instantly share code, notes, and snippets.

@halcyon
Forked from labocho/change_lang_of_epub.rb
Created April 14, 2014 02:15
Show Gist options
  • Save halcyon/10611192 to your computer and use it in GitHub Desktop.
Save halcyon/10611192 to your computer and use it in GitHub Desktop.
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