2017-10-22 21:38:57 +00:00
|
|
|
require 'gepub'
|
|
|
|
require 'erb'
|
2017-10-26 14:23:24 +00:00
|
|
|
require 'optparse'
|
|
|
|
require 'pathname'
|
2017-10-22 21:38:57 +00:00
|
|
|
|
|
|
|
# Parse epubs
|
|
|
|
|
2017-10-26 14:23:24 +00:00
|
|
|
options = {:subdir => "books"}
|
|
|
|
OptionParser.new do |opts|
|
|
|
|
opts.banner = "Usage: index.rb [options]"
|
|
|
|
|
|
|
|
opts.on("-d", "--dir DIR", "directory to index (subdir specified with '-s' will be indexed)") do |d|
|
|
|
|
options[:dir] = d
|
|
|
|
end
|
2017-10-22 21:38:57 +00:00
|
|
|
|
2017-10-26 14:23:24 +00:00
|
|
|
opts.on("-s", "--subdir DIR", "subdirectory to search for books") do |s|
|
|
|
|
options[:subdir] = s
|
|
|
|
end
|
|
|
|
end.parse!
|
2017-10-22 21:38:57 +00:00
|
|
|
|
2017-10-26 14:23:24 +00:00
|
|
|
raise OptionParser::MissingArgument unless options[:dir]
|
|
|
|
|
|
|
|
books = Dir["#{options[:dir]}/#{options[:subdir]}/*.epub"].map do |f|
|
|
|
|
[Pathname.new(f).relative_path_from(Pathname.new(options[:dir])), GEPUB::Book.parse(File.new f)]
|
|
|
|
end.to_h
|
2017-10-22 21:38:57 +00:00
|
|
|
|
|
|
|
# Collection of sort_by procs
|
|
|
|
|
|
|
|
author_surname_sort_by = proc { |filename, book|
|
|
|
|
book.creator.content.split(" ")[-1]
|
|
|
|
}
|
|
|
|
|
|
|
|
title_sort_by = proc { |filename, book|
|
|
|
|
book.title.content
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
# Sort books by one of the above
|
|
|
|
|
|
|
|
books = books.sort_by &title_sort_by
|
|
|
|
|
2017-10-26 14:23:24 +00:00
|
|
|
i = 0
|
|
|
|
book_ids = books.map { |filename, book| [filename, i += 1] }.to_h
|
|
|
|
i = nil
|
2017-10-22 21:38:57 +00:00
|
|
|
|
|
|
|
# Generate 'index.html' from template
|
|
|
|
|
|
|
|
bind = binding
|
|
|
|
bind.local_variable_set(:books, books)
|
|
|
|
|
2017-10-26 14:23:24 +00:00
|
|
|
html_template = File.read 'index.html.erb'
|
|
|
|
|
|
|
|
html_result = ERB.new(html_template).result(bind)
|
|
|
|
File.open "#{options[:dir]}/index.html", "w+" do |f|
|
|
|
|
f << html_result
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
# Generate 'books.csv'
|
2017-10-22 21:38:57 +00:00
|
|
|
|
2017-10-26 14:23:24 +00:00
|
|
|
File.open "#{options[:dir]}/books.csv", "w+" do |f|
|
|
|
|
f << "id\ttitle\tauthor\tfilename\n"
|
|
|
|
books.each do |filename, book|
|
|
|
|
f << "#{book_ids[filename]}\t"
|
|
|
|
f << "#{book.title.content}\t"
|
|
|
|
f << "#{book.creator.content}\t"
|
|
|
|
f << "#{filename}\t"
|
|
|
|
f << "\n"
|
|
|
|
end
|
2017-10-22 21:38:57 +00:00
|
|
|
end
|