dir to be indexed is now an option to index.rb
This commit is contained in:
parent
b8c449d9c9
commit
a6a5c9bea0
7
.gitignore
vendored
Normal file
7
.gitignore
vendored
Normal file
|
@ -0,0 +1,7 @@
|
|||
.bundle/
|
||||
Gemfile.lock
|
||||
books.csv
|
||||
books/
|
||||
index.html
|
||||
tmp/
|
||||
vendor/
|
1
books.csv.erb
Normal file
1
books.csv.erb
Normal file
|
@ -0,0 +1 @@
|
|||
<% books.each do |b| %>
|
47
index.rb
47
index.rb
|
@ -1,14 +1,28 @@
|
|||
require 'gepub'
|
||||
require 'erb'
|
||||
require 'optparse'
|
||||
require 'pathname'
|
||||
|
||||
# Parse epubs
|
||||
|
||||
books = Dir["books/*.epub"].map do |f|
|
||||
[f, GEPUB::Book.parse(File.new f)]
|
||||
end
|
||||
options = {:subdir => "books"}
|
||||
OptionParser.new do |opts|
|
||||
opts.banner = "Usage: index.rb [options]"
|
||||
|
||||
books = books.to_h
|
||||
opts.on("-d", "--dir DIR", "directory to index (subdir specified with '-s' will be indexed)") do |d|
|
||||
options[:dir] = d
|
||||
end
|
||||
|
||||
opts.on("-s", "--subdir DIR", "subdirectory to search for books") do |s|
|
||||
options[:subdir] = s
|
||||
end
|
||||
end.parse!
|
||||
|
||||
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
|
||||
|
||||
# Collection of sort_by procs
|
||||
|
||||
|
@ -25,15 +39,32 @@ title_sort_by = proc { |filename, book|
|
|||
|
||||
books = books.sort_by &title_sort_by
|
||||
|
||||
i = 0
|
||||
book_ids = books.map { |filename, book| [filename, i += 1] }.to_h
|
||||
i = nil
|
||||
|
||||
# Generate 'index.html' from template
|
||||
|
||||
bind = binding
|
||||
bind.local_variable_set(:books, books)
|
||||
|
||||
template = File.read 'index.html.erb'
|
||||
html_template = File.read 'index.html.erb'
|
||||
|
||||
result = ERB.new(template).result(bind)
|
||||
File.open "index.html", "w+" do |f|
|
||||
f << result
|
||||
html_result = ERB.new(html_template).result(bind)
|
||||
File.open "#{options[:dir]}/index.html", "w+" do |f|
|
||||
f << html_result
|
||||
end
|
||||
|
||||
|
||||
# Generate 'books.csv'
|
||||
|
||||
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
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue
Block a user