jam_script_highlighter 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gemfiles/Gemfile.travis +9 -0
- data/.travis.yml +10 -0
- data/Gemfile +2 -0
- data/Gemfile.lock +55 -0
- data/Guardfile +30 -0
- data/Rakefile +17 -0
- data/bin/jam_as_html +73 -0
- data/bin/jam_as_pdf +78 -0
- data/jam_script_highlighter.gemspec +32 -0
- data/lib/jam_script_highlighter/html/tag_helpers.rb +5 -0
- data/lib/jam_script_highlighter/html.rb +51 -0
- data/lib/jam_script_highlighter/line.rb +24 -0
- data/lib/jam_script_highlighter/line_types/control_line.rb +9 -0
- data/lib/jam_script_highlighter/line_types/empty_line.rb +6 -0
- data/lib/jam_script_highlighter/line_types/metadata_line.rb +9 -0
- data/lib/jam_script_highlighter/line_types/note_line.rb +9 -0
- data/lib/jam_script_highlighter/line_types/other_line.rb +6 -0
- data/lib/jam_script_highlighter/line_types/section_line.rb +9 -0
- data/lib/jam_script_highlighter/line_types/title_line.rb +9 -0
- data/lib/jam_script_highlighter/line_types/variation_line.rb +9 -0
- data/lib/jam_script_highlighter/line_types.rb +8 -0
- data/lib/jam_script_highlighter/version.rb +9 -0
- data/lib/jam_script_highlighter.rb +59 -0
- data/readme.md +103 -0
- data/test/support/colors.rb +26 -0
- data/test/support/helpers/section_helpers.rb +26 -0
- data/test/support/jams/jams.rb +42 -0
- data/test/support/output.rb +35 -0
- data/test/test_helper.rb +41 -0
- data/test/unit/jam_script_highlighter/html_test.rb +65 -0
- data/test/unit/jam_script_highlighter/line_test.rb +22 -0
- data/test/unit/jam_script_highlighter_test.rb +84 -0
- metadata +173 -0
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
jam_script_highlighter (0.0.1)
|
5
|
+
pdfkit
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: http://rubygems.org/
|
9
|
+
specs:
|
10
|
+
ansi (1.4.3)
|
11
|
+
coderay (1.0.8)
|
12
|
+
columnize (0.3.6)
|
13
|
+
debugger (1.2.3)
|
14
|
+
columnize (>= 0.3.1)
|
15
|
+
debugger-linecache (~> 1.1.1)
|
16
|
+
debugger-ruby_core_source (~> 1.1.5)
|
17
|
+
debugger-linecache (1.1.2)
|
18
|
+
debugger-ruby_core_source (>= 1.1.1)
|
19
|
+
debugger-ruby_core_source (1.1.6)
|
20
|
+
guard (1.6.1)
|
21
|
+
listen (>= 0.6.0)
|
22
|
+
lumberjack (>= 1.0.2)
|
23
|
+
pry (>= 0.9.10)
|
24
|
+
thor (>= 0.14.6)
|
25
|
+
guard-minitest (0.5.0)
|
26
|
+
guard (>= 0.4)
|
27
|
+
hirb (0.7.0)
|
28
|
+
listen (0.7.1)
|
29
|
+
lumberjack (1.0.2)
|
30
|
+
method_source (0.8.1)
|
31
|
+
minitest (4.4.0)
|
32
|
+
pdfkit (0.5.2)
|
33
|
+
pry (0.9.10)
|
34
|
+
coderay (~> 1.0.5)
|
35
|
+
method_source (~> 0.8)
|
36
|
+
slop (~> 3.3.1)
|
37
|
+
rb-fsevent (0.9.3)
|
38
|
+
slop (3.3.3)
|
39
|
+
thor (0.16.0)
|
40
|
+
turn (0.9.6)
|
41
|
+
ansi
|
42
|
+
wirble (0.1.3)
|
43
|
+
|
44
|
+
PLATFORMS
|
45
|
+
ruby
|
46
|
+
|
47
|
+
DEPENDENCIES
|
48
|
+
debugger
|
49
|
+
guard-minitest
|
50
|
+
hirb
|
51
|
+
jam_script_highlighter!
|
52
|
+
minitest
|
53
|
+
rb-fsevent (~> 0.9.1)
|
54
|
+
turn
|
55
|
+
wirble
|
data/Guardfile
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
# A sample Guardfile
|
2
|
+
# More info at https://github.com/guard/guard#readme
|
3
|
+
|
4
|
+
def run testfile
|
5
|
+
cmd = "rake test TEST='#{testfile}'"
|
6
|
+
out cmd
|
7
|
+
exec cmd
|
8
|
+
end
|
9
|
+
|
10
|
+
guard 'minitest' do
|
11
|
+
# with Minitest::Unit
|
12
|
+
watch(%r|^test/(.*)\/?test_(.*)\.rb|)
|
13
|
+
watch(%r|^lib/(.*)([^/]+)\.rb|) { |m| "test/#{m[1]}test_#{m[2]}.rb" }
|
14
|
+
watch(%r|^test/test_helper\.rb|) { "test" }
|
15
|
+
|
16
|
+
# with Minitest::Spec
|
17
|
+
# watch(%r|^spec/(.*)_spec\.rb|)
|
18
|
+
# watch(%r|^lib/(.*)([^/]+)\.rb|) { |m| "spec/#{m[1]}#{m[2]}_spec.rb" }
|
19
|
+
# watch(%r|^spec/spec_helper\.rb|) { "spec" }
|
20
|
+
|
21
|
+
# Rails 3.2
|
22
|
+
# watch(%r|^app/controllers/(.*)\.rb|) { |m| "test/controllers/#{m[1]}_test.rb" }
|
23
|
+
# watch(%r|^app/helpers/(.*)\.rb|) { |m| "test/helpers/#{m[1]}_test.rb" }
|
24
|
+
# watch(%r|^app/models/(.*)\.rb|) { |m| "test/unit/#{m[1]}_test.rb" }
|
25
|
+
|
26
|
+
# Rails
|
27
|
+
# watch(%r|^app/controllers/(.*)\.rb|) { |m| "test/functional/#{m[1]}_test.rb" }
|
28
|
+
# watch(%r|^app/helpers/(.*)\.rb|) { |m| "test/helpers/#{m[1]}_test.rb" }
|
29
|
+
# watch(%r|^app/models/(.*)\.rb|) { |m| "test/unit/#{m[1]}_test.rb" }
|
30
|
+
end
|
data/Rakefile
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
begin
|
2
|
+
require 'bundler/setup'
|
3
|
+
rescue LoadError
|
4
|
+
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
5
|
+
end
|
6
|
+
|
7
|
+
Bundler::GemHelper.install_tasks
|
8
|
+
|
9
|
+
|
10
|
+
require 'rake/testtask'
|
11
|
+
|
12
|
+
Rake::TestTask.new do |t|
|
13
|
+
# t.libs << "lib"
|
14
|
+
t.libs << "test"
|
15
|
+
t.test_files = FileList['test/**/*_test.rb']
|
16
|
+
t.verbose = true
|
17
|
+
end
|
data/bin/jam_as_html
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'jam_script_highlighter'
|
4
|
+
|
5
|
+
template = "
|
6
|
+
<html>
|
7
|
+
<head>
|
8
|
+
<title>{{ title }}</title>
|
9
|
+
<style>
|
10
|
+
body {font-family: arial; font-size: 16px;}
|
11
|
+
|
12
|
+
div {margin: 0px; padding: 0px}
|
13
|
+
|
14
|
+
h1 {font-weight: 400; font-size: 20px; margin-bottom: 2px; text-transform: uppercase}
|
15
|
+
h2 {font-weight: 400; font-size: 17px; margin-top: 0px}
|
16
|
+
h3 {font-weight: 400; font-size: 19px;}
|
17
|
+
h4 {font-weight: 400; font-size: 18px;}
|
18
|
+
|
19
|
+
.container {width: 80%; margin: 0px auto; padding: 20px;-webkit-column-count: 2;-webkit-column-fill: auto;-webkit-column-gap: 50px; height: 90%;}
|
20
|
+
|
21
|
+
p {font-size: 17px}
|
22
|
+
|
23
|
+
.note {color: #505050; font-size: 13px; margin-bottom: 0px; margin-top: 4px; font-style: italic;}
|
24
|
+
.control, .section {margin-top: 25px; margin-bottom: 0px; border-bottom: 5px solid #d0d0d0}
|
25
|
+
.variation {font-size: 16px; margin-top: 15px; margin-bottom: 0px; border-bottom: 1px solid #d0d0d0}
|
26
|
+
.control {margin-bottom: 25px;}
|
27
|
+
|
28
|
+
.chords .other {background-color: #f0f0f0; color: #404040; padding: 10px; margin: 5px 0px;}
|
29
|
+
.structure .other {margin: 5px 0px;}
|
30
|
+
|
31
|
+
</style>
|
32
|
+
</head>
|
33
|
+
<body>
|
34
|
+
|
35
|
+
<div class=\"container\">
|
36
|
+
{{ yield }}
|
37
|
+
</div>
|
38
|
+
|
39
|
+
</body>
|
40
|
+
</html>
|
41
|
+
"
|
42
|
+
|
43
|
+
ARGV.each do |a|
|
44
|
+
if File.directory? a
|
45
|
+
files = Dir.glob a
|
46
|
+
else
|
47
|
+
files = [a]
|
48
|
+
end
|
49
|
+
|
50
|
+
files.each do |fl|
|
51
|
+
puts "Reading #{a}\n"
|
52
|
+
file = File.open(fl, 'r')
|
53
|
+
|
54
|
+
js = file.read
|
55
|
+
html = ""
|
56
|
+
title = ""
|
57
|
+
|
58
|
+
html = JamScriptHighlighter::Html.render js
|
59
|
+
|
60
|
+
template = template.gsub '{{ title }}', title
|
61
|
+
html = template.gsub '{{ yield }}', html
|
62
|
+
|
63
|
+
output = File.open("#{a}.html", 'w')
|
64
|
+
output << html
|
65
|
+
|
66
|
+
file.close
|
67
|
+
output.close
|
68
|
+
|
69
|
+
puts "<< Rendered to #{a}.html >>\n"
|
70
|
+
|
71
|
+
`open #{a}.html`
|
72
|
+
end
|
73
|
+
end
|
data/bin/jam_as_pdf
ADDED
@@ -0,0 +1,78 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'pdfkit'
|
4
|
+
require 'jam_script_highlighter'
|
5
|
+
|
6
|
+
template = "
|
7
|
+
<html>
|
8
|
+
<head>
|
9
|
+
<title>{{ title }}</title>
|
10
|
+
<style>
|
11
|
+
body {font-family: arial; font-size: 16px;}
|
12
|
+
|
13
|
+
div {margin: 0px; padding: 0px}
|
14
|
+
|
15
|
+
h1 {font-weight: 400; font-size: 20px; margin-bottom: 2px; text-transform: uppercase}
|
16
|
+
h2 {font-weight: 400; font-size: 17px; margin-top: 0px}
|
17
|
+
h3 {font-weight: 400; font-size: 19px;}
|
18
|
+
h4 {font-weight: 400; font-size: 18px;}
|
19
|
+
|
20
|
+
.container {width: 80%; margin: 0px auto; padding: 20px;-webkit-column-count: 2;-webkit-column-fill: auto;-webkit-column-gap: 50px; height: 90%;}
|
21
|
+
|
22
|
+
p {font-size: 17px}
|
23
|
+
|
24
|
+
.note {color: #505050; font-size: 13px; margin-bottom: 0px; margin-top: 4px; font-style: italic;}
|
25
|
+
.control, .section {margin-top: 25px; margin-bottom: 0px; border-bottom: 5px solid #d0d0d0}
|
26
|
+
.variation {font-size: 16px; margin-top: 15px; margin-bottom: 0px; border-bottom: 1px solid #d0d0d0}
|
27
|
+
.control {margin-bottom: 25px;}
|
28
|
+
|
29
|
+
.chords .other {background-color: #f0f0f0; color: #404040; padding: 10px; margin: 5px 0px;}
|
30
|
+
.structure .other {margin: 5px 0px;}
|
31
|
+
|
32
|
+
</style>
|
33
|
+
</head>
|
34
|
+
<body>
|
35
|
+
|
36
|
+
<div class=\"container\">
|
37
|
+
{{ yield }}
|
38
|
+
</div>
|
39
|
+
|
40
|
+
</body>
|
41
|
+
</html>
|
42
|
+
"
|
43
|
+
|
44
|
+
ARGV.each do |a|
|
45
|
+
if File.directory? a
|
46
|
+
files = Dir.glob a
|
47
|
+
else
|
48
|
+
files = [a]
|
49
|
+
end
|
50
|
+
|
51
|
+
files.each do |fl|
|
52
|
+
puts "Reading #{a}\n"
|
53
|
+
file = File.open(fl, 'r')
|
54
|
+
|
55
|
+
js = file.read
|
56
|
+
html = ""
|
57
|
+
title = ""
|
58
|
+
|
59
|
+
html = JamScriptHighlighter::Html.render js
|
60
|
+
|
61
|
+
template = template.gsub '{{ title }}', title
|
62
|
+
html = template.gsub '{{ yield }}', html
|
63
|
+
|
64
|
+
kit = PDFKit.new(html, :page_size => 'A4')
|
65
|
+
|
66
|
+
# Get an inline PDF
|
67
|
+
pdf = kit.to_pdf
|
68
|
+
|
69
|
+
# Save the PDF to a file
|
70
|
+
kit.to_file("#{a}.pdf")
|
71
|
+
|
72
|
+
file.close
|
73
|
+
|
74
|
+
puts "<< Rendered to #{a}.pdf >>"
|
75
|
+
|
76
|
+
`open #{a}.pdf`
|
77
|
+
end
|
78
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$LOAD_PATH.unshift File.expand_path("../lib", __FILE__)
|
3
|
+
require "jam_script_highlighter/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = 'jam_script_highlighter'
|
7
|
+
s.version = JamScriptHighlighter::VERSION::STRING
|
8
|
+
s.authors = ["Adam Phillips"]
|
9
|
+
s.description = 'Simple JamScript highlighter'
|
10
|
+
s.summary = "jam_script_highlighter-#{s.version}"
|
11
|
+
s.email = ''
|
12
|
+
s.homepage = ""
|
13
|
+
|
14
|
+
s.platform = Gem::Platform::RUBY
|
15
|
+
|
16
|
+
s.add_dependency 'pdfkit'
|
17
|
+
|
18
|
+
s.add_development_dependency 'minitest'
|
19
|
+
s.add_development_dependency 'turn'
|
20
|
+
s.add_development_dependency 'debugger'
|
21
|
+
s.add_development_dependency 'guard-minitest'
|
22
|
+
s.add_development_dependency 'rb-fsevent', '~> 0.9.1'
|
23
|
+
s.add_development_dependency 'hirb'
|
24
|
+
s.add_development_dependency 'wirble'
|
25
|
+
|
26
|
+
s.rubygems_version = ">= 1.6.1"
|
27
|
+
s.files = `git ls-files`.split("\n").reject {|path| path =~ /\.gitignore$/ }
|
28
|
+
s.test_files = `git ls-files -- {spec,features}/*`.split("\n")
|
29
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
30
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
31
|
+
s.require_path = "lib"
|
32
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
module JamScriptHighlighter
|
2
|
+
module Html
|
3
|
+
class << self
|
4
|
+
def render string, opts={}
|
5
|
+
@html = ''
|
6
|
+
@section_type = 'chords'
|
7
|
+
|
8
|
+
@processing_metadata = true
|
9
|
+
@processing_section = false
|
10
|
+
|
11
|
+
js = JamScriptHighlighter.highlight(string)
|
12
|
+
js.each do |line|
|
13
|
+
render_line line
|
14
|
+
end
|
15
|
+
@html += "</div>\n" if @processing_section
|
16
|
+
@html += "</div>\n" unless @processing_metadata
|
17
|
+
@html
|
18
|
+
end
|
19
|
+
|
20
|
+
def render_line line_details
|
21
|
+
line = Line.new(line_details[:line], line_details[:type])
|
22
|
+
|
23
|
+
@html += case line_details[:type]
|
24
|
+
when :control
|
25
|
+
ret = ''
|
26
|
+
if @processing_section
|
27
|
+
ret += "</div>\n\n"
|
28
|
+
@processing_section = false
|
29
|
+
end
|
30
|
+
ret += line.to_html
|
31
|
+
@section_type = line.text.downcase
|
32
|
+
ret += "<div class=\"#{@section_type}\">"
|
33
|
+
ret
|
34
|
+
when :section
|
35
|
+
ret = ''
|
36
|
+
ret += "</div>\n\n" if @processing_section
|
37
|
+
|
38
|
+
ret += line.to_html
|
39
|
+
ret += "<div class=\"#{@section_type}\">"
|
40
|
+
|
41
|
+
@processing_metadata = false
|
42
|
+
@processing_section = true
|
43
|
+
|
44
|
+
ret
|
45
|
+
else
|
46
|
+
line.to_html
|
47
|
+
end + "\n"
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
module JamScriptHighlighter
|
2
|
+
class Line
|
3
|
+
attr_accessor :text
|
4
|
+
|
5
|
+
def to_html
|
6
|
+
@text || ''
|
7
|
+
end
|
8
|
+
|
9
|
+
class << self
|
10
|
+
def new text, type=nil
|
11
|
+
if type
|
12
|
+
class_name = "#{type.to_s.split('_').map{|e| e.capitalize}.join}Line"
|
13
|
+
cls = JamScriptHighlighter::LineTypes.const_get(class_name)
|
14
|
+
cls.new text
|
15
|
+
else
|
16
|
+
ret = super()
|
17
|
+
ret.text = text
|
18
|
+
|
19
|
+
ret
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,8 @@
|
|
1
|
+
require 'jam_script_highlighter/line_types/control_line'
|
2
|
+
require 'jam_script_highlighter/line_types/empty_line'
|
3
|
+
require 'jam_script_highlighter/line_types/metadata_line'
|
4
|
+
require 'jam_script_highlighter/line_types/note_line'
|
5
|
+
require 'jam_script_highlighter/line_types/other_line'
|
6
|
+
require 'jam_script_highlighter/line_types/section_line'
|
7
|
+
require 'jam_script_highlighter/line_types/title_line'
|
8
|
+
require 'jam_script_highlighter/line_types/variation_line'
|
@@ -0,0 +1,59 @@
|
|
1
|
+
require 'jam_script_highlighter/html'
|
2
|
+
require 'jam_script_highlighter/line'
|
3
|
+
require 'jam_script_highlighter/line_types'
|
4
|
+
|
5
|
+
module JamScriptHighlighter
|
6
|
+
CONTROL = '#'
|
7
|
+
NOTE = '!'
|
8
|
+
SECTION = '*'
|
9
|
+
VARIATION = '-'
|
10
|
+
|
11
|
+
class << self
|
12
|
+
def highlight(string)
|
13
|
+
@index = 0
|
14
|
+
@ctrl_char_set = false
|
15
|
+
|
16
|
+
result = []
|
17
|
+
lines = string.split "\n"
|
18
|
+
|
19
|
+
lines.each_with_index do |line, index|
|
20
|
+
@index = index
|
21
|
+
result << process(line)
|
22
|
+
end
|
23
|
+
|
24
|
+
result
|
25
|
+
end
|
26
|
+
|
27
|
+
def process line
|
28
|
+
type = type_of line
|
29
|
+
if [:control, :note, :section, :variation].include?(type)
|
30
|
+
line = line[1..line.length].strip
|
31
|
+
@ctrl_char_set = true
|
32
|
+
elsif type == :empty
|
33
|
+
@ctrl_char_set = true
|
34
|
+
else
|
35
|
+
unless @ctrl_char_set
|
36
|
+
type = (@index == 0) ? :title : :metadata
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
{:type => type, :line => line}
|
41
|
+
end
|
42
|
+
|
43
|
+
def type_of(line)
|
44
|
+
return :empty if line.empty?
|
45
|
+
case line[0]
|
46
|
+
when CONTROL
|
47
|
+
:control
|
48
|
+
when NOTE
|
49
|
+
:note
|
50
|
+
when SECTION
|
51
|
+
:section
|
52
|
+
when VARIATION
|
53
|
+
:variation
|
54
|
+
else
|
55
|
+
:other
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
data/readme.md
ADDED
@@ -0,0 +1,103 @@
|
|
1
|
+
JamScript Highligher
|
2
|
+
--------------------
|
3
|
+
|
4
|
+
[![Build Status](https://travis-ci.org/adamphillips/JamScriptHighlighter.png?branch=master)](undefined)
|
5
|
+
|
6
|
+
SyntaxHighlighter for [JamScript](https://github.com/adamphillips/JamScript)
|
7
|
+
|
8
|
+
## Installing
|
9
|
+
|
10
|
+
Requires
|
11
|
+
- Ruby 1.9.2 or higher
|
12
|
+
- [wkhtmltopdf](https://github.com/pdfkit/pdfkit/wiki/Installing-WKHTMLTOPDF) for PDF conversion.
|
13
|
+
|
14
|
+
At the command line type
|
15
|
+
|
16
|
+
gem install jam_script_highlighter
|
17
|
+
|
18
|
+
if you are planning to use the highlighter in a Ruby application and you are using Bundler, add the following to your Gemfile
|
19
|
+
|
20
|
+
gem 'jam_script_highlighter'
|
21
|
+
|
22
|
+
then at the command line run
|
23
|
+
|
24
|
+
bundle install
|
25
|
+
|
26
|
+
## Highlighting a JamScript
|
27
|
+
|
28
|
+
To use the highlighter in Ruby, you must first require jam_script_highlighter
|
29
|
+
|
30
|
+
require 'jam_script_highlighter'
|
31
|
+
|
32
|
+
Then in order to highlight some JamScript, call the JamScriptHighlighter.highlight method passing it the JamScript as a string. It will return an array of hashes, 1 for each line of the document. The hash will contain the line of JamScript and its type. The type will be one of the following
|
33
|
+
|
34
|
+
- :title , The title line of the document. Always the first line
|
35
|
+
- :metadata, Document metadata such as composer or tempo
|
36
|
+
- :control, A control line such as # Structure, # Chords, # Beats
|
37
|
+
- :section, A section of the song such as * Verse, * Chorus
|
38
|
+
- :note, A note (textual not musical) about the section
|
39
|
+
- :other, Usually data such as chords or beats
|
40
|
+
|
41
|
+
For example
|
42
|
+
|
43
|
+
require 'jam_script_highlighter'
|
44
|
+
|
45
|
+
jam1 = "A Funky Tune
|
46
|
+
by Someone Cool
|
47
|
+
tempo 123bpm
|
48
|
+
|
49
|
+
* Verse
|
50
|
+
C F | G G
|
51
|
+
|
52
|
+
* Chorus
|
53
|
+
F | C :x4
|
54
|
+
|
55
|
+
# Structure
|
56
|
+
Verse, Verse, Chorus
|
57
|
+
Verse, Verse, Chorus
|
58
|
+
Chorus
|
59
|
+
"
|
60
|
+
|
61
|
+
puts JamScriptHighlighter.highlight(jam1).inspect
|
62
|
+
# => [{:type=>:title, :line=>"A Funky Tune"},
|
63
|
+
# {:type=>:metadata, :line=>"by Someone Cool"},
|
64
|
+
# {:type=>:metadata, :line=>"tempo 123bpm"},
|
65
|
+
# {:type=>:section, :line=>"Verse"},
|
66
|
+
# {:type=>:other, :line=>"C F | G G"},
|
67
|
+
# {:type=>:section, :line=>"Chorus"},
|
68
|
+
# {:type=>:other, :line=>"F | C :x4"},
|
69
|
+
# {:type=>:control, :line=>"Structure"},
|
70
|
+
# {:type=>:other, :line=>"Verse, Verse, Chorus"},
|
71
|
+
# {:type=>:other, :line=>"Verse, Verse, Chorus"},
|
72
|
+
# {:type=>:other, :line=>"Chorus"}]
|
73
|
+
|
74
|
+
## Converting to HTML
|
75
|
+
|
76
|
+
To convert a JamScript document to html, at the command line type
|
77
|
+
|
78
|
+
jam_as_html path/to/jam
|
79
|
+
|
80
|
+
The html will be output to /path/to/jam.html
|
81
|
+
You can use wildcards
|
82
|
+
|
83
|
+
jam_as_html folder/of/jams/*.jam
|
84
|
+
|
85
|
+
This will convert each file that matches the path
|
86
|
+
|
87
|
+
## Converting to a PDF
|
88
|
+
|
89
|
+
This requires that [wkhtmltopdf](https://github.com/pdfkit/pdfkit/wiki/Installing-WKHTMLTOPDF) is installed.
|
90
|
+
|
91
|
+
To convert a JamScript document to a pdf, at the command line type
|
92
|
+
|
93
|
+
jam_as_pdf path/to/jam
|
94
|
+
|
95
|
+
The html will be output to /path/to/jam.pdf
|
96
|
+
You can use wildcards
|
97
|
+
|
98
|
+
jam_as_pdf folder/of/jams/*.jam
|
99
|
+
|
100
|
+
This will convert each file that matches the path
|
101
|
+
|
102
|
+
|
103
|
+
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# Standard terminal color codes
|
2
|
+
COLORS = {
|
3
|
+
:reset => '0',
|
4
|
+
:bold => '1',
|
5
|
+
:red => '31',
|
6
|
+
:green => '32',
|
7
|
+
:yellow => '33',
|
8
|
+
:blue => '34',
|
9
|
+
:magenta => '35',
|
10
|
+
:cyan => '36',
|
11
|
+
:white => '37'
|
12
|
+
}
|
13
|
+
|
14
|
+
def color(text, color)
|
15
|
+
if COLORS[color]
|
16
|
+
"#{start_color color}#{text}#{reset_color}"
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def reset_color
|
21
|
+
"\e[#{COLORS[:reset]}m"
|
22
|
+
end
|
23
|
+
|
24
|
+
def start_color color
|
25
|
+
"\e[#{COLORS[color]}m"
|
26
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
module Kernel
|
2
|
+
def should_parse_bar text, expectations, &block
|
3
|
+
describe "when the text is #{text}" do
|
4
|
+
before do
|
5
|
+
@bar = Parsers::SectionParser.new.bar
|
6
|
+
@text = text
|
7
|
+
end
|
8
|
+
|
9
|
+
it 'should parse OK' do
|
10
|
+
bar = @bar.parse(@text)[:bar]
|
11
|
+
out bar, :yellow
|
12
|
+
bar[:chords].wont_be_nil
|
13
|
+
bar[:chords].length.must_equal expectations[:notes].length
|
14
|
+
bar[:chords].each_with_index do |ch, i|
|
15
|
+
chord = ch[:chord]
|
16
|
+
chord[:note].must_equal expectations[:notes][i]
|
17
|
+
# note the extra array around the expectation
|
18
|
+
# avoids unsightly [[]] when using
|
19
|
+
chord[:modifiers].must_equal [expectations[:modifiers][i]]
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
yield if block_given?
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
module Kernel
|
2
|
+
# sample jams to use for testing
|
3
|
+
def jams
|
4
|
+
{
|
5
|
+
:single_section => 'A single section jam
|
6
|
+
by Someone Cool
|
7
|
+
tempo 130bpm
|
8
|
+
|
9
|
+
* Verse
|
10
|
+
C F | F G
|
11
|
+
C F | C
|
12
|
+
',
|
13
|
+
|
14
|
+
:two_sections => 'A jam in two parts
|
15
|
+
by Someone Cooler
|
16
|
+
tempo 110bpm
|
17
|
+
|
18
|
+
* Verse
|
19
|
+
C F | F G
|
20
|
+
C F | C
|
21
|
+
|
22
|
+
* Chorus
|
23
|
+
C | F
|
24
|
+
',
|
25
|
+
|
26
|
+
:three_sections => 'A jam in three parts
|
27
|
+
by Someone Else
|
28
|
+
tempo 50bpm
|
29
|
+
|
30
|
+
* Verse
|
31
|
+
C F | F G
|
32
|
+
C F | C
|
33
|
+
|
34
|
+
* Chorus
|
35
|
+
C | F
|
36
|
+
|
37
|
+
* Bridge
|
38
|
+
Am | G | F | G
|
39
|
+
'
|
40
|
+
}
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
require 'hirb'
|
2
|
+
|
3
|
+
# Output functions
|
4
|
+
# =================
|
5
|
+
# Functions to format and display output
|
6
|
+
|
7
|
+
# Writes content out to the terminal
|
8
|
+
# Use Hirb to format tables.
|
9
|
+
# Can specify a color using one of the defined color constants
|
10
|
+
#
|
11
|
+
# Examples
|
12
|
+
#
|
13
|
+
# out 'some_text', :blue # Prints out the text in blue
|
14
|
+
#
|
15
|
+
def out(text, text_color=nil, *opts)
|
16
|
+
extend Hirb::Console
|
17
|
+
#text = text.to_a if text.kind_of? ActiveRecord::Base
|
18
|
+
|
19
|
+
if text.kind_of? Array
|
20
|
+
STDOUT.puts start_color text_color if text_color
|
21
|
+
table text, *opts
|
22
|
+
STDOUT.puts reset_color if text_color
|
23
|
+
else
|
24
|
+
text = color(text.inspect, text_color) if text_color
|
25
|
+
|
26
|
+
STDOUT.puts "\n\n#{text}"
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
# Puts out an error.
|
31
|
+
# The error is written to STDERR and colored in the error color.
|
32
|
+
def err(message, backtrace=nil)
|
33
|
+
STDERR.puts color(message, error_color)
|
34
|
+
backtrace.each {|line| line_color = (line[0..1] == '/') ? backtrace_local_color : backtrace_gem_color; STDERR.puts color(line, line_color)} unless backtrace.nil? || backtrace.empty?
|
35
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
require 'minitest/spec'
|
2
|
+
require 'minitest/autorun'
|
3
|
+
require 'turn/autorun'
|
4
|
+
|
5
|
+
begin
|
6
|
+
require 'hirb'
|
7
|
+
require 'wirble'
|
8
|
+
|
9
|
+
require 'debugger'
|
10
|
+
rescue LoadError
|
11
|
+
end
|
12
|
+
|
13
|
+
require 'support/colors'
|
14
|
+
require 'support/output'
|
15
|
+
require 'support/jams/jams'
|
16
|
+
|
17
|
+
require 'jam_script_highlighter'
|
18
|
+
|
19
|
+
def h text
|
20
|
+
JamScriptHighlighter.highlight text
|
21
|
+
end
|
22
|
+
|
23
|
+
def htmlr text
|
24
|
+
JamScriptHighlighter::Html.render text
|
25
|
+
end
|
26
|
+
|
27
|
+
Turn.config do |c|
|
28
|
+
# use one of output formats:
|
29
|
+
# :outline - turn's original case/test outline mode [default]
|
30
|
+
# :progress - indicates progress with progress bar
|
31
|
+
# :dotted - test/unit's traditional dot-progress mode
|
32
|
+
# :pretty - new pretty reporter
|
33
|
+
# :marshal - dump output as YAML (normal run mode only)
|
34
|
+
# :cue - interactive testing
|
35
|
+
c.format = :cue
|
36
|
+
# turn on invoke/execute tracing, enable full backtrace
|
37
|
+
c.trace = true
|
38
|
+
# use humanized test names (works only with :outline format)
|
39
|
+
c.natural = true
|
40
|
+
end
|
41
|
+
|
@@ -0,0 +1,65 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
describe JamScriptHighlighter::Html do
|
4
|
+
describe '.render' do
|
5
|
+
describe 'when passed a string' do
|
6
|
+
before do
|
7
|
+
@jam = ''
|
8
|
+
end
|
9
|
+
|
10
|
+
describe 'containing just a title' do
|
11
|
+
before do
|
12
|
+
@jam += "Some jam\n"
|
13
|
+
end
|
14
|
+
|
15
|
+
it 'should be wrapped in title tags' do
|
16
|
+
htmlr(@jam).must_match %r{<h1>Some jam</h1>}
|
17
|
+
end
|
18
|
+
|
19
|
+
describe 'containing metadata' do
|
20
|
+
before do
|
21
|
+
@jam += "by Some one\n"
|
22
|
+
end
|
23
|
+
|
24
|
+
it 'should wrap the metadata in a metadata div' do
|
25
|
+
htmlr(@jam).must_match %r{<h2>by Some one</h2>}
|
26
|
+
end
|
27
|
+
|
28
|
+
describe 'containing a section' do
|
29
|
+
before do
|
30
|
+
@jam += "\n* Verse\n"
|
31
|
+
end
|
32
|
+
|
33
|
+
it 'should contain a h4 with class of section' do
|
34
|
+
htmlr(@jam).must_match %r{<h4 class="section">Verse</h4>}
|
35
|
+
end
|
36
|
+
|
37
|
+
describe 'containing chords' do
|
38
|
+
before do
|
39
|
+
@jam += "C F | G\n"
|
40
|
+
end
|
41
|
+
|
42
|
+
it 'should wrap the chords in a containing div' do
|
43
|
+
htmlr(@jam).must_match %r{<div class="chords">\s*.*C F | G.*\s*<\/div>}
|
44
|
+
end
|
45
|
+
|
46
|
+
describe 'containing a structure' do
|
47
|
+
before do
|
48
|
+
@jam += "\n# Structure\nVerse, Chorus"
|
49
|
+
end
|
50
|
+
|
51
|
+
it 'should contain a h3 with a class of control' do
|
52
|
+
htmlr(@jam).must_match %r{<h3 class="control">Structure</h3>}
|
53
|
+
end
|
54
|
+
|
55
|
+
it 'should wrap the structure in a structure div' do
|
56
|
+
htmlr(@jam).must_match %r{<div class="structure">\s*.*Verse, Chorus.*\s*</div>}
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
describe JamScriptHighlighter::Line do
|
4
|
+
describe '.new' do
|
5
|
+
describe 'when passed just a string' do
|
6
|
+
it 'should return a line with the text property set to the string' do
|
7
|
+
JamScriptHighlighter::Line.new("some text").must_be_kind_of JamScriptHighlighter::Line
|
8
|
+
JamScriptHighlighter::Line.new("some text").text.must_equal 'some text'
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
describe 'when a second parameter is passed' do
|
13
|
+
it 'should return the appropriate subclass with the text property set' do
|
14
|
+
JamScriptHighlighter::Line.new("some text", :title).must_be_kind_of JamScriptHighlighter::LineTypes::TitleLine
|
15
|
+
JamScriptHighlighter::Line.new("some text", :title).text.must_equal 'some text'
|
16
|
+
|
17
|
+
JamScriptHighlighter::Line.new("some text", :variation).must_be_kind_of JamScriptHighlighter::LineTypes::VariationLine
|
18
|
+
JamScriptHighlighter::Line.new("some text", :variation).text.must_equal 'some text'
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,84 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
describe JamScriptHighlighter do
|
4
|
+
describe '.highlight' do
|
5
|
+
describe 'when passed a single line string' do
|
6
|
+
it 'should return an array containing the hash {:type => :title, :line => ...}' do
|
7
|
+
h('Some text').must_equal [{:type => :title, :line => 'Some text'}]
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
describe 'when passed a multiple line string' do
|
12
|
+
it 'should return an array of hashes, 1 for for each line' do
|
13
|
+
h("Some text\nby Some one").must_equal [
|
14
|
+
{
|
15
|
+
:type => :title,
|
16
|
+
:line => 'Some text'
|
17
|
+
}, {
|
18
|
+
:type => :metadata,
|
19
|
+
:line => 'by Some one'
|
20
|
+
}
|
21
|
+
]
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
describe 'when passed a string containing a section' do
|
26
|
+
it 'should return metadata lines until the first control line and type :other' do
|
27
|
+
h("Some text\nby Some one\n\n* Verse\nC F | G G").must_equal [
|
28
|
+
{
|
29
|
+
:type => :title,
|
30
|
+
:line => 'Some text'
|
31
|
+
}, {
|
32
|
+
:type => :metadata,
|
33
|
+
:line => 'by Some one'
|
34
|
+
}, {
|
35
|
+
:type => :empty,
|
36
|
+
:line => ''
|
37
|
+
}, {
|
38
|
+
:type => :section,
|
39
|
+
:line => 'Verse'
|
40
|
+
}, {
|
41
|
+
:type => :other,
|
42
|
+
:line => 'C F | G G'
|
43
|
+
}
|
44
|
+
]
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
describe '.type_of' do
|
50
|
+
describe 'when passed a line that starts with a #' do
|
51
|
+
it 'should return :control' do
|
52
|
+
JamScriptHighlighter.type_of('# Some line').must_equal :control
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
describe 'when passed a line that starts with a !' do
|
57
|
+
it 'should return :note' do
|
58
|
+
JamScriptHighlighter.type_of('! Some line').must_equal :note
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
describe 'when passed a line that starts with a *' do
|
63
|
+
it 'should return :section' do
|
64
|
+
JamScriptHighlighter.type_of('* Some line').must_equal :section
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
describe 'when the line is empty' do
|
69
|
+
it 'should return :empty' do
|
70
|
+
JamScriptHighlighter.type_of('').must_equal :empty
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
describe 'otherwise' do
|
75
|
+
it 'should return :other' do
|
76
|
+
JamScriptHighlighter.type_of('oeiruqw').must_equal :other
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
describe 'repeats?' do
|
82
|
+
|
83
|
+
end
|
84
|
+
end
|
metadata
ADDED
@@ -0,0 +1,173 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: jam_script_highlighter
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Adam Phillips
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-01-20 00:00:00.000000000Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: pdfkit
|
16
|
+
requirement: &2151834560 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *2151834560
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: minitest
|
27
|
+
requirement: &2151833680 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
type: :development
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *2151833680
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: turn
|
38
|
+
requirement: &2151832060 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ! '>='
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '0'
|
44
|
+
type: :development
|
45
|
+
prerelease: false
|
46
|
+
version_requirements: *2151832060
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: debugger
|
49
|
+
requirement: &2151830180 !ruby/object:Gem::Requirement
|
50
|
+
none: false
|
51
|
+
requirements:
|
52
|
+
- - ! '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
type: :development
|
56
|
+
prerelease: false
|
57
|
+
version_requirements: *2151830180
|
58
|
+
- !ruby/object:Gem::Dependency
|
59
|
+
name: guard-minitest
|
60
|
+
requirement: &2151829020 !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: *2151829020
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rb-fsevent
|
71
|
+
requirement: &2151826120 !ruby/object:Gem::Requirement
|
72
|
+
none: false
|
73
|
+
requirements:
|
74
|
+
- - ~>
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: 0.9.1
|
77
|
+
type: :development
|
78
|
+
prerelease: false
|
79
|
+
version_requirements: *2151826120
|
80
|
+
- !ruby/object:Gem::Dependency
|
81
|
+
name: hirb
|
82
|
+
requirement: &2151824240 !ruby/object:Gem::Requirement
|
83
|
+
none: false
|
84
|
+
requirements:
|
85
|
+
- - ! '>='
|
86
|
+
- !ruby/object:Gem::Version
|
87
|
+
version: '0'
|
88
|
+
type: :development
|
89
|
+
prerelease: false
|
90
|
+
version_requirements: *2151824240
|
91
|
+
- !ruby/object:Gem::Dependency
|
92
|
+
name: wirble
|
93
|
+
requirement: &2151823080 !ruby/object:Gem::Requirement
|
94
|
+
none: false
|
95
|
+
requirements:
|
96
|
+
- - ! '>='
|
97
|
+
- !ruby/object:Gem::Version
|
98
|
+
version: '0'
|
99
|
+
type: :development
|
100
|
+
prerelease: false
|
101
|
+
version_requirements: *2151823080
|
102
|
+
description: Simple JamScript highlighter
|
103
|
+
email: ''
|
104
|
+
executables:
|
105
|
+
- jam_as_html
|
106
|
+
- jam_as_pdf
|
107
|
+
extensions: []
|
108
|
+
extra_rdoc_files: []
|
109
|
+
files:
|
110
|
+
- .gemfiles/Gemfile.travis
|
111
|
+
- .travis.yml
|
112
|
+
- Gemfile
|
113
|
+
- Gemfile.lock
|
114
|
+
- Guardfile
|
115
|
+
- Rakefile
|
116
|
+
- bin/jam_as_html
|
117
|
+
- bin/jam_as_pdf
|
118
|
+
- jam_script_highlighter.gemspec
|
119
|
+
- lib/jam_script_highlighter.rb
|
120
|
+
- lib/jam_script_highlighter/html.rb
|
121
|
+
- lib/jam_script_highlighter/html/tag_helpers.rb
|
122
|
+
- lib/jam_script_highlighter/line.rb
|
123
|
+
- lib/jam_script_highlighter/line_types.rb
|
124
|
+
- lib/jam_script_highlighter/line_types/control_line.rb
|
125
|
+
- lib/jam_script_highlighter/line_types/empty_line.rb
|
126
|
+
- lib/jam_script_highlighter/line_types/metadata_line.rb
|
127
|
+
- lib/jam_script_highlighter/line_types/note_line.rb
|
128
|
+
- lib/jam_script_highlighter/line_types/other_line.rb
|
129
|
+
- lib/jam_script_highlighter/line_types/section_line.rb
|
130
|
+
- lib/jam_script_highlighter/line_types/title_line.rb
|
131
|
+
- lib/jam_script_highlighter/line_types/variation_line.rb
|
132
|
+
- lib/jam_script_highlighter/version.rb
|
133
|
+
- readme.md
|
134
|
+
- test/support/colors.rb
|
135
|
+
- test/support/helpers/section_helpers.rb
|
136
|
+
- test/support/jams/jams.rb
|
137
|
+
- test/support/output.rb
|
138
|
+
- test/test_helper.rb
|
139
|
+
- test/unit/jam_script_highlighter/html_test.rb
|
140
|
+
- test/unit/jam_script_highlighter/line_test.rb
|
141
|
+
- test/unit/jam_script_highlighter_test.rb
|
142
|
+
homepage: ''
|
143
|
+
licenses: []
|
144
|
+
post_install_message:
|
145
|
+
rdoc_options:
|
146
|
+
- --charset=UTF-8
|
147
|
+
require_paths:
|
148
|
+
- lib
|
149
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
150
|
+
none: false
|
151
|
+
requirements:
|
152
|
+
- - ! '>='
|
153
|
+
- !ruby/object:Gem::Version
|
154
|
+
version: '0'
|
155
|
+
segments:
|
156
|
+
- 0
|
157
|
+
hash: -1597204558210840588
|
158
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
159
|
+
none: false
|
160
|
+
requirements:
|
161
|
+
- - ! '>='
|
162
|
+
- !ruby/object:Gem::Version
|
163
|
+
version: '0'
|
164
|
+
segments:
|
165
|
+
- 0
|
166
|
+
hash: -1597204558210840588
|
167
|
+
requirements: []
|
168
|
+
rubyforge_project:
|
169
|
+
rubygems_version: 1.8.10
|
170
|
+
signing_key:
|
171
|
+
specification_version: 3
|
172
|
+
summary: jam_script_highlighter-0.0.1
|
173
|
+
test_files: []
|