Module: Jade

Defined in:
lib/jade-ruby/config.rb,
lib/jade-ruby/compile.rb

Defined Under Namespace

Classes: CompileError, Config, ExecutableError

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.configObject

Returns the value of attribute config.



47
48
49
# File 'lib/jade-ruby/config.rb', line 47

def config
  @config
end

Class Method Details

.check_executable!Object



48
49
50
51
52
53
54
55
56
# File 'lib/jade-ruby/compile.rb', line 48

def check_executable!
  unless @executable_checked
    `jade --version`
    unless $?.success?
      raise ExecutableError, 'No jade executable found in your system. Did you forget to "npm install -g jade"?'
    end
    @executable_checked = true
  end
end

.compile(source, options = {}) ⇒ Object

Raises:



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/jade-ruby/compile.rb', line 8

def compile(source, options = {})
  check_executable!

  source  = source.read if source.respond_to?(:read)
  options = config.to_hash.merge(options)

  # http://web.archive.org/web/*/http://jade-lang.com/command-line/
  cmd = ['jade']

  options[:compileDebug]  = options[:compile_debug]
  options[:nameAfterFile] = options[:name_after_file]

  options.respond_to?(:compact) ? options.compact : options.delete_if { |k, v| v.nil? }

  # Command line arguments take precedence over json options
  # https://github.com/jadejs/jade/blob/master/bin/jade.js
  cmd.push('--obj',             JSON.generate(options))

  cmd.push('--out',             escape(options[:out]))             if options[:out]
  cmd.push('--path',            escape(options[:filename]))        if options[:filename]
  cmd.push('--basedir',         escape(options[:basedir]))         if options[:basedir]
  cmd.push('--pretty')                                             if options[:pretty]
  cmd.push('--client')                                             if options[:client]
  cmd.push('--name',            escape(options[:name]))            if options[:name]
  cmd.push('--no-debug')                                           unless options[:compile_debug]
  cmd.push('--extension',       escape(options[:extension]))       if options[:extension]
  cmd.push('--hierarchy',       escape(options[:hierarchy]))       if options[:hierarchy]
  cmd.push('--name-after-file', escape(options[:name_after_file])) if options[:name_after_file]
  cmd.push('--doctype',         escape(options[:doctype]))         if options[:doctype]

  stdout, stderr, exit_status = Open3.capture3(*cmd, stdin_data: source)
  raise CompileError.new(stderr) unless exit_status.success?

  if options[:client]
    %{ (function(jade) { #{stdout}; return #{options[:name]}; }).call(this, jade); }
  else
    stdout
  end
end