Sha256: 2230b9435eed9375330bc1308ca6a96873ab7bf870917651b2e3c8e495c66eb8
Contents?: true
Size: 1.79 KB
Versions: 8
Compression:
Stored size: 1.79 KB
Contents
require 'optparse' require 'epub/parser' options = {:format => :line} opt = OptionParser.new do |opt| opt.banner = <<EOB Show metadata of an EPUB file Usage: epubinfo [options] EPUBFILE EOB opt.version = EPUB::Parser::VERSION formats = [:line, :json, :yaml] opt.on '-f', '--format=FORMAT', formats, "format of output(#{formats[0..-2].join(', ')} or #{formats.last}), defaults to line(for console)" do |format| options[:format] = format end opt.on '--words', 'count words of content documents' do options[:words] = true end opt.on '--chars', 'count charactors of content documents' do options[:chars] = true end end opt.parse!(ARGV) file = ARGV.shift unless file $stdout.puts "error: you must supply an EPUB file name" $stdout.puts opt.help abort end book = EPUB::Parser.parse(file) data = {'Title' => [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
Version data entries
8 entries across 8 versions & 2 rubygems