Skip to content

Commit 1ea755f

Browse files
committed
fix linting using standardrb
1 parent 6b18bda commit 1ea755f

9 files changed

Lines changed: 258 additions & 257 deletions

File tree

Rakefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# frozen_string_literal: true
22

3-
require 'bundler/gem_tasks'
4-
require 'rspec/core/rake_task'
3+
require "bundler/gem_tasks"
4+
require "rspec/core/rake_task"
55

66
RSpec::Core::RakeTask.new(:spec)
77

lib/chord_pro.rb

Lines changed: 98 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
# frozen_string_literal: true
22

3-
require 'chord_pro/version'
4-
require 'chord_pro/song'
5-
require 'chord_pro/section'
6-
require 'chord_pro/line'
7-
require 'chord_pro/part'
8-
require 'chord_pro/measure'
3+
require "chord_pro/version"
4+
require "chord_pro/song"
5+
require "chord_pro/section"
6+
require "chord_pro/line"
7+
require "chord_pro/part"
8+
require "chord_pro/measure"
99

1010
module ChordPro
1111
SECTION_REGEX = /\{(?:chorus|soc|sov|start_of_verse|start_of_chorus|sob|start_of_bridge|start_of_tab|sot|start_of_grid|sog):?\n?(.*)\}/m
@@ -18,121 +18,122 @@ module ChordPro
1818
COMMENT_REGEX = /\{(?:c|comment|comment_italic|ci|comment_box|cb):([^$]*)\}/
1919
SANITIZE_REGEX = /\{end_of_chorus|eoc|end_of_verse|eov|end_of_tab|eot|end_of_tab|eog|end_of_grid|colb\}/
2020

21-
def self.parse(lines)
22-
song = Song.new
23-
current_section = nil
24-
25-
lines.split("\n").each do |text|
26-
if text.start_with?('{meta:')
27-
process_custom_attribute(song, text)
28-
elsif section_start?(text)
29-
current_section = process_section(song, text)
30-
elsif !comment_starts?(text) && attribute_start?(text)
31-
process_attribute(song, text)
32-
elsif text.match(SANITIZE_REGEX)
33-
# ignore
34-
else
35-
process_lyrics_and_chords(song, current_section, text)
21+
class << self
22+
def parse(lines)
23+
song = Song.new
24+
current_section = nil
25+
26+
lines.split("\n").each do |text|
27+
if text.start_with?("{meta:")
28+
process_custom_attribute(song, text)
29+
elsif section_start?(text)
30+
current_section = process_section(song, text)
31+
elsif !comment_starts?(text) && attribute_start?(text)
32+
process_attribute(song, text)
33+
elsif text.match(SANITIZE_REGEX)
34+
# ignore
35+
else
36+
process_lyrics_and_chords(song, current_section, text)
37+
end
3638
end
39+
40+
song
3741
end
3842

39-
song
40-
end
43+
def process_section(song, text)
44+
matches = SECTION_REGEX.match(text)
45+
name = (!matches[1].empty?) ? matches[1].strip : section_name_by_directive(text)
4146

42-
def self.process_section(song, text)
43-
matches = SECTION_REGEX.match(text)
44-
name = !matches[1].empty? ? matches[1].strip : section_name_by_directive(text)
47+
current_section = Section.new(name: name)
48+
song.sections << current_section
4549

46-
current_section = Section.new(name: name)
47-
song.sections << current_section
50+
current_section
51+
end
4852

49-
current_section
50-
end
53+
def process_attribute(song, text)
54+
matches = ATTRIBUTE_REGEX.match(text)
55+
key = matches[1]
56+
value = matches[2].strip
57+
58+
if song.respond_to?(:"#{key}=")
59+
song.send(:"#{key}=", value)
60+
else
61+
puts "WARNING: Unknown attribute '#{key}'"
62+
end
63+
end
5164

52-
def self.process_attribute(song, text)
53-
matches = ATTRIBUTE_REGEX.match(text)
54-
key = matches[1]
55-
value = matches[2].strip
65+
def process_custom_attribute(song, text)
66+
matches = CUSTOM_ATTRIBUTE_REGEX.match(text)
67+
key = matches[1]
68+
value = matches[2].strip
5669

