Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: CI

on: [push]

jobs:
test:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- name: Install Ruby and gems
uses: ruby/setup-ruby@v1
with:
bundler-cache: true
ruby-version: 3.2.4
- name: Setup DB, Run tests
run: |
bundle exec rspec

lint:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Install Ruby and gems
uses: ruby/setup-ruby@v1
with:
bundler-cache: true
ruby-version: 3.2.4
- name: Standard Ruby
uses: standardrb/standard-ruby-action@v1

2 changes: 1 addition & 1 deletion .ruby-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.7.0
3.2.4
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
sudo: false
language: ruby
rvm:
- 2.5.1
before_install: gem install bundler -v 1.16.1
- 3.2.4
before_install: gem install bundler -v 2.6.2
5 changes: 1 addition & 4 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
# frozen_string_literal: true

source 'https://rubygems.org'
source "https://rubygems.org"

git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }

# Specify your gem's dependencies in song_pro.gemspec
gemspec
4 changes: 2 additions & 2 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

require 'bundler/gem_tasks'
require 'rspec/core/rake_task'
require "bundler/gem_tasks"
require "rspec/core/rake_task"

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

Expand Down
195 changes: 98 additions & 97 deletions lib/chord_pro.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# frozen_string_literal: true

require 'chord_pro/version'
require 'chord_pro/song'
require 'chord_pro/section'
require 'chord_pro/line'
require 'chord_pro/part'
require 'chord_pro/measure'
require "chord_pro/version"
require "chord_pro/song"
require "chord_pro/section"
require "chord_pro/line"
require "chord_pro/part"
require "chord_pro/measure"

module ChordPro
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
Expand All @@ -18,121 +18,122 @@ module ChordPro
COMMENT_REGEX = /\{(?:c|comment|comment_italic|ci|comment_box|cb):([^$]*)\}/
SANITIZE_REGEX = /\{end_of_chorus|eoc|end_of_verse|eov|end_of_tab|eot|end_of_tab|eog|end_of_grid|colb\}/

def self.parse(lines)
song = Song.new
current_section = nil

lines.split("\n").each do |text|
if text.start_with?('{meta:')
process_custom_attribute(song, text)
elsif section_start?(text)
current_section = process_section(song, text)
elsif !comment_starts?(text) && attribute_start?(text)
process_attribute(song, text)
elsif text.match(SANITIZE_REGEX)
# ignore
else
process_lyrics_and_chords(song, current_section, text)
class << self
def parse(lines)
song = Song.new
current_section = nil

lines.split("\n").each do |text|
if text.start_with?("{meta:")
process_custom_attribute(song, text)
elsif section_start?(text)
current_section = process_section(song, text)
elsif !comment_starts?(text) && attribute_start?(text)
process_attribute(song, text)
elsif text.match(SANITIZE_REGEX)
# ignore
else
process_lyrics_and_chords(song, current_section, text)
end
end

song
end

song
end
def process_section(song, text)
matches = SECTION_REGEX.match(text)
name = (!matches[1].empty?) ? matches[1].strip : section_name_by_directive(text)

def self.process_section(song, text)
matches = SECTION_REGEX.match(text)
name = !matches[1].empty? ? matches[1].strip : section_name_by_directive(text)
current_section = Section.new(name: name)
song.sections << current_section

current_section = Section.new(name: name)
song.sections << current_section
current_section
end

current_section
end
def process_attribute(song, text)
matches = ATTRIBUTE_REGEX.match(text)
key = matches[1]
value = matches[2].strip

if song.respond_to?(:"#{key}=")
song.send(:"#{key}=", value)
else
puts "WARNING: Unknown attribute '#{key}'"
end
end

def self.process_attribute(song, text)
matches = ATTRIBUTE_REGEX.match(text)
key = matches[1]
value = matches[2].strip
def process_custom_attribute(song, text)
matches = CUSTOM_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}'"
song.set_custom(key, value)
end
end

def self.process_custom_attribute(song, text)
matches = CUSTOM_ATTRIBUTE_REGEX.match(text)
key = matches[1]
value = matches[2].strip
def process_lyrics_and_chords(song, current_section, text)
return if text == ""

song.set_custom(key, value)
end
if current_section.nil?
current_section = Section.new(name: "")
song.sections << current_section
end

def self.process_lyrics_and_chords(song, current_section, text)
return if text == ''
line = Line.new

if current_section.nil?
current_section = Section.new(name: '')
song.sections << current_section
end
if text.start_with?("|-")
line.tablature = text
elsif text.start_with?("| ")
captures = text.scan(MEASURES_REGEX).flatten

line = Line.new
measures = []

if text.start_with?('|-')
line.tablature = text
elsif text.start_with?('| ')
captures = text.scan(MEASURES_REGEX).flatten
captures.each do |capture|
chords = capture.scan(CHORDS_REGEX).flatten
measure = Measure.new
measure.chords = chords
measures << measure
end

measures = []
line.measures = measures
elsif comment_starts?(text)
matches = COMMENT_REGEX.match(text)
comment = matches[1].strip
line.comment = comment
else
captures = text.scan(CHORDS_AND_LYRICS_REGEX).flatten

captures.each do |capture|
chords = capture.scan(CHORDS_REGEX).flatten
measure = Measure.new
measure.chords = chords
measures << measure
end
captures.each_slice(2) do |pair|
part = Part.new
chord = pair[0]&.strip || ""
part.chord = chord.delete("[").delete("]")
part.lyric = pair[1] || ""

line.measures = measures
elsif comment_starts?(text)
matches = COMMENT_REGEX.match(text)
comment = matches[1].strip
line.comment = comment
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 == '')
line.parts << part unless (part.chord == "") && (part.lyric == "")
end
end
end

current_section.lines << line unless line.empty?
end
current_section.lines << line unless line.empty?
end

private
private

def self.attribute_start?(text)
text.match(/\{(.+):(.*)\}/)
end
def attribute_start?(text)
text.match(/\{(.+):(.*)\}/)
end

def self.section_start?(text)
text.match(SECTION_REGEX)
end
def section_start?(text)
text.match(SECTION_REGEX)
end

def self.comment_starts?(text)
text.match(/\{(?:c|comment|comment_italic|ci|comment_box|cb):/)
end
def comment_starts?(text)
text.match(/\{(?:c|comment|comment_italic|ci|comment_box|cb):/)
end

def self.section_name_by_directive(text)
return 'Chorus' if text.match(/soc|start_of_chorus|chorus/)
return 'Verse' if text.match(/sov|start_of_verse/)
return 'Tab' if text.match(/sot|start_of_tab/)
return 'Grid' if text.match(/sot|start_of_grid/)
def section_name_by_directive(text)
return "Chorus" if /soc|start_of_chorus|chorus/.match?(text)
return "Verse" if /sov|start_of_verse/.match?(text)
return "Tab" if /sot|start_of_tab/.match?(text)
"Grid" if /sot|start_of_grid/.match?(text)
end
end

end
2 changes: 1 addition & 1 deletion lib/chord_pro/section.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module ChordPro
class Section
attr_accessor :name, :lines

def initialize(name: '')
def initialize(name: "")
@name = name
@lines = []
end
Expand Down
Loading