Class: Liquid::BlockBody

Inherits:
Object
  • Object
show all
Defined in:
lib/liquid/block_body.rb,
lib/liquid/profiler/hooks.rb

Direct Known Subclasses

Document

Constant Summary collapse

FullToken =
/\A#{TagStart}\s*(\w+)\s*(.*)?#{TagEnd}\z/om
ContentOfVariable =
/\A#{VariableStart}(.*)#{VariableEnd}\z/om
TAGSTART =
"{%".freeze
VARSTART =
"{{".freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBlockBody

Returns a new instance of BlockBody.



10
11
12
13
# File 'lib/liquid/block_body.rb', line 10

def initialize
  @nodelist = []
  @blank = true
end

Instance Attribute Details

#nodelistObject (readonly)

Returns the value of attribute nodelist.



8
9
10
# File 'lib/liquid/block_body.rb', line 8

def nodelist
  @nodelist
end

Instance Method Details

#blank?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/liquid/block_body.rb', line 58

def blank?
  @blank
end

#parse(tokens, options) {|nil, nil| ... } ⇒ Object

Yields:

  • (nil, nil)


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
47
48
49
50
51
52
53
54
55
56
# File 'lib/liquid/block_body.rb', line 15

def parse(tokens, options)
  while token = tokens.shift
    begin
      unless token.empty?
        case
        when token.start_with?(TAGSTART)
          if token =~ FullToken
            tag_name = $1
            markup = $2
            # fetch the tag from registered blocks
            if tag = Template.tags[tag_name]
              markup = token.child(markup) if token.is_a?(Token)
              new_tag = tag.parse(tag_name, markup, tokens, options)
              new_tag.line_number = token.line_number if token.is_a?(Token)
              @blank &&= new_tag.blank?
              @nodelist << new_tag
            else
              # end parsing if we reach an unknown tag and let the caller decide
              # determine how to proceed
              return yield tag_name, markup
            end
          else
            raise_missing_tag_terminator(token, options)
          end
        when token.start_with?(VARSTART)
          new_var = create_variable(token, options)
          new_var.line_number = token.line_number if token.is_a?(Token)
          @nodelist << new_var
          @blank = false
        else
          @nodelist << token
          @blank &&= !!(token =~ /\A\s*\z/)
        end
      end
    rescue SyntaxError => e
      e.set_line_number_from_token(token)
      raise
    end
  end

  yield nil, nil
end

#render(context) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/liquid/block_body.rb', line 70

def render(context)
  output = []
  context.resource_limits.render_score += @nodelist.length

  @nodelist.each do |token|
    # Break out if we have any unhanded interrupts.
    break if context.has_interrupt?

    begin
      # If we get an Interrupt that means the block must stop processing. An
      # Interrupt is any command that stops block execution such as {% break %}
      # or {% continue %}
      if token.is_a?(Continue) or token.is_a?(Break)
        context.push_interrupt(token.interrupt)
        break
      end

      token_output = render_token(token, context)

      unless token.is_a?(Block) && token.blank?
        output << token_output
      end
    rescue MemoryError => e
      raise e
    rescue ::StandardError => e
      output << context.handle_error(e, token)
    end
  end

  output.join
end

#render_token_with_profiling(token, context) ⇒ Object Also known as: render_token



3
4
5
6
7
# File 'lib/liquid/profiler/hooks.rb', line 3

def render_token_with_profiling(token, context)
  Profiler.profile_token_render(token) do
    render_token_without_profiling(token, context)
  end
end

#warningsObject



62
63
64
65
66
67
68
# File 'lib/liquid/block_body.rb', line 62

def warnings
  all_warnings = []
  nodelist.each do |node|
    all_warnings.concat(node.warnings || []) if node.respond_to?(:warnings)
  end
  all_warnings
end