Class: Albino

Inherits:
Object
  • Object
show all
Defined in:
lib/albino.rb,
lib/albino/process.rb

Overview

GitHub // github.com

Defined Under Namespace

Classes: Process, ShellArgumentError

Constant Summary collapse

VERSION =
'1.2.3'

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(target, lexer = :text, format = :html, encoding = self.class.default_encoding) ⇒ Albino

Returns a new instance of Albino.



62
63
64
65
# File 'lib/albino.rb', line 62

def initialize(target, lexer = :text, format = :html, encoding = self.class.default_encoding)
  @target  = target
  @options = { :l => lexer, :f => format, :O => "encoding=#{encoding}" }
end

Class Attribute Details

.binObject

Returns the value of attribute bin.



51
52
53
# File 'lib/albino.rb', line 51

def bin
  @bin
end

.default_encodingObject

Returns the value of attribute default_encoding.



51
52
53
# File 'lib/albino.rb', line 51

def default_encoding
  @default_encoding
end

.timeout_thresholdObject

Returns the value of attribute timeout_threshold.



51
52
53
# File 'lib/albino.rb', line 51

def timeout_threshold
  @timeout_threshold
end

Class Method Details

.colorize(*args) ⇒ Object



58
59
60
# File 'lib/albino.rb', line 58

def self.colorize(*args)
  new(*args).colorize
end

Instance Method Details

#binObject



106
107
108
# File 'lib/albino.rb', line 106

def bin
  self.class.bin
end

#colorize(options = {}) ⇒ Object Also known as: to_s



75
76
77
# File 'lib/albino.rb', line 75

def colorize(options = {})
  execute(options).out
end

#convert_options(options = {}) ⇒ Object



80
81
82
83
84
85
# File 'lib/albino.rb', line 80

def convert_options(options = {})
  @options.merge(options).inject([]) do |memo, (flag, value)|
    validate_shell_args(flag.to_s, value.to_s)
    memo << "-#{flag}" << value.to_s
  end
end

#execute(options = {}) ⇒ Object



67
68
69
70
71
72
73
# File 'lib/albino.rb', line 67

def execute(options = {})
  proc_options = {}
  proc_options[:timeout] = options.delete(:timeout) || self.class.timeout_threshold
  command = convert_options(options)
  command.unshift(bin)
  Process.new(command, env={}, proc_options.merge(:input => write_target))
end

#validate_shell_args(flag, value) ⇒ Object



97
98
99
100
101
102
103
104
# File 'lib/albino.rb', line 97

def validate_shell_args(flag, value)
  if flag !~ /^[a-z]+$/i
    raise ShellArgumentError, "Flag is invalid: #{flag.inspect}"
  end
  if value !~ /^[a-z0-9\-\_\+\=\#\,\s]+$/i
    raise ShellArgumentError, "Flag value is invalid: -#{flag} #{value.inspect}"
  end
end

#write_targetObject



87
88
89
90
91
92
93
94
95
# File 'lib/albino.rb', line 87

def write_target
  if @target.respond_to?(:read)
    out = @target.read
    @target.close
    out
  else
    @target.to_s
  end
end