all repos — markup @ 8a8b52e3e33942a230102c7576a441f03721710f

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      renderer(filename)[content] || content
14    end
15
16    def markup(file, pattern, &block)
17      require file.to_s
18      add_markup(pattern, &block)
19    rescue LoadError
20      nil
21    end
22
23    def command(command, regexp, &block)
24      command = command.to_s
25      if !File.exists?(command) && !command.include?('/')
26        command = File.dirname(__FILE__) + '/commands/' + command.to_s
27      end
28
29      add_markup(regexp) do |content|
30        rendered = execute(command, content)
31        block ? block.call(rendered) : rendered
32      end
33    end
34
35    def add_markup(regexp, &block)
36      @@markups[regexp] = block
37    end
38
39    def renderer(filename)
40      @@markups.each do |key, value|
41        if Regexp.compile("#{key}$") =~ filename
42          return value
43        end
44      end
45    end
46
47    def execute(command, target)
48      out = ''
49      Open3.popen3(command) do |stdin, stdout, _|
50        stdin.puts target
51        stdin.close
52        while tmp = stdout.read(1024)
53          out << tmp
54        end
55      end
56      out
57    end
58
59    # Define markups
60    instance_eval File.read(File.dirname(__FILE__) + '/markups.rb')
61  end
62end