-
Notifications
You must be signed in to change notification settings - Fork 122
/
generate_monster_json.rb
executable file
·63 lines (50 loc) · 1.87 KB
/
generate_monster_json.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#!/usr/bin/env ruby
# frozen_string_literal: true
require 'json'
require 'redcarpet'
# Value object for each monster in the list
class Monster
def initialize(markdown)
@markdown = markdown
end
attr_reader :markdown
# Search for the first line that matches the header pattern (#)
def name
header_line = markdown.find { |line| line.match(/^#\s*(.*)/) }
header_line&.match(/^#\s*(.*)/)&.send(:[], 1)&.strip
end
# Extracts the stats like HP, STR, DEX, WIL, armor, and all attacks from the markdown
def stats
stats_line = markdown.find { |line| line.match(/\d+ HP/) }
return {} unless stats_line
# Extract HP, Armor, STR, DEX, WIL
hp = stats_line.match(/(\d+)\s*HP/)&.send(:[], 1)
armor = stats_line.match(/(\d+)\s*Armor/)&.send(:[], 1) # Capture the Armor value if present
str = stats_line.match(/(\d+)\s*STR/)&.send(:[], 1)
dex = stats_line.match(/(\d+)\s*DEX/)&.send(:[], 1)
wil = stats_line.match(/(\d+)\s*WIL/)&.send(:[], 1)
# Extract all attacks (e.g., "ceremonial dagger (d6), club (d10)")
attacks = stats_line.scan(/([a-zA-Z\s]+)\s*\((.*?)\)/).map do |attack, damage|
{ name: attack.strip, damage: damage }
end
{ hp: hp, armor: armor, str: str, dex: dex, wil: wil, attacks: attacks }
end
def description
renderer = Redcarpet::Render::HTML.new
redcarpet = Redcarpet::Markdown.new(renderer)
# The description starts after the metadata and the header
redcarpet.render(markdown.drop_while { |line| !line.match(/^#/) }.drop(1).join)
end
end
# Process all markdown files in the "monsters" folder
monsters = Dir.glob('monsters/*.md').map do |monster_file|
markdown = File.readlines(monster_file)
monster = Monster.new(markdown)
{
name: monster.name,
description: monster.description,
stats: monster.stats
}
end.to_json
# Output the JSON
puts JSON.pretty_generate(JSON.parse(monsters))