carmine 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile ADDED
@@ -0,0 +1,11 @@
1
+ source :gemcutter
2
+
3
+ gem 'httparty'
4
+
5
+ group :development do
6
+ gem 'rake'
7
+ gem 'rspec'
8
+ gem 'guard'
9
+ gem 'guard-rspec'
10
+ gem 'jeweler'
11
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,45 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ diff-lcs (1.1.3)
5
+ ffi (1.0.11)
6
+ git (1.2.5)
7
+ guard (1.0.0)
8
+ ffi (>= 0.5.0)
9
+ thor (~> 0.14.6)
10
+ guard-rspec (0.6.0)
11
+ guard (>= 0.10.0)
12
+ httparty (0.8.1)
13
+ multi_json
14
+ multi_xml
15
+ jeweler (1.8.3)
16
+ bundler (~> 1.0)
17
+ git (>= 1.2.5)
18
+ rake
19
+ rdoc
20
+ json (1.6.5)
21
+ multi_json (1.1.0)
22
+ multi_xml (0.4.1)
23
+ rake (0.9.2.2)
24
+ rdoc (3.12)
25
+ json (~> 1.4)
26
+ rspec (2.8.0)
27
+ rspec-core (~> 2.8.0)
28
+ rspec-expectations (~> 2.8.0)
29
+ rspec-mocks (~> 2.8.0)
30
+ rspec-core (2.8.0)
31
+ rspec-expectations (2.8.0)
32
+ diff-lcs (~> 1.1.2)
33
+ rspec-mocks (2.8.0)
34
+ thor (0.14.6)
35
+
36
+ PLATFORMS
37
+ ruby
38
+
39
+ DEPENDENCIES
40
+ guard
41
+ guard-rspec
42
+ httparty
43
+ jeweler
44
+ rake
45
+ rspec
data/Guardfile ADDED
@@ -0,0 +1,4 @@
1
+ guard :rspec, :cli => '-f doc -r "spec_helper" -c' do
2
+ watch(%r{^lib/(.+)\.rb$}) { 'spec' }
3
+ watch(%r{^spec/(.+)\.rb$}) { 'spec' }
4
+ end
data/LICENSE ADDED
@@ -0,0 +1,19 @@
1
+ Copyright (C) 2012 by Genadi Samokovarov
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,3 @@
1
+ Dir['tasks/**/*.rake'].each do |taskfile|
2
+ load taskfile
3
+ end
data/Readme.markdown ADDED
@@ -0,0 +1,35 @@
1
+ Carmine is a [ruby](http://ruby-lang.org) client for [pygmentize.me](http://pygmentize.me) - your friendly syntax highlighting service.
2
+
3
+ # Usage
4
+
5
+ ```ruby
6
+ require 'carmine'
7
+
8
+ Carmine.colorize 'puts "Hello World!"', :lexer => :ruby, :formatter => :terminal256
9
+ ```
10
+
11
+ # Installation
12
+
13
+ gem install carmine
14
+
15
+ # License
16
+
17
+ Copyright (C) 2012 by Genadi Samokovarov
18
+
19
+ Permission is hereby granted, free of charge, to any person obtaining a copy
20
+ of this software and associated documentation files (the "Software"), to deal
21
+ in the Software without restriction, including without limitation the rights
22
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
23
+ copies of the Software, and to permit persons to whom the Software is
24
+ furnished to do so, subject to the following conditions:
25
+
26
+ The above copyright notice and this permission notice shall be included in
27
+ all copies or substantial portions of the Software.
28
+
29
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
30
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
31
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
32
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
33
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
34
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
35
+ THE SOFTWARE.
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
data/carmine.gemspec ADDED
@@ -0,0 +1,70 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = "carmine"
8
+ s.version = "0.1.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Genadi Samokovarov"]
12
+ s.date = "2012-02-28"
13
+ s.description = "Ruby client for pygmentize.me"
14
+ s.email = "gsamokovarov@gmail.com"
15
+ s.extra_rdoc_files = [
16
+ "LICENSE"
17
+ ]
18
+ s.files = [
19
+ "Gemfile",
20
+ "Gemfile.lock",
21
+ "Guardfile",
22
+ "LICENSE",
23
+ "Rakefile",
24
+ "Readme.markdown",
25
+ "VERSION",
26
+ "carmine.gemspec",
27
+ "lib/carmine.rb",
28
+ "lib/carmine/version.rb",
29
+ "spec/carmine/version_spec.rb",
30
+ "spec/carmine_spec.rb",
31
+ "spec/spec_helper.rb",
32
+ "tasks/gem.rake",
33
+ "tasks/spec.rake"
34
+ ]
35
+ s.licenses = ["MIT"]
36
+ s.require_paths = ["lib"]
37
+ s.rubygems_version = "1.8.10"
38
+ s.summary = "Ruby client for pygmentize.me"
39
+
40
+ if s.respond_to? :specification_version then
41
+ s.specification_version = 3
42
+
43
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
44
+ s.add_runtime_dependency(%q<httparty>, [">= 0"])
45
+ s.add_development_dependency(%q<rake>, [">= 0"])
46
+ s.add_development_dependency(%q<rspec>, [">= 0"])
47
+ s.add_development_dependency(%q<guard>, [">= 0"])
48
+ s.add_development_dependency(%q<guard-rspec>, [">= 0"])
49
+ s.add_development_dependency(%q<jeweler>, [">= 0"])
50
+ s.add_runtime_dependency(%q<httparty>, [">= 0"])
51
+ else
52
+ s.add_dependency(%q<httparty>, [">= 0"])
53
+ s.add_dependency(%q<rake>, [">= 0"])
54
+ s.add_dependency(%q<rspec>, [">= 0"])
55
+ s.add_dependency(%q<guard>, [">= 0"])
56
+ s.add_dependency(%q<guard-rspec>, [">= 0"])
57
+ s.add_dependency(%q<jeweler>, [">= 0"])
58
+ s.add_dependency(%q<httparty>, [">= 0"])
59
+ end
60
+ else
61
+ s.add_dependency(%q<httparty>, [">= 0"])
62
+ s.add_dependency(%q<rake>, [">= 0"])
63
+ s.add_dependency(%q<rspec>, [">= 0"])
64
+ s.add_dependency(%q<guard>, [">= 0"])
65
+ s.add_dependency(%q<guard-rspec>, [">= 0"])
66
+ s.add_dependency(%q<jeweler>, [">= 0"])
67
+ s.add_dependency(%q<httparty>, [">= 0"])
68
+ end
69
+ end
70
+
data/lib/carmine.rb ADDED
@@ -0,0 +1,105 @@
1
+ require 'httparty'
2
+
3
+ class Carmine
4
+ include HTTParty
5
+
6
+ # Public: The base error for the library.
7
+ #
8
+ # Raised when there is something wrong with the colorization request.
9
+ class Error < StandardError
10
+ attr_reader :response
11
+
12
+ def initialize(response)
13
+ @response = response
14
+ end
15
+
16
+ def to_s
17
+ @response.body.match(/\(.*?\)/, &:to_s).gsub(/[()]/, "")
18
+ end
19
+ end
20
+
21
+ class << self
22
+ # Public: Delegates the calls to '#colorize', '#pygmentize' and #defaults.
23
+ #
24
+ # Examples
25
+ #
26
+ # Carmine.colorize "puts 'Hello World!'", :lexer => :ruby
27
+ # Carmine.pygmentize "puts 'Hello World!'", :lexer => :ruby
28
+ def method_missing(name, *args, &block)
29
+ if instance.respond_to? name
30
+ instance.send name, *args, &block
31
+ else
32
+ super
33
+ end
34
+ end
35
+
36
+ private
37
+
38
+ def instance
39
+ @instance ||= new
40
+ end
41
+ end
42
+
43
+ base_uri 'pygmentize.me'
44
+
45
+ # Public: Gets/Sets the client default options.
46
+ attr_accessor :defaults
47
+
48
+ # Public: Initialize the carmine object.
49
+ #
50
+ # options - The default options to use when colorizing.
51
+ # :formatter (default: :html)
52
+ # :lexer (default: :plain)
53
+ def initialize(options = {})
54
+ @defaults = {
55
+ :formatter => :html,
56
+ :lexer => :plain
57
+ }.merge options
58
+ end
59
+
60
+ # Public: Colorizes the given code using the specified :formatter and :lexer.
61
+ #
62
+ # code - The code to colorize (default: nil).
63
+ # options - The option passed to the pygmentize.me api, the optinal options
64
+ # take their defaults from the hash passed in the constructor.
65
+ # :code - Can be specified here (optional).
66
+ # :formatter - The name of the formatter to use (optional).
67
+ # :lexer - The name of the lexer to use (optiona).
68
+ #
69
+ # Examples
70
+ #
71
+ # colorize "puts 'Hello World!'", :lexer => :ruby, :formatter => :text
72
+ # # => "puts 'Hello World!'"
73
+ #
74
+ # colorize :code => "puts 'Hello World!'", :lexer => :ruby, :formatter => :text
75
+ # # => "puts 'Hello World!'"
76
+ #
77
+ # Returns the colorized code.
78
+ # Yields the colorized code if there is a block given.
79
+ def colorize(code = nil, options = {})
80
+ params = @defaults.merge options
81
+ params[:code] = code.respond_to?(:read) ? code.read : code unless code.nil?
82
+
83
+ raise ArgumentError, "Argument or option :code missing" if params[:code].nil?
84
+
85
+ response = post "/api/formatter/#{options[:formatter]}", :query => params
86
+
87
+ raise Error, response unless response.success?
88
+
89
+ if block_given?
90
+ yield response.body
91
+ else
92
+ response.body
93
+ end
94
+ end
95
+
96
+ alias :pygmentize :colorize
97
+
98
+ private
99
+
100
+ def post(path, options = {})
101
+ self.class.post path, @defaults.merge(options)
102
+ end
103
+ end
104
+
105
+ require 'carmine/version'
@@ -0,0 +1,15 @@
1
+ class Carmine
2
+ module Version
3
+ Major, Minor, Patch =
4
+ File.read(File.expand_path('../../../VERSION', __FILE__)).split('.')
5
+ end
6
+
7
+ class << self
8
+ # Public: The version of the client.
9
+ #
10
+ # Returns a String of the dot joined version parts.
11
+ def version
12
+ [Version::Major, Version::Minor, Version::Patch].join('.')
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,7 @@
1
+ describe Carmine::Version do
2
+ [:Major, :Minor, :Patch].each do |const|
3
+ it "should have #{const} part constant" do
4
+ Carmine::Version.constants.should include(const)
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,49 @@
1
+ describe Carmine do
2
+ describe '#initialize' do
3
+ it 'should accept options used as defaults' do
4
+ carmine = Carmine.new :lexer => :ruby, :formatter => :latex
5
+
6
+ carmine.defaults[:lexer].should eq(:ruby)
7
+ carmine.defaults[:formatter].should eq(:latex)
8
+ end
9
+ end
10
+
11
+ %w{ :: # }.each do |access|
12
+ describe "#{access}colorize" do
13
+ let(:carmine) { access == '::' ? Carmine : Carmine.new }
14
+
15
+ it 'should colorize code with valid lexer and formatter' do
16
+ carmine.colorize('puts "Hello World!"', :lexer => :ruby, :formatter => 'html').should ==
17
+ %Q{<div class="highlight"><pre><span class="nb">puts</span> <span class="s2">&quot;Hello World!&quot;</span>\n</pre></div>\n}
18
+ end
19
+
20
+
21
+ it 'should throw Carmine::Error on server side errors' do
22
+ expect {
23
+ carmine.colorize 'pass', :lexer => :python, :formatter => :invalid
24
+ }.to raise_error(Carmine::Error)
25
+ end
26
+
27
+ it 'should raise ArgumentError on missing code' do
28
+ expect { carmine.colorize }.to raise_error(ArgumentError)
29
+ end
30
+ end
31
+ end
32
+
33
+ describe '#defaults' do
34
+ it 'should get or set instance wide default options' do
35
+ carmine = Carmine.new
36
+ carmine.defaults = { :lexer => :ruby, :formatter => :latex }
37
+
38
+ carmine.defaults[:lexer].should eq(:ruby)
39
+ carmine.defaults[:formatter].should eq(:latex)
40
+ end
41
+ end
42
+
43
+ describe '::version' do
44
+ it 'should consist of Major, Minor and Patch joined by a dot' do
45
+ [Carmine::Version::Major, Carmine::Version::Minor, Carmine::Version::Patch].join('.').should \
46
+ eq(Carmine.version)
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,8 @@
1
+ $LOAD_PATH << File.expand_path('../../lib', __FILE__)
2
+
3
+ require 'carmine'
4
+ require 'rspec'
5
+
6
+ RSpec.configure do |config|
7
+ config.fail_fast = true
8
+ end
data/tasks/gem.rake ADDED
@@ -0,0 +1,16 @@
1
+ $LOAD_PATH << File.expand_path("../../lib", __FILE__)
2
+
3
+ require 'jeweler'
4
+ require 'carmine/version'
5
+
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = 'carmine'
8
+ gem.version = Carmine.version
9
+ gem.email = 'gsamokovarov@gmail.com'
10
+ gem.authors = ['Genadi Samokovarov']
11
+ gem.license = 'MIT'
12
+ gem.summary = 'Ruby client for pygmentize.me'
13
+ gem.description = 'Ruby client for pygmentize.me'
14
+
15
+ gem.add_dependency 'httparty'
16
+ end
data/tasks/spec.rake ADDED
@@ -0,0 +1,7 @@
1
+ require 'rspec/core/rake_task'
2
+
3
+ desc 'Run the specifiaction examples'
4
+ RSpec::Core::RakeTask.new do |task|
5
+ task.rspec_opts = %w{-r "spec_helper" --color --format documentation}
6
+ task.pattern = FileList['spec/**/*_spec.rb']
7
+ end
metadata ADDED
@@ -0,0 +1,138 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: carmine
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Genadi Samokovarov
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-02-28 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: httparty
16
+ requirement: &12191080 !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: *12191080
25
+ - !ruby/object:Gem::Dependency
26
+ name: rake
27
+ requirement: &12186440 !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: *12186440
36
+ - !ruby/object:Gem::Dependency
37
+ name: rspec
38
+ requirement: &12185540 !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: *12185540
47
+ - !ruby/object:Gem::Dependency
48
+ name: guard
49
+ requirement: &12182400 !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: *12182400
58
+ - !ruby/object:Gem::Dependency
59
+ name: guard-rspec
60
+ requirement: &12181720 !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: *12181720
69
+ - !ruby/object:Gem::Dependency
70
+ name: jeweler
71
+ requirement: &12181060 !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ! '>='
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: *12181060
80
+ - !ruby/object:Gem::Dependency
81
+ name: httparty
82
+ requirement: &12180440 !ruby/object:Gem::Requirement
83
+ none: false
84
+ requirements:
85
+ - - ! '>='
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ type: :runtime
89
+ prerelease: false
90
+ version_requirements: *12180440
91
+ description: Ruby client for pygmentize.me
92
+ email: gsamokovarov@gmail.com
93
+ executables: []
94
+ extensions: []
95
+ extra_rdoc_files:
96
+ - LICENSE
97
+ files:
98
+ - Gemfile
99
+ - Gemfile.lock
100
+ - Guardfile
101
+ - LICENSE
102
+ - Rakefile
103
+ - Readme.markdown
104
+ - VERSION
105
+ - carmine.gemspec
106
+ - lib/carmine.rb
107
+ - lib/carmine/version.rb
108
+ - spec/carmine/version_spec.rb
109
+ - spec/carmine_spec.rb
110
+ - spec/spec_helper.rb
111
+ - tasks/gem.rake
112
+ - tasks/spec.rake
113
+ homepage:
114
+ licenses:
115
+ - MIT
116
+ post_install_message:
117
+ rdoc_options: []
118
+ require_paths:
119
+ - lib
120
+ required_ruby_version: !ruby/object:Gem::Requirement
121
+ none: false
122
+ requirements:
123
+ - - ! '>='
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
126
+ required_rubygems_version: !ruby/object:Gem::Requirement
127
+ none: false
128
+ requirements:
129
+ - - ! '>='
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ requirements: []
133
+ rubyforge_project:
134
+ rubygems_version: 1.8.10
135
+ signing_key:
136
+ specification_version: 3
137
+ summary: Ruby client for pygmentize.me
138
+ test_files: []