MetalAdventures/bin/skill_convert.rb

78 lines
2.3 KiB
Ruby
Executable File

#!/usr/bin/env ruby
# Argv 0: text filepath to read (input)
# Argv 1: directory where files will be written (output, default=/tmp)
require "ostruct"
require "active_support/all"
require "pry"
require "yaml"
class Skill < Struct.new(:name, :ref, :description, :long, :skill, :duration, :_desc_passed)
def to_h
super.except(:_desc_passed).transform_keys(&:to_s)
end
end
skills = []
next_joiner = " "
joiner = " "
ref = "MJ 114" # NOTE: I inserted the refs manually in the text with REF:MJ 114 etc. as a new line
state = nil
lines = File.readlines(ARGV[0]).map(&:chomp)
state = Skill.new
lines.each_with_index do |line, _index|
if line.empty?
skills << state if state.name
state = Skill.new
state.ref = ref
next_joiner = " "
elsif line == ">>"
skills << state if state.name
state = Skill.new
state.ref = ref
next_joiner = " "
elsif (match = line.match(/^REF:(.+)/))
ref = match[1]
elsif state.name.nil? && (match = line.match(/>>(.+)/))
skills << state if state.name
state = Skill.new
state.ref = ref
next_joiner = " "
state.name = match[1].strip
elsif (match = line.match(/^Durée *: *Action (complexe|simple)/i))
state.duration = match[1][0].upcase
elsif (match = line.match(/^Compétence *: *(.+)/))
state.skill = match[1]
elsif state.name.nil?
state.name = line.strip
else
joiner = next_joiner
if line.end_with?("-")
line = line[0..-2]
next_joiner = ""
else
next_joiner = line.size < 50 ? "\n" : " "
end
joiner = "\n" if line.start_with?(/>>/)
state._desc_passed = true if !state._desc_passed && line.match?(/cette (action|man)/i)
# binding.pry if state.name == "Virer [Pilote]"
# binding.pry if line.start_with?("de vol et vous livrez ")
if state._desc_passed
state.long = [state.long, line].compact.join(joiner)
else
state.description = [state.description, line].compact.join(joiner)
end
end
end
skills << state if state.name
# puts skills.map { _1.transform_keys(&:to_s) }.to_yaml
skills.each do |skill|
canon = skill.name.gsub(/\[.+/, '').downcase.gsub(/[^[:alnum:]]+/, '_').chomp("_")
filename = "#{canon}.yaml"
dir = ARGV[1] || "/tmp"
filepath = File.join(dir, filename)
File.write(filepath, skill.to_h.to_yaml)
puts filepath
end