module GitHub::Markup

def add_markup(regexp, &block)

def add_markup(regexp, &block)
  @@markups[regexp] = block
end

def can_render?(filename)

def can_render?(filename)
  !!renderer(filename)
end

def command(command, regexp, &block)

def command(command, regexp, &block)
  command = command.to_s
  if File.exists?(file = File.dirname(__FILE__) + "/commands/#{command}")
    command = file
  end
  add_markup(regexp) do |content|
    rendered = execute(command, content)
    rendered = rendered.to_s.empty? ? content : rendered
    block ? block.call(rendered) : rendered
  end
end

def execute(command, target)

def execute(command, target)
  out = ''
  Open3.popen3(command) do |stdin, stdout, _|
    stdin.puts target
    stdin.close
    out = stdout.read
  end
  out.gsub("\r", '')
rescue Errno::EPIPE
  ""
end

def markup(file, pattern, &block)

def markup(file, pattern, &block)
  require file.to_s
  add_markup(pattern, &block)
rescue LoadError
  nil
end

def render(filename, content)

def render(filename, content)
  if proc = renderer(filename)
    proc[content]
  else
    content
  end
end

def renderer(filename)

def renderer(filename)
  @@markups.each do |key, value|
    if Regexp.compile("\\.(#{key})$") =~ filename
      return value
    end
  end
  nil
end