#!/usr/bin/env ruby
# Take a file as first argument, which is to be the file biblio.csv, containing
# all books read. Then formats this as a table. It sorts the books in
# the order [Ongoing, Completed, Yet to Read], and replaces several values with
# ones more appropriate for viewing pleasure.
# For the final column (index: 11, zero-indexed) we replace the contents with a
# link. So rd/the-progress-of-a-crime becomes
# For column 3 (Part), this is rounded as special logic.
# \n"
exit
end
# def do_header(h: str): str
# Returns the rows of the table corresponding to the header.
def do_header(h)
s = "\t\n"
values = h.split($delim)
values.each_with_index do |value, idx|
next if idx == 0 or idx == 7 or idx == 8 or idx == 9
s << ("\t\t \n"
return s
end
# Make the string look sexy, ooh la la.
def sexify(val)
# Round integers if they end .00.
if val.end_with? ".00" then
integral = val.to_i
unless integral == 0
return integral.to_s
end
end
# Transform strings.
if $status_transform.has_key? (val)
return $status_transform[val]
end
if $form_transform.has_key? (val)
return $form_transform[val]
end
if $ownership_transform.has_key? (val)
return $ownership_transform[val]
end
if $general_transform.has_key? (val)
return $general_transform[val]
end
return val
end
# The tables, which are set as targets during the look
table=""
table_ongoing=""
table_cmp="" # Just contains completed entries.
table_paused=""
table_drp=""
table_rest=""
table_coll=""
# And the counts, for adding in counts if I want
ctable_ongoing=0
ctable_cmp=0
ctable_paused=0
ctable_drp=0
ctable_rest=0
ctable_coll=0
# And the header, don't forget!
header=""
File.foreach(ARGV[0]).with_index do |line, line_num|
next if line == "\n" or line == "\t" or line == "" or line == " " or line == $delim
# Handle the first line - the header.
if line_num == 0 then
header << do_header(line)
else
# We need to figure out whether it is ongoing, completed, or yet to read.
# To do this, we will check if the corresponding string is in the line.
if line.include? "completed;" then
target = table_cmp
ctable_cmp += 1
elsif line.include? "ongoing;" then
target = table_ongoing
ctable_ongoing += 1
elsif line.include? "paused;" then
target = table_paused
ctable_paused += 1
elsif line.include? "dropped;" then
target = table_drp
ctable_drp += 1
elsif line.include? "collection;" then
target = table_coll
ctable_coll += 1
else
target = table_rest
ctable_rest += 1
end
target << "\t" + value + " \n")
end
s << "\t\n"
line.split($delim).each_with_index do |entry, idx|
# We want to skip: Status, Ownership, Bought, Cost as they just take up
# space. Here we are skipping by the relevant index.
next if idx == 0 or idx == 7 or idx == 8 or idx == 9
entry = entry.delete "\n"
if idx == 11 and entry != "N/A" then
# Review column: transform it to contain a link.
target << "\t\t \n"
end
end
# Instead of one table, split into Link \n"
else
target << "\t\t" \
<< sexify(entry) \
<< " \n"
end
end
target << "\tCurrently Reading
There are " << ctable_ongoing.to_s << " entries in this table.
\n" table << "There are " << ctable_cmp.to_s << " entries in this table.
\n" table << "There are " << ctable_paused.to_s << " entries in this table.
\n" table << "There are " << ctable_drp.to_s << " entries in this table.
\n" table << "There are " << ctable_rest.to_s << " entries in this table.
\n" table << "