Sha256: ae83bec6a18102664cb7f9912c52c9a99cd07672f4c97f3b1439443f2e0e2c87

Contents?: true

Size: 1.4 KB

Versions: 2

Compression:

Stored size: 1.4 KB

Contents

begin
  require 'open3_detach'
rescue LoadError
  require 'open3'
end

module GitHub
  module Markup
    extend self
    @@markups = {}

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

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

    def command(command, regexp, &block)
      command = command.to_s

      if File.exists?(file = File.dirname(__FILE__) + "/commands/#{command}")
        command = file
      end

      return if !system("which #{command} > /dev/null")

      add_markup(regexp) do |content|
        rendered = execute(command, content)
        block ? block.call(rendered) : rendered
      end
    end

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

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

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

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

    # Define markups
    instance_eval File.read(File.dirname(__FILE__) + '/markups.rb')
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
github-markup-0.1.6 lib/github/markup.rb
github-markup-0.1.5 lib/github/markup.rb