require 'optparse' require 'epub/parser' options = {:format => :line} opt = OptionParser.new do |opt| opt.banner = < [book.title]} data.merge!(book.metadata.to_h) data['Unique identifier'] = [book.metadata.unique_identifier] data['EPUB Version'] = [book.package.version] counts = {:chars => 0, :words => 0} if options[:words] or options[:chars] book.resources.select(&:xhtml?).each do |xhtml| begin doc = xhtml.content_document.nokogiri body = doc.search('body').first content = body.content if body counts[:words] += content.scan(/\S+/).length counts[:chars] += content.gsub(/\r|\n/, '').length end rescue => error warn "#{xhtml.href}: #{error}" end end end data['Words'] = [counts[:words]] if options[:words] data['Characters'] = [counts[:chars]] if options[:chars] if options[:format] == :line key_width = data.keys.map {|k| k.length}.max + 3 data.each_pair do |k, v| puts (k.to_s.capitalize + ':').ljust(key_width) + v.join(', ') end else require options[:format].to_s puts data.__send__("to_#{options[:format]}") end