-
-
Save halcyon/10611192 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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