irealb_parser 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "http://rubygems.org"
2
+
3
+ gemspec
@@ -0,0 +1,35 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ irealb_parser (0.0.1)
5
+ activesupport (~> 3.0)
6
+ i18n (~> 0.6.0)
7
+ thor (~> 0.13)
8
+
9
+ GEM
10
+ remote: http://rubygems.org/
11
+ specs:
12
+ activesupport (3.2.3)
13
+ i18n (~> 0.6)
14
+ multi_json (~> 1.0)
15
+ diff-lcs (1.1.3)
16
+ i18n (0.6.0)
17
+ multi_json (1.2.0)
18
+ rake (0.9.2.2)
19
+ rspec (2.9.0)
20
+ rspec-core (~> 2.9.0)
21
+ rspec-expectations (~> 2.9.0)
22
+ rspec-mocks (~> 2.9.0)
23
+ rspec-core (2.9.0)
24
+ rspec-expectations (2.9.0)
25
+ diff-lcs (~> 1.1.3)
26
+ rspec-mocks (2.9.0)
27
+ thor (0.14.6)
28
+
29
+ PLATFORMS
30
+ ruby
31
+
32
+ DEPENDENCIES
33
+ irealb_parser!
34
+ rake
35
+ rspec (~> 2.0)
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 Ben Hughes
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,19 @@
1
+ = iReal B Parser
2
+
3
+ Parses the iReal B chords format and outputs chords-json.
4
+
5
+ == Command-Line Usage
6
+
7
+ The gem comes with a single thor task "chords-json" that will take a file containing the iReal B chords format and convert it to chords-json (http://github.com/rubiety/chords-json).
8
+
9
+ $ irealb_parser json file.txt
10
+
11
+ == How to Get a File
12
+
13
+ Various iRealB links are available on the iRealB forums[http://irealb.com/forums].
14
+
15
+ * Find a link, and copy link to the clipboard. What you have copied is the URL-encoded contents of the text we need to process.
16
+ * Go to a URL decoder like this[http://meyerweb.com/eric/tools/dencoder/], paste the entire string in there, but remove the "irealb://" prefix.
17
+ * Take decoded contents and save it to a text file.
18
+ * Run <tt>$ irealb_parser json file.txt</tt> against the file.
19
+
@@ -0,0 +1,23 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+
4
+ require 'rake'
5
+ require 'rspec/core/rake_task'
6
+
7
+ Bundler::GemHelper.install_tasks
8
+
9
+ desc 'Default: run unit tests.'
10
+ task :default => [:clean, :test]
11
+
12
+ desc "Run Specs"
13
+ RSpec::Core::RakeTask.new(:spec) do |t|
14
+ end
15
+
16
+ task :test => :spec
17
+
18
+ desc "Clean up files."
19
+ task :clean do |t|
20
+ FileUtils.rm_rf "tmp"
21
+ Dir.glob("spec/db/*.sqlite3").each {|f| FileUtils.rm f }
22
+ Dir.glob("validates_lengths_from_database-*.gem").each {|f| FileUtils.rm f }
23
+ end
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "rubygems"
4
+ require "irealb/parser"
5
+ require "irealb/parser/runner"
6
+
7
+ IRealB::Parser::Runner.start
@@ -0,0 +1,134 @@
1
+ require "rubygems"
2
+ require "active_support/core_ext"
3
+ require "active_support/json/encoding"
4
+
5
+ module IRealB
6
+ class Parser
7
+ require "irealb/parser/chord"
8
+ require "irealb/parser/chord_grouping"
9
+ require "irealb/parser/tune"
10
+
11
+ attr_reader :text
12
+ attr_reader :tunes
13
+
14
+ def initialize(text)
15
+ @text = text
16
+ @tunes = []
17
+ end
18
+
19
+ def parse
20
+ @text.split("=").in_groups_of(6).each do |raw_tune|
21
+ parse_tune(raw_tune).tap do |tune|
22
+ @tunes << tune if tune
23
+ end
24
+ end
25
+ self
26
+ end
27
+
28
+ def as_json(options = {})
29
+ tunes.map(&:as_json)
30
+ end
31
+
32
+
33
+ protected
34
+
35
+ def parse_tune(raw_tune)
36
+ return nil if raw_tune.size < 5
37
+
38
+ Tune.new(:name => raw_tune[0], :composer => raw_tune[1], :style => raw_tune[2], :key => raw_tune[3]).tap do |tune|
39
+ raw_changes = raw_tune[5].to_s
40
+
41
+ tokens = raw_changes.scan(/\*[A-Za-z]|\<[^\>]+\>|S|Y|Q|U|s|l|T\d\d|[\|\{\}\[\]]|N\d|[^ \|\,\]\}]+/)
42
+ parse_changes(tune, tokens)
43
+
44
+ reduce_time_signatures(tune)
45
+ end
46
+ end
47
+
48
+ def parse_changes(tune, tokens)
49
+ current_section = ChordGrouping.new
50
+ current_bar = []
51
+ current_ending = nil
52
+ tune_ended = false
53
+
54
+ end_bar = lambda {|token|
55
+ unless current_bar.empty?
56
+ if current_ending
57
+ current_section.endings ||= [[], []]
58
+ current_section.endings[current_ending - 1] << current_bar
59
+ else
60
+ current_section.bars << current_bar
61
+ end
62
+
63
+ current_bar = []
64
+ end
65
+ }
66
+
67
+ tokens.each do |token|
68
+ case token
69
+ when /\*[A-Za-z]/
70
+ current_section.section = token.gsub("*", "")
71
+ when /T\d\d/
72
+ current_section.time = "#{token[1]}/#{token[2]}"
73
+ when "|"
74
+ end_bar.call(token)
75
+ when "]"
76
+ end_bar.call(token)
77
+
78
+ tune.chord_groupings << current_section
79
+ current_section = ChordGrouping.new
80
+ current_ending = nil
81
+ when "Z"
82
+ end_bar.call(token)
83
+
84
+ tune.chord_groupings << current_section
85
+ current_section = ChordGrouping.new
86
+ tune_ended = true
87
+ when "}"
88
+ end_bar.call(token)
89
+
90
+ if tune_ended
91
+ current_section.coda = true
92
+ tune.chord_groupings << current_section
93
+ current_section = ChordGrouping.new
94
+ else
95
+ current_section.repeat = 1
96
+
97
+ unless current_ending
98
+ tune.chord_groupings << current_section
99
+ current_section = ChordGrouping.new
100
+ end
101
+ end
102
+ when "N1"
103
+ current_ending = 1
104
+ when "N2"
105
+ current_ending = 2
106
+ when "Q"
107
+ current_bar << {"goto" => "coda"}
108
+ when /^\</
109
+ current_bar << {"note" => token.gsub("<", "").gsub(">", "") }
110
+ when "Y"
111
+ true # Code Designation
112
+ when "[", "{"
113
+ true # Begin Section Designation
114
+ when "s", "l", "S"
115
+ true # Don't know what these are, but we don't want them
116
+ else
117
+ current_bar << parse_chord(token)
118
+ end
119
+ end
120
+ end
121
+
122
+ def parse_chord(raw_chord)
123
+ raw_chord.gsub!("^", "maj")
124
+ raw_chord.gsub!("h", "dim")
125
+ raw_chord.gsub!("x", "%")
126
+ raw_chord
127
+ end
128
+
129
+ def reduce_time_signatures(tune)
130
+ # TODO: This is a bit tricky...
131
+ end
132
+ end
133
+ end
134
+
@@ -0,0 +1,7 @@
1
+ module IRealB
2
+ class Parser
3
+ class Chord
4
+
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,31 @@
1
+ module IRealB
2
+ class Parser
3
+ class ChordGrouping
4
+ attr_accessor :section
5
+ attr_accessor :time
6
+ attr_accessor :repeat
7
+ attr_accessor :bars
8
+ attr_accessor :endings
9
+ attr_accessor :coda
10
+
11
+ def initialize(attributes = {})
12
+ self.bars = []
13
+
14
+ attributes.each do |key, value|
15
+ self.send("#{key}=", value) if respond_to?(key)
16
+ end
17
+ end
18
+
19
+ def as_json(options = {})
20
+ {
21
+ :section => section,
22
+ :time => time,
23
+ :repeat => repeat,
24
+ :coda => coda,
25
+ :bars => bars,
26
+ :endings => endings
27
+ }.reject {|k,v| v.nil? }
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,22 @@
1
+ require "thor"
2
+ require "irealb/parser"
3
+
4
+ module IRealB
5
+ class Parser
6
+ class Runner < Thor
7
+ desc :json, "Takes an iReal B text file (decoded contents of href attribute) and outputs chords-json."
8
+ def json(path)
9
+ puts JSON.pretty_generate(IRealB::Parser.new(File.read(path)).parse.as_json)
10
+ end
11
+
12
+ protected
13
+
14
+ def expand_paths(paths = [])
15
+ paths.map do |path|
16
+ [path] + Dir[path + "**/*.rb"]
17
+ end.flatten.uniq.reject {|f| File.directory?(f) }
18
+ end
19
+ end
20
+ end
21
+ end
22
+
@@ -0,0 +1,29 @@
1
+ module IRealB
2
+ class Parser
3
+ class Tune
4
+ attr_accessor :name
5
+ attr_accessor :composer
6
+ attr_accessor :style
7
+ attr_accessor :key
8
+ attr_accessor :chord_groupings
9
+
10
+ def initialize(attributes = {})
11
+ self.chord_groupings = []
12
+
13
+ attributes.each do |key, value|
14
+ self.send("#{key}=", value) if respond_to?(key)
15
+ end
16
+ end
17
+
18
+ def as_json(options = {})
19
+ {
20
+ :name => name,
21
+ :composer => composer,
22
+ :style => style,
23
+ :key => key,
24
+ :changes => chord_groupings.as_json
25
+ }.reject {|k,v| v.nil? }
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,5 @@
1
+ module IRealB
2
+ class Parser
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,10 @@
1
+ $:.unshift(File.join(File.dirname(__FILE__), "../lib"))
2
+
3
+ require "rubygems"
4
+ require "rspec"
5
+ require "irealb/parser"
6
+
7
+ # Requires supporting files with custom matchers and macros, etc,
8
+ # in ./support/ and its subdirectories.
9
+ Dir["#{File.dirname(__FILE__)}/matchers/**/*.rb"].each {|f| require f}
10
+
metadata ADDED
@@ -0,0 +1,114 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: irealb_parser
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Ben Hughes
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-03-17 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: thor
16
+ requirement: &70118634751900 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '0.13'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *70118634751900
25
+ - !ruby/object:Gem::Dependency
26
+ name: activesupport
27
+ requirement: &70118634750420 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ~>
31
+ - !ruby/object:Gem::Version
32
+ version: '3.0'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *70118634750420
36
+ - !ruby/object:Gem::Dependency
37
+ name: i18n
38
+ requirement: &70118634749700 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ version: 0.6.0
44
+ type: :runtime
45
+ prerelease: false
46
+ version_requirements: *70118634749700
47
+ - !ruby/object:Gem::Dependency
48
+ name: rspec
49
+ requirement: &70118634763040 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '2.0'
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: *70118634763040
58
+ - !ruby/object:Gem::Dependency
59
+ name: rake
60
+ requirement: &70118634762060 !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ! '>='
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ type: :development
67
+ prerelease: false
68
+ version_requirements: *70118634762060
69
+ description: Converts iRealB chords to the chords-json format.
70
+ email: ben@railsgarden.com
71
+ executables:
72
+ - irealb_parser
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - lib/irealb/parser/chord.rb
77
+ - lib/irealb/parser/chord_grouping.rb
78
+ - lib/irealb/parser/runner.rb
79
+ - lib/irealb/parser/tune.rb
80
+ - lib/irealb/parser/version.rb
81
+ - lib/irealb/parser.rb
82
+ - spec/spec_helper.rb
83
+ - Gemfile
84
+ - Gemfile.lock
85
+ - LICENSE
86
+ - Rakefile
87
+ - README.rdoc
88
+ - bin/irealb_parser
89
+ homepage: http://github.com/rubiety/irealb_parser
90
+ licenses: []
91
+ post_install_message:
92
+ rdoc_options: []
93
+ require_paths:
94
+ - lib
95
+ required_ruby_version: !ruby/object:Gem::Requirement
96
+ none: false
97
+ requirements:
98
+ - - ! '>='
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
101
+ required_rubygems_version: !ruby/object:Gem::Requirement
102
+ none: false
103
+ requirements:
104
+ - - ! '>='
105
+ - !ruby/object:Gem::Version
106
+ version: 1.3.4
107
+ requirements: []
108
+ rubyforge_project: irealb_parser
109
+ rubygems_version: 1.8.16
110
+ signing_key:
111
+ specification_version: 3
112
+ summary: Converts iRealB chords to the chords-json format.
113
+ test_files: []
114
+ has_rdoc: