2020-11-10 15:11:15 +00:00
|
|
|
#!/usr/bin/env ruby -w
|
2020-10-20 09:59:13 +00:00
|
|
|
|
2020-11-10 15:11:15 +00:00
|
|
|
require 'optparse'
|
|
|
|
require 'ostruct'
|
|
|
|
require 'pp'
|
|
|
|
require 'roo'
|
2020-10-20 09:59:13 +00:00
|
|
|
|
2020-11-10 15:11:15 +00:00
|
|
|
# command line options
|
|
|
|
class Optparsec
|
|
|
|
# Return a structure describing the options.
|
|
|
|
def self.parse(args)
|
|
|
|
# The options specified on the command line will be collected in *options*.
|
|
|
|
# set default values here.
|
|
|
|
options = OpenStruct.new
|
|
|
|
options.input_file = "cards.ods"
|
|
|
|
options.output_file = "cards.csv"
|
|
|
|
options.input_type = "ods"
|
|
|
|
options.verbose = true
|
|
|
|
|
|
|
|
opt_parser = OptionParser.new do |opts|
|
|
|
|
opts.banner = "Usage: example.rb [options]"
|
|
|
|
opts.separator ""
|
|
|
|
opts.separator "Specific options:"
|
|
|
|
|
|
|
|
opts.on("-i", "--input FILE", "Input file") do |i|
|
|
|
|
options.input_file = i
|
|
|
|
end
|
|
|
|
opts.on("-o", "--output FILE", "Output file (csv)") do |i|
|
|
|
|
options.input_file = i
|
|
|
|
end
|
|
|
|
# type of input file (can also check extension)
|
|
|
|
opts.on("-t", "--type [TYPE]", ["ods", "csv", "txt"],
|
|
|
|
"Input type (ods, csv or txt)") do |t|
|
|
|
|
options.input_type = t
|
|
|
|
end
|
|
|
|
opts.on("-v", "--[no-]verbose", "Verbosity") do |v|
|
|
|
|
options.verbose = v
|
|
|
|
end
|
|
|
|
# help and/or no opts
|
|
|
|
opts.on_tail("-h", "--help", "Show this message") do
|
|
|
|
puts opts
|
|
|
|
exit
|
|
|
|
end
|
|
|
|
end
|
|
|
|
opt_parser.parse!(args)
|
|
|
|
options
|
|
|
|
end # parse()
|
|
|
|
end # class OptparseExample
|
2020-11-10 12:16:59 +00:00
|
|
|
|
2020-11-10 15:11:15 +00:00
|
|
|
options = Optparsec.parse(ARGV)
|
2020-10-20 09:59:13 +00:00
|
|
|
|
2020-11-10 15:11:15 +00:00
|
|
|
if options.verbose
|
|
|
|
puts "reading from file: " + options.input_file
|
|
|
|
puts "assuming input is: " + options.input_type
|
|
|
|
puts "results output to: " + options.output_file
|
|
|
|
puts "..."
|
|
|
|
end
|
2020-11-10 12:16:59 +00:00
|
|
|
|
|
|
|
# assuming an .ods file laid out as follows
|
|
|
|
#
|
|
|
|
# col 1 - occasions
|
|
|
|
# col 2 - qualifiers
|
|
|
|
# col 3 - verbs
|
|
|
|
|
2020-11-10 15:11:15 +00:00
|
|
|
case options.input_type
|
|
|
|
when "ods"
|
|
|
|
# all card descriptions in a single spreadsheet
|
|
|
|
ods = Roo::OpenOffice.new(options.input_file)
|
|
|
|
when "txt"
|
|
|
|
# card descriptions in textfiles
|
|
|
|
$cards_o = "cards-occasions.txt"
|
|
|
|
$cards_q = "cards-qualifiers.txt"
|
|
|
|
$cards_v = "cards-verbs.txt"
|
|
|
|
else
|
|
|
|
puts "input format not supported."
|
|
|
|
exit
|
|
|
|
end
|
|
|
|
|
|
|
|
# convert text files into .csv for deck.rb mogrification
|
|
|
|
$cards_csv = options.output_file
|
2020-11-10 12:16:59 +00:00
|
|
|
|
2020-10-20 09:59:13 +00:00
|
|
|
# preamble...
|
|
|
|
File.open($cards_csv, "w")
|
|
|
|
File.write($cards_csv, "name,type,text,illustration\n", mode: "w")
|
|
|
|
|
|
|
|
# Occasions
|
|
|
|
def write_occasion(line)
|
2020-10-20 10:12:49 +00:00
|
|
|
File.write($cards_csv, "#{line},O,...,img/o_card.svg\n", mode: "a")
|
2020-10-20 09:59:13 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# Qualifiers
|
|
|
|
def write_qualifier(line)
|
2020-10-20 10:12:49 +00:00
|
|
|
File.write($cards_csv, "#{line},Q,...,img/q_card.svg\n", mode: "a")
|
2020-10-20 09:59:13 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# Verbs
|
|
|
|
def write_verb(line)
|
2020-10-20 10:12:49 +00:00
|
|
|
File.write($cards_csv, "#{line},V,...,img/v_card.svg\n", mode: "a")
|
2020-10-20 09:59:13 +00:00
|
|
|
end
|
|
|
|
|
2020-11-10 15:11:15 +00:00
|
|
|
# read descriptions and write to file...
|
|
|
|
case options.input_type
|
2020-11-10 12:16:59 +00:00
|
|
|
when "txt"
|
2020-11-10 15:11:15 +00:00
|
|
|
puts "reading descriptions from text files" if options.verbose
|
2020-11-10 12:16:59 +00:00
|
|
|
File.foreach($cards_o) { |line| write_occasion line.chomp }
|
|
|
|
File.foreach($cards_q) { |line| write_qualifier line.chomp }
|
|
|
|
File.foreach($cards_v) { |line| write_verb line.chomp }
|
|
|
|
when "ods"
|
2020-11-10 15:11:15 +00:00
|
|
|
puts "reading descriptions from ods file: " + options.input_file if options.verbose
|
2020-11-10 12:16:59 +00:00
|
|
|
ods.column(1).each { |line| write_occasion line.chomp }
|
|
|
|
ods.column(2).each { |line| write_qualifier line.chomp }
|
|
|
|
ods.column(3).each { |line| write_verb line.chomp }
|
|
|
|
else
|
|
|
|
puts "input format not supported."
|
2020-11-10 15:11:15 +00:00
|
|
|
exit
|
2020-11-10 12:16:59 +00:00
|
|
|
end
|
2020-10-20 09:59:13 +00:00
|
|
|
|
2020-11-10 15:11:15 +00:00
|
|
|
puts "written out to file: " + options.output_file if options.verbose
|
|
|
|
|