class GitHub::Markup::CommandImplementation

Attributes

block[R]
command[R]
name[R]

Public Class Methods

new(regexp, command, name, &block) click to toggle source
Calls superclass method
# File lib/github/markup/command_implementation.rb, line 17
def initialize(regexp, command, name, &block)
  super regexp
  @command = command.to_s
  @block = block
  @name = name
end

Public Instance Methods

render(content) click to toggle source
# File lib/github/markup/command_implementation.rb, line 24
def render(content)
  rendered = execute(command, content)
  rendered = rendered.to_s.empty? ? content : rendered
  call_block(rendered, content)
end

Private Instance Methods

call_block(rendered, content) click to toggle source
# File lib/github/markup/command_implementation.rb, line 31
def call_block(rendered, content)
  if block && block.arity == 2
    block.call(rendered, content)
  elsif block
    block.call(rendered)
  else
    rendered
  end
end
execute(command, target) click to toggle source
# File lib/github/markup/command_implementation.rb, line 42
def execute(command, target)
  spawn = POSIX::Spawn::Child.new(*command, :input => target)
  if spawn.status.success?
    sanitize(spawn.out, target.encoding)
  else
    raise CommandError.new(spawn.err.strip)
  end
end
sanitize(input, encoding) click to toggle source
# File lib/github/markup/command_implementation.rb, line 61
def sanitize(input, encoding)
  input.gsub("\r", '').force_encoding(encoding)
end