commit d13eb1f4b566810d6cde8acdbaefcd9d3ba97ccd Author: Securely Erase Disk Date: Sun Dec 31 10:48:16 2017 +0100 initial version diff --git a/Gemfile b/Gemfile new file mode 100644 index 0000000..c16b7e0 --- /dev/null +++ b/Gemfile @@ -0,0 +1,3 @@ +source 'https://rubygems.org' +gem 'zip' +gem 'css_parser' diff --git a/Gemfile.lock b/Gemfile.lock new file mode 100644 index 0000000..e94bf4e --- /dev/null +++ b/Gemfile.lock @@ -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 diff --git a/README.md b/README.md new file mode 100644 index 0000000..5615175 --- /dev/null +++ b/README.md @@ -0,0 +1 @@ +Script to fix epub where the line spacings are set to a fixed value. diff --git a/src/epub-spacing-fixer.rb b/src/epub-spacing-fixer.rb new file mode 100755 index 0000000..2e107a4 --- /dev/null +++ b/src/epub-spacing-fixer.rb @@ -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 < + +-h, --help show this message +-n do nothing +-o 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 diff --git a/test/Openstreetmap.epub b/test/Openstreetmap.epub new file mode 100644 index 0000000..5a619d6 Binary files /dev/null and b/test/Openstreetmap.epub differ