57-
if song.respond_to?("#{key}=".to_sym)
58-
song.send("#{key}=", value)
59-
else
60-
puts "WARNING: Unknown attribute '#{key}'"
70+
song.set_custom(key, value)
6171
end
62-
end
6372

64-
def self.process_custom_attribute(song, text)
65-
matches = CUSTOM_ATTRIBUTE_REGEX.match(text)
66-
key = matches[1]
67-
value = matches[2].strip
73+
def process_lyrics_and_chords(song, current_section, text)
74+
return if text == ""
6875

69-
song.set_custom(key, value)
70-
end
76+
if current_section.nil?
77+
current_section = Section.new(name: "")
78+
song.sections << current_section
79+
end
7180

72-
def self.process_lyrics_and_chords(song, current_section, text)
73-
return if text == ''
81+
line = Line.new
7482

75-
if current_section.nil?
76-
current_section = Section.new(name: '')
77-
song.sections << current_section
78-
end
83+
if text.start_with?("|-")
84+
line.tablature = text
85+
elsif text.start_with?("| ")
86+
captures = text.scan(MEASURES_REGEX).flatten
7987

80-
line = Line.new
88+
measures = []
8189

82-
if text.start_with?('|-')
83-
line.tablature = text
84-
elsif text.start_with?('| ')
85-
captures = text.scan(MEASURES_REGEX).flatten
90+
captures.each do |capture|
91+
chords = capture.scan(CHORDS_REGEX).flatten
92+
measure = Measure.new
93+
measure.chords = chords
94+
measures << measure
95+
end
8696

87-
measures = []
97+
line.measures = measures
98+
elsif comment_starts?(text)
99+
matches = COMMENT_REGEX.match(text)
100+
comment = matches[1].strip
101+
line.comment = comment
102+
else
103+
captures = text.scan(CHORDS_AND_LYRICS_REGEX).flatten
88104

89-
captures.each do |capture|
90-
chords = capture.scan(CHORDS_REGEX).flatten
91-
measure = Measure.new
92-
measure.chords = chords
93-
measures << measure
94-
end
105+
captures.each_slice(2) do |pair|
106+
part = Part.new
107+
chord = pair[0]&.strip || ""
108+
part.chord = chord.delete("[").delete("]")
109+
part.lyric = pair[1] || ""
95110

96-
line.measures = measures
97-
elsif comment_starts?(text)
98-
matches = COMMENT_REGEX.match(text)
99-
comment = matches[1].strip
100-
line.comment = comment
101-
else
102-
captures = text.scan(CHORDS_AND_LYRICS_REGEX).flatten
103-
104-
captures.each_slice(2) do |pair|
105-
part = Part.new
106-
chord = pair[0]&.strip || ''
107-
part.chord = chord.delete('[').delete(']')
108-
part.lyric = pair[1] || ''
109-
110-
line.parts << part unless (part.chord == '') && (part.lyric == '')
111+
line.parts << part unless (part.chord == "") && (part.lyric == "")
112+
end
111113
end
112-
end
113114

114-
current_section.lines << line unless line.empty?
115-
end
115+
current_section.lines << line unless line.empty?
116+
end
116117

117-
private
118+
private
118119

119-
def self.attribute_start?(text)
120-
text.match(/\{(.+):(.*)\}/)
121-
end
120+
def attribute_start?(text)
121+
text.match(/\{(.+):(.*)\}/)
122+
end
122123

123-
def self.section_start?(text)
124-
text.match(SECTION_REGEX)
125-
end
124+
def section_start?(text)
125+
text.match(SECTION_REGEX)
126+
end
126127

