initial commit, it works

This commit is contained in:
Christoffer Müller Madsen 2017-10-22 23:38:57 +02:00
commit b8c449d9c9
Signed by: christoffer
GPG Key ID: 337BA5A95E686EFD
2 changed files with 66 additions and 0 deletions

27
index.html.erb Normal file
View File

@ -0,0 +1,27 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>The Secret Book Lending Site</title>
</head>
<body>
<table>
<tr>
<th>Title</th>
<th>Author</th>
<th>Link</th>
</tr>
<% books.each do |f, b| %>
<tr>
<td><%= b.title.content %></td>
<td><%= b.creator.content %></td>
<td><a href="<%= f %>">EPUB</a></td>
</tr>
<% end %>
</table>
</body>
</html>

39
index.rb Normal file
View File

@ -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