#!/usr/bin/env ruby -w require 'optparse' require 'ostruct' require 'pp' require 'roo' # scrip to use to build the deck $deck_build = "./deck-neural.rb" # 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.deck_label = "..." 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 # 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 # 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 opts.on("-b", "--build", "Build a deck after pre.processing") do |b| options.build = b 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 - lures 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") # optional deck labels $label = options.deck_label # Occasions (with text escaping...) def write_occasion(line) File.write($cards_csv, "\"" + %Q[#{line}] + "\"" + ",O,#{$label},img/O_card.svg\n", mode: "a") end # Qualifiers def write_qualia(line) File.write($cards_csv, "\"" + %Q[#{line}] + "\"" + ",Q,#{$label},img/Q_card.svg\n", mode: "a") #File.write($cards_csv, "#{line},Q,#{$label},img/q_card.svg\n", mode: "a") end # Verbs def write_lure(line) File.write($cards_csv, "\"" + %Q[#{line}] + "\"" + ",L,#{$label},img/L_card.svg\n", mode: "a") #File.write($cards_csv, "#{line},L,#{$label},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_qualia line.chomp } File.foreach($cards_v) { |line| write_lure line.chomp } when "ods" 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_qualia line.chomp end ods.column(3).reject{ |n| n.nil?}.each do |line| puts "3> " + line if options.verbose; write_lure line.chomp end else puts "input format not supported." exit end puts "written out to file: " + options.output_file if options.verbose if options.build puts "building a deck with #{$deck_build}" system($deck_build) # run the deck builder else puts "not building a deck" end puts "done" if options.verbose