Class: SprocketsInline::Processor

Inherits:
Tilt::Template
  • Object
show all
Defined in:
lib/sprockets_inline/processor.rb

Constant Summary collapse

JS_DIRECTIVE_PATTERN =

regexp for detecting a JS statement Examples: //= inline "bob" // =inline "bob" // = inline "bob" and all sorts of other wierd combos

/
  ^\s* \/\/ \s* = \s* (\w+.*?) $
/x
INDENT =

regexp for extracting the indent

/^(\s*)/

Instance Method Summary collapse

Instance Method Details

#evaluate(context, locals, &block) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/sprockets_inline/processor.rb', line 21

def evaluate(context, locals, &block)
  @context = context
  @result  = ""

  data.lines.each do |line|
    if directive = get_directive(line)
      process_directive(directive)
    else
      @result << line
    end
  end

  @result
end

#get_directive(line) ⇒ Object



36
37
38
39
40
41
42
43
44
45
# File 'lib/sprockets_inline/processor.rb', line 36

def get_directive(line)
  if directive = line[JS_DIRECTIVE_PATTERN, 1]
    name, *args = Shellwords.shellwords(directive)
    if respond_to?("process_#{name}_directive", true)
      return { :name => name, :args => args, :line => line }
    end
  end

  return nil
end

#prepare ⇒ Object



18
19
# File 'lib/sprockets_inline/processor.rb', line 18

def prepare
end

#process_directive(directive) ⇒ Object



47
48
49
# File 'lib/sprockets_inline/processor.rb', line 47

def process_directive(directive)
  send("process_#{directive[:name]}_directive", directive)
end

#process_inline_directive(directive) ⇒ Object



51
52
53
54
55
56
57
# File 'lib/sprockets_inline/processor.rb', line 51

def process_inline_directive(directive)
  content = @context.evaluate(directive[:args][0])

  # preserve indenting of the directive
  indent = directive[:line][INDENT, 1]
  @result << content.lines.map { |c| indent + c }.join
end