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}#{WhitespaceControl}?\s*(\w+)\s*(.*?)#{WhitespaceControl}?#{TagEnd}\z/om
ContentOfVariable =
/\A#{VariableStart}#{WhitespaceControl}?(.*?)#{WhitespaceControl}?#{VariableEnd}\z/om
WhitespaceOrNothing =
/\A\s*\z/
TAGSTART =
"{%".freeze
VARSTART =
"{{".freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBlockBody

Returns a new instance of BlockBody.



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

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

Instance Attribute Details

#nodelistObject (readonly)

Returns the value of attribute nodelist.



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

def nodelist
  @nodelist
end

Instance Method Details

#blank?Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/liquid/block_body.rb', line 65

def blank?
  @blank
end

#parse(tokenizer, parse_context) {|nil, nil| ... } ⇒ Object

Yields:

  • (nil, nil)


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
# File 'lib/liquid/block_body.rb', line 16

def parse(tokenizer, parse_context)
  parse_context.line_number = tokenizer.line_number
  while token = tokenizer.shift
    next if token.empty?
    case
    when token.start_with?(TAGSTART)
      whitespace_handler(token, parse_context)
      unless token =~ FullToken
        raise_missing_tag_terminator(token, parse_context)
      end
      tag_name = $1
      markup = $2
      # fetch the tag from registered blocks
      unless tag = registered_tags[tag_name]
        # end parsing if we reach an unknown tag and let the caller decide
        # determine how to proceed
        return yield tag_name, markup
      end
      new_tag = tag.parse(tag_name, markup, tokenizer, parse_context)
      @blank &&= new_tag.blank?
      @nodelist << new_tag
    when token.start_with?(VARSTART)
      whitespace_handler(token, parse_context)
      @nodelist << create_variable(token, parse_context)
      @blank = false
    else
      if parse_context.trim_whitespace
        token.lstrip!
      end
      parse_context.trim_whitespace = false
      @nodelist << token
      @blank &&= !!(token =~ WhitespaceOrNothing)
    end
    parse_context.line_number = tokenizer.line_number
  end

  yield nil, nil
end

#render(context) ⇒ Object



69
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
# File 'lib/liquid/block_body.rb', line 69

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

  idx = 0
  while node = @nodelist[idx]
    case node
    when String
      check_resources(context, node)
      output << node
    when Variable
      render_node_to_output(node, output, context)
    when Block
      render_node_to_output(node, output, context, node.blank?)
      break if context.interrupt? # might have happened in a for-block
    when Continue, Break
      # 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 %}
      context.push_interrupt(node.interrupt)
      break
    else # Other non-Block tags
      render_node_to_output(node, output, context)
      break if context.interrupt? # might have happened through an include
    end
    idx += 1
  end

  output.join
end

#render_node_with_profiling(node, output, context, skip_output = false) ⇒ Object Also known as: render_node_to_output



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

def render_node_with_profiling(node, output, context, skip_output = false)
  Profiler.profile_node_render(node) do
    render_node_without_profiling(node, output, context, skip_output)
  end
end

#whitespace_handler(token, parse_context) ⇒ Object



55
56
57
58
59
60
61
62
63
# File 'lib/liquid/block_body.rb', line 55

def whitespace_handler(token, parse_context)
  if token[2] == WhitespaceControl
    previous_token = @nodelist.last
    if previous_token.is_a? String
      previous_token.rstrip!
    end
  end
  parse_context.trim_whitespace = (token[-3] == WhitespaceControl)
end