all repos — markup @ 91950b83bbc9990613bec5370f53ddddaad644f6

The code we use to render README.your_favorite_markup

lib/github/markup/command_implementation.rb (view raw)

 1begin
 2  require "open3_detach"
 3rescue LoadError
 4  require "open3"
 5end
 6
 7require "github/markup/implementation"
 8
 9module GitHub
10  module Markup
11    class CommandImplementation < Implementation
12      attr_reader :command, :block
13
14      def initialize(regexp, command, &block)
15        super regexp
16        @command = command.to_s
17        @block = block
18      end
19
20      def render(content)
21        rendered = execute(command, content)
22        rendered = rendered.to_s.empty? ? content : rendered
23        call_block(rendered, content)
24      end
25
26    private
27      def call_block(rendered, content)
28        if block && block.arity == 2
29          block.call(rendered, content)
30        elsif block
31          block.call(rendered)
32        else
33          rendered
34        end
35      end
36
37      def execute(command, target)
38        out = ''
39        Open3.popen3(command) do |stdin, stdout, _|
40          stdin.puts target
41          stdin.close
42          out = stdout.read
43        end
44        out.gsub("\r", '')
45      rescue Errno::EPIPE
46        ""
47      rescue Errno::ENOENT
48        ""
49      end
50    end
51  end
52end