Class: Nanoc::External::Filter

Inherits:
Filter
  • Object
show all
Defined in:
lib/nanoc/external/filter.rb

Overview

Pipes content through an external process.

For this filter to work, the external program must be able to receive input from standard input and it must write its output to standard output.

Instance Method Summary collapse

Instance Method Details

#run(content, params = {}) ⇒ String

Executes this filter. Parameters passed to this filter through ‘:options` will be passed to the specified command.

Examples:

filter :external, exec: 'multimarkdown', options: %w( --smart )
filter :external, exec: '/usr/local/bin/htmlcompressor

Parameters:

  • content (String)

    The content to filter.

  • params (Hash) (defaults to: {})

    a customizable set of options

Options Hash (params):

  • :options (Symbol) — default: []

    A list of options for the external command.

  • :exec (Symbol) — default: ""

    The name or the full path of the executable.

  • :debug (Symbol) — default: false

    Set to true to enable debugging.

Returns:

  • (String)

    The filtered content



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/nanoc/external/filter.rb', line 29

def run(content, params = {})
  debug = params.fetch(:debug, false)
  cmd   = params.fetch(:exec, nil)
  opts  = params.fetch(:options, [])

  if cmd.nil?
    raise Nanoc::Errors::GenericTrivial.new("nanoc-external: missing :exec argument")
  end

  cmd_with_opts = [ cmd ] + opts
  odebug(cmd_with_opts.join(' ')) if debug
  out = StringIO.new
  piper = Nanoc::Extra::Piper.new(:stdout => out, :stderr => $stderr)
  piper.run(cmd_with_opts, content)
  odebug(out.string) if debug
  out.string
end