all repos — markup @ v0.1.4

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?(command) && !command.include?('/')
31        command = File.dirname(__FILE__) + '/commands/' + command.to_s
32      end
33
34      add_markup(regexp) do |content|
35        rendered = execute(command, content)
36        block ? block.call(rendered) : rendered
37      end
38    end
39
40    def add_markup(regexp, &block)
41      @@markups[regexp] = block
42    end
43
44    def can_render?(filename)
45      !!renderer(filename)
46    end
47
48    def renderer(filename)
49      @@markups.each do |key, value|
50        if Regexp.compile("\\.(#{key})$") =~ filename
51          return value
52        end
53      end
54      nil
55    end
56
57    def execute(command, target)
58      return target if !system("which #{command} > /dev/null")
59
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