Module: SongPro
- Defined in:
- lib/song_pro.rb,
lib/song_pro/line.rb,
lib/song_pro/part.rb,
lib/song_pro/song.rb,
lib/song_pro/measure.rb,
lib/song_pro/section.rb,
lib/song_pro/version.rb
Defined Under Namespace
Classes: Line, Measure, Part, Section, Song
Constant Summary
collapse
- SECTION_REGEX =
/#\s*([^$]*)/
- ATTRIBUTE_REGEX =
/@(\w*)=([^%]*)/
- CUSTOM_ATTRIBUTE_REGEX =
/!(\w*)=([^%]*)/
- CHORDS_AND_LYRICS_REGEX =
%r{(\[[\w#b\/]+\])?([^\[]*)}i
- MEASURES_REGEX =
%r{([\[[\w#b\/]+\]\s]+)[|]*}i
- CHORDS_REGEX =
%r{\[([\w#b\/]+)\]?}i
/>\s*([^$]*)/
- VERSION =
'0.1.8'.freeze
Class Method Summary
collapse
Class Method Details
.parse(lines) ⇒ Object
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
# File 'lib/song_pro.rb', line 20
def self.parse(lines)
song = Song.new
current_section = nil
lines.split("\n").each do |text|
if text.start_with?('@')
process_attribute(song, text)
elsif text.start_with?('!')
process_custom_attribute(song, text)
elsif text.start_with?('#')
current_section = process_section(song, text)
else
process_lyrics_and_chords(song, current_section, text)
end
end
song
end
|
.process_attribute(song, text) ⇒ Object
48
49
50
51
52
53
54
55
56
57
58
|
# File 'lib/song_pro.rb', line 48
def self.process_attribute(song, text)
matches = ATTRIBUTE_REGEX.match(text)
key = matches[1]
value = matches[2].strip
if song.respond_to?("#{key}=".to_sym)
song.send("#{key}=", value)
else
puts "WARNING: Unknown attribute '#{key}'"
end
end
|
.process_custom_attribute(song, text) ⇒ Object
60
61
62
63
64
65
66
|
# File 'lib/song_pro.rb', line 60
def self.process_custom_attribute(song, text)
matches = CUSTOM_ATTRIBUTE_REGEX.match(text)
key = matches[1]
value = matches[2].strip
song.set_custom(key, value)
end
|
.process_lyrics_and_chords(song, current_section, text) ⇒ Object
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
|
# File 'lib/song_pro.rb', line 68
def self.process_lyrics_and_chords(song, current_section, text)
return if text == ''
if current_section.nil?
current_section = Section.new(name: '')
song.sections << current_section
end
line = Line.new
if text.start_with?('|-')
line.tablature = text
elsif text.start_with?('| ')
captures = text.scan(MEASURES_REGEX).flatten
measures = []
captures.each do |capture|
chords = capture.scan(CHORDS_REGEX).flatten
measure = Measure.new
measure.chords = chords
measures << measure
end
line.measures = measures
elsif text.start_with?('>')
matches = .match(text)
= matches[1].strip
line. =
else
captures = text.scan(CHORDS_AND_LYRICS_REGEX).flatten
captures.each_slice(2) do |pair|
part = Part.new
chord = pair[0]&.strip || ''
part.chord = chord.delete('[').delete(']')
part.lyric = pair[1] || ''
line.parts << part unless (part.chord == '') && (part.lyric == '')
end
end
current_section.lines << line
end
|
.process_section(song, text) ⇒ Object
39
40
41
42
43
44
45
46
|
# File 'lib/song_pro.rb', line 39
def self.process_section(song, text)
matches = SECTION_REGEX.match(text)
name = matches[1].strip
current_section = Section.new(name: name)
song.sections << current_section
current_section
end
|