Class: Slim::Compiler Private

Inherits:
Filter
  • Object
show all
Defined in:
lib/slim/compiler.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Compiles Slim expressions into Temple::HTML expressions.

Instance Method Summary collapse

Methods inherited from Filter

#tmp_var

Instance Method Details

#on_slim_attrs(*attrs) ⇒ Array

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Handle tag attributes expression ‘[:slim, :attrs, *attrs]`

Parameters:

  • attrs (Array)

    Attributes

Returns:

  • (Array)

    Compiled temple expression



83
84
85
# File 'lib/slim/compiler.rb', line 83

def on_slim_attrs(*attrs)
  [:html, :staticattrs, *attrs.map {|k, v| [k.to_s, compile!(v)] }]
end

#on_slim_control(code, content) ⇒ Array

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Handle control expression ‘[:slim, :control, code, content]`

Parameters:

  • ruby (String)

    code

  • content (Array)

    Temple expression

Returns:

  • (Array)

    Compiled temple expression



10
11
12
13
14
# File 'lib/slim/compiler.rb', line 10

def on_slim_control(code, content)
  [:multi,
    [:block, code],
    compile!(content)]
end

#on_slim_directive(type) ⇒ Array

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Handle directive expression ‘[:slim, :directive, type]`

Parameters:

  • type (String)

    Directive type

Returns:

  • (Array)

    Compiled temple expression



63
64
65
66
67
# File 'lib/slim/compiler.rb', line 63

def on_slim_directive(type)
  if type =~ /^doctype/
    [:html, :doctype, $'.strip]
  end
end

#on_slim_output(escape, code, content) ⇒ Array

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Handle output expression ‘[:slim, :output, escape, code, content]`

Parameters:

  • escape (Boolean)

    Escape html

  • code (String)

    Ruby code

  • content (Array)

    Temple expression

Returns:

  • (Array)

    Compiled temple expression



22
23
24
25
26
27
28
# File 'lib/slim/compiler.rb', line 22

def on_slim_output(escape, code, content)
  if empty_exp?(content)
    [:multi, escape ? [:escape, :dynamic, code] : [:dynamic, code], content]
  else
    on_slim_output_block(escape, code, content)
  end
end

#on_slim_output_block(escape, code, content) ⇒ Array

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Handle output expression ‘[:slim, :output, escape, code, content]` if content is not empty.

Parameters:

  • escape (Boolean)

    Escape html

  • code (String)

    Ruby code

  • content (Array)

    Temple expression

Returns:

  • (Array)

    Compiled temple expression



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/slim/compiler.rb', line 37

def on_slim_output_block(escape, code, content)
  tmp1, tmp2 = tmp_var('capture'), tmp_var('capture')

  [:multi,
    # Capture the result of the code in a variable. We can't do
    # `[:dynamic, code]` because it's probably not a complete
    # expression (which is a requirement for Temple).
    [:block, "#{tmp1} = #{code}"],

    # Capture the content of a block in a separate buffer. This means
    # that `yield` will not output the content to the current buffer,
    # but rather return the output.
    [:capture, tmp2,
     compile!(content)],

    # Close the block.
    [:block, 'end'],

    # Output the content.
    on_slim_output(escape, tmp1, [:multi])]
end

#on_slim_tag(name, attrs, closed, content) ⇒ Array

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Handle tag expression ‘[:slim, :tag, name, attrs, closed, content]`

Parameters:

  • name (String)

    Tag name

  • attrs (Array)

    Attributes

  • content (Array)

    Temple expression

Returns:

  • (Array)

    Compiled temple expression



75
76
77
# File 'lib/slim/compiler.rb', line 75

def on_slim_tag(name, attrs, closed, content)
  [:html, :tag, name, compile!(attrs), closed, compile!(content)]
end