initial version
This commit is contained in:
commit
d13eb1f4b5
5 changed files with 140 additions and 0 deletions
3
Gemfile
Normal file
3
Gemfile
Normal file
|
|
@ -0,0 +1,3 @@
|
||||||
|
source 'https://rubygems.org'
|
||||||
|
gem 'zip'
|
||||||
|
gem 'css_parser'
|
||||||
19
Gemfile.lock
Normal file
19
Gemfile.lock
Normal file
|
|
@ -0,0 +1,19 @@
|
||||||
|
GEM
|
||||||
|
remote: https://rubygems.org/
|
||||||
|
specs:
|
||||||
|
addressable (2.5.2)
|
||||||
|
public_suffix (>= 2.0.2, < 4.0)
|
||||||
|
css_parser (1.6.0)
|
||||||
|
addressable
|
||||||
|
public_suffix (3.0.0)
|
||||||
|
zip (2.0.2)
|
||||||
|
|
||||||
|
PLATFORMS
|
||||||
|
ruby
|
||||||
|
|
||||||
|
DEPENDENCIES
|
||||||
|
css_parser
|
||||||
|
zip
|
||||||
|
|
||||||
|
BUNDLED WITH
|
||||||
|
1.10.6
|
||||||
1
README.md
Normal file
1
README.md
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
Script to fix epub where the line spacings are set to a fixed value.
|
||||||
117
src/epub-spacing-fixer.rb
Executable file
117
src/epub-spacing-fixer.rb
Executable file
|
|
@ -0,0 +1,117 @@
|
||||||
|
#!/usr/bin/env ruby
|
||||||
|
|
||||||
|
require "rubygems"
|
||||||
|
require 'getoptlong'
|
||||||
|
require "zip/zip"
|
||||||
|
require "css_parser"
|
||||||
|
require "fileutils"
|
||||||
|
|
||||||
|
def open_zip(zip_file_name, nowrite=false)
|
||||||
|
Zip::ZipFile.open(zip_file_name) do |zipfile|
|
||||||
|
files = zipfile.select(&:file?)
|
||||||
|
files.each do |zip_entry|
|
||||||
|
#puts zip_entry.name
|
||||||
|
contents = zipfile.read(zip_entry.name)
|
||||||
|
css = ""
|
||||||
|
if zip_entry.name.match(/\.css$/)
|
||||||
|
# modify css here
|
||||||
|
contents = fix_css(contents)
|
||||||
|
# write back css file
|
||||||
|
zipfile.get_output_stream(zip_entry.name){ |f| f.puts contents } unless nowrite
|
||||||
|
else
|
||||||
|
# write back non-css file
|
||||||
|
zipfile.get_output_stream(zip_entry.name){ |f| f.puts contents } unless nowrite
|
||||||
|
end
|
||||||
|
end
|
||||||
|
zipfile.commit unless nowrite
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def fix_css(contents)
|
||||||
|
parser = CssParser::Parser.new
|
||||||
|
parser.load_string!(contents)
|
||||||
|
|
||||||
|
parser.each_rule_set{ |rule_set|
|
||||||
|
rule_set.each_declaration{ |decl|
|
||||||
|
if decl.match(/line-height/)
|
||||||
|
rule_set.remove_declaration!(decl)
|
||||||
|
end
|
||||||
|
}
|
||||||
|
if rule_set.selectors == ["body"]
|
||||||
|
rule_set.add_declaration!("line-height", "1.2")
|
||||||
|
elsif rule_set.selectors == ["body *"]
|
||||||
|
rule_set.add_declaration!("line-height", "inherit")
|
||||||
|
end
|
||||||
|
}
|
||||||
|
|
||||||
|
# If there is no 'body' selector, add one
|
||||||
|
if parser.find_by_selector('body').length == 0
|
||||||
|
css = <<-EOT.gsub(/^\t*/, '')
|
||||||
|
body {
|
||||||
|
line-height: 1.2;
|
||||||
|
}
|
||||||
|
EOT
|
||||||
|
parser.add_block!(css)
|
||||||
|
end
|
||||||
|
# If there is no 'body *' selector, add one
|
||||||
|
if parser.find_by_selector('body *').length == 0
|
||||||
|
css = <<-EOT.gsub(/^\t*/, '')
|
||||||
|
body * {
|
||||||
|
line-height: inherit;
|
||||||
|
}
|
||||||
|
EOT
|
||||||
|
parser.add_block!(css)
|
||||||
|
end
|
||||||
|
return parser.to_s
|
||||||
|
end
|
||||||
|
|
||||||
|
def usage
|
||||||
|
puts <<EOT
|
||||||
|
Tool to change epub css to fix common annoyances.
|
||||||
|
|
||||||
|
Usage: #{$0.sub(/.*\//, "")} [options] <file>
|
||||||
|
|
||||||
|
-h, --help show this message
|
||||||
|
-n do nothing
|
||||||
|
-o <file> output to file
|
||||||
|
EOT
|
||||||
|
exit
|
||||||
|
end
|
||||||
|
|
||||||
|
def cmdline
|
||||||
|
options = Hash.new
|
||||||
|
begin
|
||||||
|
opts = GetoptLong.new(
|
||||||
|
[ "-h", "--help", GetoptLong::NO_ARGUMENT ],
|
||||||
|
[ "-n", GetoptLong::NO_ARGUMENT ],
|
||||||
|
[ "-o", GetoptLong::REQUIRED_ARGUMENT ]
|
||||||
|
)
|
||||||
|
opts.quiet=true
|
||||||
|
|
||||||
|
opts.each do |opt, arg|
|
||||||
|
options[opt] = arg
|
||||||
|
end
|
||||||
|
|
||||||
|
rescue
|
||||||
|
print "#{$!}\n"
|
||||||
|
usage
|
||||||
|
end
|
||||||
|
if options["-h"]
|
||||||
|
usage
|
||||||
|
end
|
||||||
|
return options
|
||||||
|
end
|
||||||
|
|
||||||
|
options = cmdline
|
||||||
|
infile = ARGV[0]
|
||||||
|
|
||||||
|
if options["-o"]
|
||||||
|
FileUtils.cp(infile, options["-o"])
|
||||||
|
infile = options["-o"]
|
||||||
|
end
|
||||||
|
|
||||||
|
open_zip(infile, options["-n"])
|
||||||
|
|
||||||
|
|
||||||
|
# the following seems to be completely evil in css:
|
||||||
|
# display: block
|
||||||
BIN
test/Openstreetmap.epub
Normal file
BIN
test/Openstreetmap.epub
Normal file
Binary file not shown.
Loading…
Add table
Add a link
Reference in a new issue