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"
|
2020-11-11 00:22:18 +00:00
|
|
|
options.deck_label = "..."
|
2020-11-10 15:11:15 +00:00
|
|
|
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
|
2020-11-11 00:22:18 +00:00
|
|
|
# a text label for all cards in the deck
|
|
|
|
opts.on("-l", "--label TEXT", "Text label to appear on each card") do |i|
|
|
|
|
options.deck_label = i
|
|
|
|
end
|
2020-11-10 15:11:15 +00:00
|
|
|
# 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
|
2020-11-11 00:22:18 +00:00
|
|
|
opts.on("-b", "--build", "Build a deck after pre.processing") do |b|
|
|
|
|
options.build = b
|
|
|
|
end
|
2020-11-10 15:11:15 +00:00
|
|
|
# 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")
|
|
|
|
|
2020-11-11 00:22:18 +00:00
|
|
|
# optional deck labels
|
|
|
|
$label = options.deck_label
|
|
|
|
|
2020-11-11 12:44:35 +00:00
|
|
|
# Occasions (with text escaping...)
|
2020-10-20 09:59:13 +00:00
|
|
|
def write_occasion(line)
|
2020-11-11 12:44:35 +00:00
|
|
|
File.write($cards_csv, "\"" + %Q[#{line}] + "\"" + ",O,#{$label},img/o_card.svg\n", mode: "a")
|
2020-10-20 09:59:13 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# Qualifiers
|
|
|
|
def write_qualifier(line)
|
2020-11-11 00:22:18 +00:00
|
|
|
File.write($cards_csv, "#{line},Q,#{$label},img/q_card.svg\n", mode: "a")
|
2020-10-20 09:59:13 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# Verbs
|
|
|
|
def write_verb(line)
|
2020-11-11 00:22:18 +00:00
|
|
|
File.write($cards_csv, "#{line},V,#{$label},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-11 12:44:35 +00:00
|
|
|
if options.verbose
|
|
|
|
puts "reading descriptions from ods file: " + options.input_file
|
|
|
|
puts ods.info
|
|
|
|
end
|
|
|
|
# filter empty cells
|
|
|
|
ods.column(1).reject{ |n| n.nil?}.each do |line|
|
|
|
|
puts "1> " + line if options.verbose; write_occasion line.chomp
|
|
|
|
end
|
|
|
|
ods.column(2).reject{ |n| n.nil?}.each do |line|
|
|
|
|
puts "2> " + line if options.verbose; write_qualifier line.chomp
|
|
|
|
end
|
|
|
|
ods.column(3).reject{ |n| n.nil?}.each do |line|
|
|
|
|
puts "3> " + line if options.verbose; write_verb line.chomp
|
|
|
|
end
|
2020-11-10 12:16:59 +00:00
|
|
|
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
|
|
|
|
|
2020-11-11 00:22:18 +00:00
|
|
|
if options.build
|
|
|
|
puts "building..."
|
|
|
|
system("./deck.rb") # run the deck builder
|
|
|
|
else
|
|
|
|
puts "not"
|
|
|
|
end
|
|
|
|
|
|
|
|
puts "done" if options.verbose
|