Sha256: 8588ca495be3f92c89233496b699951d313ea2207ff9b5fe3ea6c08fc449539d

Contents?: true

Size: 1.02 KB

Versions: 5

Compression:

Stored size: 1.02 KB

Contents

require "posix-spawn"
require "github/markup/implementation"

module GitHub
  module Markup
    class CommandError < RuntimeError
    end

    class CommandImplementation < Implementation
      attr_reader :command, :block

      def initialize(regexp, command, &block)
        super regexp
        @command = command.to_s
        @block = block
      end

      def render(content)
        rendered = execute(command, content)
        rendered = rendered.to_s.empty? ? content : rendered
        call_block(rendered, content)
      end

    private
      def call_block(rendered, content)
        if block && block.arity == 2
          block.call(rendered, content)
        elsif block
          block.call(rendered)
        else
          rendered
        end
      end

      def execute(command, target)
        spawn = POSIX::Spawn::Child.new(*command, :input => target)
        if spawn.status.success?
          spawn.out.gsub("\r", '')
        else
          raise CommandError.new(spawn.err.strip)
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
github-markup-1.2.1 lib/github/markup/command_implementation.rb
github-markup-1.2.0 lib/github/markup/command_implementation.rb
github-markup-1.1.2 lib/github/markup/command_implementation.rb
github-markup-1.1.1 lib/github/markup/command_implementation.rb
github-markup-1.1.0 lib/github/markup/command_implementation.rb