class Plaintext::ExternalCommandHandler

Constants

DEFAULT_STREAM_ENCODING
FILE_PLACEHOLDER

Public Class Methods

available?() click to toggle source
# File lib/plaintext/file_handler/external_command_handler.rb, line 42
def self.available?
  new.available?
end

Public Instance Methods

accept?(content_type) click to toggle source
Calls superclass method Plaintext::FileHandler#accept?
# File lib/plaintext/file_handler/external_command_handler.rb, line 34
def accept?(content_type)
  super and available?
end
available?() click to toggle source
# File lib/plaintext/file_handler/external_command_handler.rb, line 38
def available?
  @command.present? and File.executable?(@command[0])
end
shellout(cmd, options = {}, &block) click to toggle source
# File lib/plaintext/file_handler/external_command_handler.rb, line 18
def shellout(cmd, options = {}, &block)
  mode = "r+"
  IO.popen(cmd, mode) do |io|
    set_stream_encoding(io)
    io.close_write unless options[:write_stdin]
    block.call(io) if block_given?
  end
end
text(file, options = {}) click to toggle source
# File lib/plaintext/file_handler/external_command_handler.rb, line 27
def text(file, options = {})
  cmd = @command.dup
  cmd[cmd.index(FILE_PLACEHOLDER)] = Pathname(file).to_s
  shellout(cmd) { |io| read io, options[:max_size] }.to_s
end

Protected Instance Methods

utf8_stream?() click to toggle source
# File lib/plaintext/file_handler/external_command_handler.rb, line 48
def utf8_stream?
  false
end

Private Instance Methods

read(io, max_size = nil) click to toggle source
# File lib/plaintext/file_handler/external_command_handler.rb, line 64
def read(io, max_size = nil)
  piece = io.read(max_size)

  if utf8_stream?
    piece
  else
    Plaintext::CodesetUtil.to_utf8 piece, DEFAULT_STREAM_ENCODING
  end
end
set_stream_encoding(io) click to toggle source
# File lib/plaintext/file_handler/external_command_handler.rb, line 54
def set_stream_encoding(io)
  return unless io.respond_to?(:set_encoding)

  if utf8_stream?
    io.set_encoding('UTF-8'.freeze)
  else
    io.set_encoding(DEFAULT_STREAM_ENCODING)
  end
end