-
Notifications
You must be signed in to change notification settings - Fork 0
/
presets-compiler.rb
executable file
·230 lines (184 loc) · 5.45 KB
/
presets-compiler.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
require 'bundler/inline'
gemfile do
source 'https://rubygems.org'
gem 'pry'
gem 'nokogiri'
end
class PresetBuilder
DEFAULT_CLOCK_LENGTH = 8
DEFAULT_SEND_MICROPHONE = true
DEFAULT_SEND_KEYBOARD = false
DEFAULT_SEND_GUITAR = true
def initialize(file)
@file = file
@step = 1
@result = []
@title = nil
@clockLength = DEFAULT_CLOCK_LENGTH
@sendMicrophone = DEFAULT_SEND_MICROPHONE
@sendKeyboard = DEFAULT_SEND_KEYBOARD
@sendGuitar = DEFAULT_SEND_GUITAR
@nextMicrophonePreset = nil
@nextKeyboardPreset = nil
@nextGuitarPreset = nil
process
end
def title
@title
end
def code
@result.map {|line| line = " #{line}"}.join("\n")
end
def tmp_file
"#{@file}.html"
end
def process
convert_md_to_html
generate_mozaic_script
tear_down
end
def tear_down
puts `unlink #{tmp_file}`
end
def convert_md_to_html
puts `pandoc -s -o #{tmp_file} #{@file} --metadata title="No title"`
end
def generate_mozaic_script
@doc = File.open(tmp_file) { |f| Nokogiri::Slop(f) }
@title = @doc.html.body.h1.text
@doc.html.body.h2.each_with_index do |h2, i|
@result << "Log {#{h2.text}}"
li = @doc.html.body.ul[i].li
li = [li] if li.count == 0 # See https://stackoverflow.com/questions/65576289/
li.each do |li|
# next if li.is_a?(Array) # See https://stackoverflow.com/questions/65576168/
part = li.children.reject(&:element?).first.text.strip # Text of first <li> element
settings = li.ul.li.select(&:element?).map(&:text) # All texts of contained <li> elements (2nd level list items)
generate_prepare part, settings
generate_activate unless @step == 2 # The very first song part will be activated immediately
end
@result << ""
end
@result << ' Call @Error'
@result << "endif"
end
def generate_prepare(part, settings)
@result << "#{:else if @step > 1}if step = #{@step}"
@result << " Log { #{part}}"
settings.each_with_index do |setting, i|
if codes = convert_setting_to_code(setting)
codes.each do |code|
@result << " #{'Call @' unless code.start_with? 'Log'}" + code
end
@result << nil if codes.size > 1 && i != codes.size # Add a line break
end
end
@step += 1
end
def convert_setting_to_code(setting)
codes = []
case setting
when /🎤\s?❌/
if @sendMicrophone
@sendMicrophone = false
codes << 'ToggleSendMicrophone' # TODO: Should not happen immediately!
codes << 'Log { 🎤 ❌}'
end
when /🎤\s?✔️/
if !@sendMicrophone
@sendMicrophone = true
@nextMicrophonePreset = 1
end
when /🎸\s?([1-3])\s?(✔️)?/
codes << "PrepareGuitarPreset#{$1}"
codes << "Log { 🎸 #{$1}}"
@nextGuitarPreset = $1
if $2 == '✔️'
@sendGuitar = true
codes << 'ToggleSendGuitar'
codes << 'Log { 🎸 ✔️}'
@nextGuitarPreset = nil
end
when /🎹\s?([1-3])\s?(✔️)?/
codes << "PrepareKeyboardPreset#{$1}"
codes << "Log { 🎹 #{$1}}"
@nextKeyboardPreset = $1
if $2 == '✔️'
@sendKeyboard = true
codes << 'ToggleSendKeyboard'
codes << 'Log { 🎹 ✔️}'
@nextKeyboardPreset = nil
end
when /⏲️\s?(\d+)/
while @clockLength != $1.to_i
if @clockLength < $1.to_i
@clockLength += 1
codes << 'IncreaseClockLength'
else
@clockLength -= 1
codes << "DecreaseClockLength"
end
codes << "Log { ⏲️ #{@clockLength}}"
end
when /⏺️\s?([0-4])/
codes << "RecordNextLoopInGroup#{$1}"
codes << "Log { ⏺️ #{$1}}"
when /▶️\s?([1-4])/
codes << "ToggleAndSelectGroup#{$1}"
codes << "Log { ▶️ #{$1}}"
else
puts "Unknown setting #{setting}, assuming it's just text."
end
codes
end
def generate_activate
@result << "#{:else if @step > 1}if step = #{@step}"
if @nextMicrophonePreset
@result << ' Call @ToggleSendMicrophone'
@result << ' Log { 🎤 ✔️}'
@nextMicrophonePreset = nil
end
if @nextKeyboardPreset
@result << ' Call @ToggleSendKeyboard'
@result << ' Log { 🎹 ✔️}'
@nextKeyboardPreset = nil
end
if @nextGuitarPreset
@result << ' Call @ToggleSendGuitar'
@result << ' Log { 🎸 ✔️}'
@nextGuitarPreset = nil
end
@step += 1
end
end
class PresetsCompiler
def initialize
@presets = []
process
print_script_to_file
end
def process
Dir["presets/*.md"].sort.each do |file|
@presets << PresetBuilder.new(file)
end
end
def print_script_to_file
result = []
template = File.open("mozaic/1b-preset-mode").read
template.gsub! /presetsSize = \d+/, "presetsSize = #{@presets.size}"
@presets.each_with_index do |preset, i|
result << " //////////////////"
result << " // #{preset.title}"
result << " //////////////////"
result << " #{:else if i > 0}if preset = #{i + 1}"
result << preset.code
result << ""
end
result << " endif"
template.gsub! /\n@ProceedWithPreset\n(.*?)\n@End\n/m, "\n@ProceedWithPreset\n#{result.join("\n")}\n@End\n"
file = File.new("mozaic/1b-preset-mode", "w")
file.puts(template)
file.close
end
end
PresetsCompiler.new