superject/cards/preprocess.rb

120 lines
3.3 KiB
Ruby
Executable file

#!/usr/bin/env ruby -w
require 'optparse'
require 'ostruct'
require 'pp'
require 'roo'
# 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
options = Optparsec.parse(ARGV)
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
# assuming an .ods file laid out as follows
#
# col 1 - occasions
# col 2 - qualifiers
# col 3 - verbs
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
# preamble...
File.open($cards_csv, "w")
File.write($cards_csv, "name,type,text,illustration\n", mode: "w")
# Occasions
def write_occasion(line)
File.write($cards_csv, "#{line},O,...,img/o_card.svg\n", mode: "a")
end
# Qualifiers
def write_qualifier(line)
File.write($cards_csv, "#{line},Q,...,img/q_card.svg\n", mode: "a")
end
# Verbs
def write_verb(line)
File.write($cards_csv, "#{line},V,...,img/v_card.svg\n", mode: "a")
end
# read descriptions and write to file...
case options.input_type
when "txt"
puts "reading descriptions from text files" if options.verbose
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"
puts "reading descriptions from ods file: " + options.input_file if options.verbose
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."
exit
end
puts "written out to file: " + options.output_file if options.verbose