Skip to content

Instantly share code, notes, and snippets.

@Nephos
Created May 22, 2020 07:51
Show Gist options
  • Save Nephos/0f2dc90500ca977a4063f1a531391e51 to your computer and use it in GitHub Desktop.
Save Nephos/0f2dc90500ca977a4063f1a531391e51 to your computer and use it in GitHub Desktop.
Conversion d'une liste de sort (copié collé de pathfinder wiki) en csv
# coding: utf-8
require "json"
require "csv"
txt = File.readlines('spells.txt')
spells = []
current_level = 0
current_school = "none"
txt.each do |line|
if line.match? /^Sorts d.+ \d+$/
current_level = line.split(' ')[-1].to_i
STDERR.puts "Add spells level #{current_level}"
# spells[current_level] ||= {}
elsif line.match? /^[^[[:space:]]]/
current_school = line.chomp
STDERR.puts "Add spells level #{current_level} school #{current_school}"
# spells[current_level][current_school] ||= []
elsif line.match? /^\s+$/
elsif line.match? /^\s+/
match = line.match /^\s+([^\.]+)\. (.+)/
match = [line, line, line] if !match
name = match[1].strip
description = match[2].strip
realname = name.gsub(/ \([A-Z, ]+\)/im, '')
url = "https://www.pathfinder-fr.org/Wiki/Pathfinder-RPG.#{realname}.ashx"
STDERR.puts "Add spell [#{current_level}][#{current_school}] #{realname}"
# spells[current_level][current_school] << { name: name, description: description, url: url }
spells << { level: current_level, school: current_school, name: name, description: description, url: url }
else
raise "Invalid line #{line}"
end
end
csv_string = CSV.generate do |csv|
csv << ["Préparé", "Connu", "Niveau", "École", "Nom", "Description", "Url"]
spells.each do |spell|
csv << (["", ""] + spell.values)
end
end
# puts spells.to_json
puts csv_string
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment