all repos — markup @ 4c10ef7ce2833003a79832f368c89509fd03d5e5

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      if !File.exists?(command) && !command.include?('/')
30        command = File.dirname(__FILE__) + '/commands/' + command.to_s
31      end
32
33      add_markup(regexp) do |content|
34        rendered = execute(command, content)
35        block ? block.call(rendered) : rendered
36      end
37    end
38
39    def add_markup(regexp, &block)
40      @@markups[regexp] = block
41    end
42
43    def can_render?(filename)
44      !!renderer(filename)
45    end
46
47    def renderer(filename)
48      @@markups.each do |key, value|
49        if Regexp.compile("\\.(#{key})$") =~ filename
50          return value
51        end
52      end
53      nil
54    end
55
56    def execute(command, target)
57      out = ''
58      Open3.popen3(command) do |stdin, stdout, _|
59        stdin.puts target
60        stdin.close
61        out = stdout.read
62      end
63      out.gsub("\r", '')
64    end
65
66    # Define markups
67    instance_eval File.read(File.dirname(__FILE__) + '/markups.rb')
68  end
69end