class GitHub::Markup::CommandImplementation

def call_block(rendered, content)

def call_block(rendered, content)
  if block && block.arity == 2
    block.call(rendered, content)
  elsif block
    block.call(rendered)
  else
    rendered
  end
end

def execute(command, target)

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

def execute(command, target)

def execute(command, target)
  output = Open3.popen3(*command) do |stdin, stdout, stderr, wait_thr|
    stdin.puts target
    stdin.close
    if wait_thr.value.success?
      stdout.readlines
    else
      raise CommandError.new(stderr.readlines.join('').strip)
    end
  end
  sanitize(output.join(''), target.encoding)
end

def initialize(regexp, languages, command, name, &block)

def initialize(regexp, languages, command, name, &block)
  super(regexp, languages)
  @command = command.to_s
  @block = block
  @name = name
end

def render(filename, content)

def render(filename, content)
  rendered = execute(command, content)
  rendered = rendered.to_s.empty? ? content : rendered
  call_block(rendered, content)
end

def sanitize(input, encoding)

def sanitize(input, encoding)
  input.gsub("\r", '').force_encoding(encoding)
end