ebook-index/index.rb

40 lines
634 B
Ruby
Raw Normal View History

2017-10-22 21:38:57 +00:00
require 'gepub'
require 'erb'
# Parse epubs
books = Dir["books/*.epub"].map do |f|
[f, GEPUB::Book.parse(File.new f)]
end
books = books.to_h
# 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
# Generate 'index.html' from template
bind = binding
bind.local_variable_set(:books, books)
template = File.read 'index.html.erb'
result = ERB.new(template).result(bind)
File.open "index.html", "w+" do |f|
f << result
end