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)
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 execute(command, content)
31 end
32 end
33
34 def add_markup(regexp, &block)
35 @@markups[regexp] = block
36 end
37
38 def renderer(filename)
39 @@markups.each do |key, value|
40 if Regexp.compile("#{key}$") =~ filename
41 return value
42 end
43 end
44 end
45
46 def execute(command, target)
47 out = ''
48 Open3.popen3(command) do |stdin, stdout, _|
49 stdin.puts target
50 stdin.close
51 while tmp = stdout.read(1024)
52 out << tmp
53 end
54 end
55 out
56 end
57
58 # Define markups
59 instance_eval File.read(File.dirname(__FILE__) + '/markups.rb')
60 end
61end