Class: Carmine

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/carmine.rb,
lib/carmine/version.rb,
lib/carmine/conversion.rb

Defined Under Namespace

Modules: Conversion, Version Classes: Error

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Carmine

Public: Initialize the carmine object.

options - The default options to use when colorizing.

:formatter (default: :html)
:lexer     (default: :plain)


61
62
63
64
65
66
# File 'lib/carmine.rb', line 61

def initialize(options = {})
  @defaults = {
    :formatter => :html,
    :lexer     => :plain
  }.merge options
end

Instance Attribute Details

#defaultsObject

Public: Gets/Sets the client default options.



54
55
56
# File 'lib/carmine.rb', line 54

def defaults
  @defaults
end

Class Method Details

.method_missing(name, *args, &block) ⇒ Object

Public: Delegates the calls to the #instance_methods.

Examples

Carmine.colorize "puts 'Hello World!'", :lexer => :ruby
# Calls `Colorize.send(:instance).colorize(*args)`.

Carmine.pygmentize "puts 'Hello World!'", :lexer => :ruby
# Calls `Colorize.send(:pygmentize).colorize(*args)`.

Returns the delegated method call result.



35
36
37
38
39
40
41
# File 'lib/carmine.rb', line 35

def method_missing(name, *args, &block)
  if instance.respond_to? name
    instance.send name, *args, &block
  else
    super
  end
end

.versionObject

Public: The version of the client.

Returns a String of the dot joined version parts.



11
12
13
# File 'lib/carmine/version.rb', line 11

def version
  [Version::Major, Version::Minor, Version::Patch].join('.')
end

Instance Method Details

#colorize(code = nil, options = {}) ⇒ Object Also known as: pygmentize

Public: Colorizes the given code using the specified :formatter and :lexer.

code - The code to colorize (default: nil). options - The option passed to the pygmentize.me api, the optinal options

take their defaults from the hash passed in the constructor.
:code      - Can be specified here (optional).
:formatter - The name of the formatter to use (optional).
:lexer     - The name of the lexer to use (optiona).

Examples

colorize "puts 'Hello World!'", :lexer => :ruby, :formatter => :text
# => "puts 'Hello World!'"

colorize :code => "puts 'Hello World!'", :lexer => :ruby, :formatter => :text
# => "puts 'Hello World!'"

Returns the colorized code. Yields the colorized code if there is a block given.

Raises:

  • (ArgumentError)


87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/carmine.rb', line 87

def colorize(code = nil, options = {})
  params        = @defaults.merge options
  params[:code] = code.respond_to?(:read) ? code.read : code unless code.nil?

  raise ArgumentError, "Argument or option :code missing" if params[:code].nil?

  response = post "/api/formatter/#{params.delete :formatter}", :query => params

  raise Error, response unless response.success?

  if block_given?
    yield response.body
  else
    response.body
  end
end