From b8c449d9c98ff7cf5bcec96ad3d86eaa3fad58d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoffer=20M=C3=BCller=20Madsen?= Date: Sun, 22 Oct 2017 23:38:57 +0200 Subject: [PATCH] initial commit, it works --- index.html.erb | 27 +++++++++++++++++++++++++++ index.rb | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+) create mode 100644 index.html.erb create mode 100644 index.rb diff --git a/index.html.erb b/index.html.erb new file mode 100644 index 0000000..b4c5a20 --- /dev/null +++ b/index.html.erb @@ -0,0 +1,27 @@ + + + + + The Secret Book Lending Site + + + + + + + + + + + <% books.each do |f, b| %> + + + + + + <% end %> +
TitleAuthorLink
<%= b.title.content %><%= b.creator.content %>EPUB
+ + + + diff --git a/index.rb b/index.rb new file mode 100644 index 0000000..eb93c1b --- /dev/null +++ b/index.rb @@ -0,0 +1,39 @@ +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