all repos — markup @ v0.1.5

The code we use to render README.your_favorite_markup

lib/github/markup.rb (view raw)

 1begin
 2  require 'open3_detach'
 3rescue LoadError
 4  require 'open3'
 5end
 6
 7module GitHub
 8  module Markup
 9    extend self
10    @@markups = {}
11
12    def render(filename, content)
13      if proc = renderer(filename)
14        proc[content]
15      else
16        content
17      end
18    end
19
20    def markup(file, pattern, &block)
21      require file.to_s
22      add_markup(pattern, &block)
23    rescue LoadError
24      nil
25    end
26
27    def command(command, regexp, &block)
28      command = command.to_s
29
30      if File.exists?(file = File.dirname(__FILE__) + "/commands/#{command}")
31        command = file
32      end
33
34      return if !system("which #{command} > /dev/null")
35
36      add_markup(regexp) do |content|
37        rendered = execute(command, content)
38        block ? block.call(rendered) : rendered
39      end
40    end
41
42    def add_markup(regexp, &block)
43      @@markups[regexp] = block
44    end
45
46    def can_render?(filename)
47      !!renderer(filename)
48    end
49
50    def renderer(filename)
51      @@markups.each do |key, value|
52        if Regexp.compile("\\.(#{key})$") =~ filename
53          return value
54        end
55      end
56      nil
57    end
58
59    def execute(command, target)
60      out = ''
61      Open3.popen3(command) do |stdin, stdout, _|
62        stdin.puts target
63        stdin.close
64        out = stdout.read
65      end
66      out.gsub("\r", '')
67    end
68
69    # Define markups
70    instance_eval File.read(File.dirname(__FILE__) + '/markups.rb')
71  end
72end