127-
def self.comment_starts?(text)
128-
text.match(/\{(?:c|comment|comment_italic|ci|comment_box|cb):/)
129-
end
128+
def comment_starts?(text)
129+
text.match(/\{(?:c|comment|comment_italic|ci|comment_box|cb):/)
130+
end
130131

131-
def self.section_name_by_directive(text)
132-
return 'Chorus' if text.match(/soc|start_of_chorus|chorus/)
133-
return 'Verse' if text.match(/sov|start_of_verse/)
134-
return 'Tab' if text.match(/sot|start_of_tab/)
135-
return 'Grid' if text.match(/sot|start_of_grid/)
132+
def section_name_by_directive(text)
133+
return "Chorus" if /soc|start_of_chorus|chorus/.match?(text)
134+
return "Verse" if /sov|start_of_verse/.match?(text)
135+
return "Tab" if /sot|start_of_tab/.match?(text)
136+
"Grid" if /sot|start_of_grid/.match?(text)
137+
end
136138
end
137-
138139
end

lib/chord_pro/section.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ module ChordPro
44
class Section
55
attr_accessor :name, :lines
66

7-
def initialize(name: '')
7+
def initialize(name: "")
88
@name = name
99
@lines = []
1010
end

lib/chord_pro/song.rb

Lines changed: 40 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
# frozen_string_literal: true
22

3-
require 'markaby'
3+
require "markaby"
44

55
module ChordPro
66
class Song
77
attr_accessor :title,
8-
:artist,
9-
:capo,
10-
:key,
11-
:tempo,
12-
:year,
13-
:copyright,
14-
:album,
15-
:tuning,
16-
:sections,
17-
:custom
8+
:artist,
9+
:capo,
10+
:key,
11+
:tempo,
12+
:year,
13+
:copyright,
14+
:album,
15+
:tuning,
16+
:sections,
17+
:custom
1818

1919
def initialize
2020
@sections = []
@@ -45,72 +45,72 @@ def to_html
4545

4646
dl.information do
4747
if song.tuning
48-
dt.tuning 'Tuning'
48+
dt.tuning "Tuning"
4949
dd.tuning song.tuning
5050
end
5151
if song.capo
52-
dt.capo 'Capo'
52+
dt.capo "Capo"
5353
dd.capo song.capo
5454
end
5555
if song.key
56-
dt.key 'Key'
56+
dt.key "Key"
5757
dd.key song.key
5858
end
5959
if song.tempo
60-
dt.tempo 'Tempo'
60+
dt.tempo "Tempo"
6161
dd.tempo song.tempo
6262
end
6363
if song.year
64-
dt.year 'Year'
64+
dt.year "Year"
6565
dd.year song.year
6666
end
6767
if song.album
68-
dt.album 'Album'
68+
dt.album "Album"
6969
dd.album song.album
7070
end
7171
end
7272

7373
song.sections.each do |section|
7474
div.section do
7575
div.name section.name
76-
div.lines do
77-
section.lines.each do |line|
78-
if line.tablature?
79-
div.tablature do
80-
line.tablature
81-
end
82-
elsif line.measures?
83-
div.measures do
84-
line.measures.each do |measure|
85-
div.measure do
86-
measure.chords.each do |chord|
87-
div.chord chord
76+
unless section.lines.empty?
77+
div.lines do
78+
section.lines.each do |line|
79+
if line.tablature?
80+
div.tablature do
81+
line.tablature
82+
end
83+
elsif line.measures?
84+
div.measures do
85+
line.measures.each do |measure|
86+
div.measure do
87+
measure.chords.each do |chord|
88+
div.chord chord
89+
end
8890
end
8991
end
9092
end
91-
end
92-
else
93-
div.line do
94-
line.parts.each do |part|
95-
div.part do
96-
div.chord part.chord
97-
div.lyric part.lyric
93+
else
94+
div.line do
95+
line.parts.each do |part|
96+
div.part do
97+
div.chord part.chord
98+
div.lyric part.lyric
99+
end
98100
end
99101
end
100102
end
101103
end
102104
end
103-
end unless section.lines.empty?
105+
end
104106
end
105107
end
106108
end
107109

108110
mab.to_s
109111
end
110112

111-
def comment=(value)
112-
@comment = value
113-
end
113+
attr_writer :comment
114114

115115
alias_method :ci=, :comment=
116116
alias_method :comment_italic=, :comment=

lib/chord_pro/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module ChordPro
2-
VERSION = '0.1.7'.freeze
2+
VERSION = "0.1.8".freeze
33
end

0 commit comments

Comments
 (0)