module GitHub::Markup

Constants

VERSION
Version

Public Instance Methods

add_markup(regexp, &block) click to toggle source
# File lib/github/markup.rb, line 55
def add_markup(regexp, &block)
  @@markups[regexp] = block
end
can_render?(filename) click to toggle source
# File lib/github/markup.rb, line 59
def can_render?(filename)
  !!renderer(filename)
end
command(command, regexp, &block) click to toggle source
# File lib/github/markup.rb, line 30
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

    if block && block.arity == 2
      # If the block takes two arguments, pass new content and old
      # content.
      block.call(rendered, content)
    elsif block
      # One argument is just the new content.
      block.call(rendered)
    else
      # No block? No problem!
      rendered
    end
  end
end
execute(command, target) click to toggle source
# File lib/github/markup.rb, line 81
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
markup(file, pattern, &block) click to toggle source
# File lib/github/markup.rb, line 22
def markup(file, pattern, &block)
  require file.to_s
  add_markup(pattern, &block)
  true
rescue LoadError
  false
end
render(filename, content = nil) click to toggle source
# File lib/github/markup.rb, line 12
def render(filename, content = nil)
  content ||= File.read(filename)

  if proc = renderer(filename)
    proc[content]
  else
    content
  end
end
renderer(filename) click to toggle source
# File lib/github/markup.rb, line 63
def renderer(filename)
  @@markups.each do |key, value|
    if Regexp.compile("\\.(#{key})$") =~ filename
      return value
    end
  end
  nil
end
renderer_name(filename) click to toggle source
# File lib/github/markup.rb, line 72
def renderer_name(filename)
  @@markups.each do |key, value|
    if Regexp.compile("\\.(#{key})$") =~ filename
      return key
    end
  end
  nil
end