File: document.rb

package info (click to toggle)
ruby-liquid 5.4.0-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,176 kB
  • sloc: ruby: 10,561; makefile: 6
file content (65 lines) | stat: -rw-r--r-- 1,448 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# frozen_string_literal: true

module Liquid
  class Document
    def self.parse(tokens, parse_context)
      doc = new(parse_context)
      doc.parse(tokens, parse_context)
      doc
    end

    attr_reader :parse_context, :body

    def initialize(parse_context)
      @parse_context = parse_context
      @body = new_body
    end

    def nodelist
      @body.nodelist
    end

    def parse(tokenizer, parse_context)
      while parse_body(tokenizer)
      end
      @body.freeze
    rescue SyntaxError => e
      e.line_number ||= parse_context.line_number
      raise
    end

    def unknown_tag(tag, _markup, _tokenizer)
      case tag
      when 'else', 'end'
        raise SyntaxError, parse_context.locale.t("errors.syntax.unexpected_outer_tag", tag: tag)
      else
        raise SyntaxError, parse_context.locale.t("errors.syntax.unknown_tag", tag: tag)
      end
    end

    def render_to_output_buffer(context, output)
      @body.render_to_output_buffer(context, output)
    end

    def render(context)
      render_to_output_buffer(context, +'')
    end

    private

    def new_body
      parse_context.new_block_body
    end

    def parse_body(tokenizer)
      @body.parse(tokenizer, parse_context) do |unknown_tag_name, unknown_tag_markup|
        if unknown_tag_name
          unknown_tag(unknown_tag_name, unknown_tag_markup, tokenizer)
          true
        else
          false
        end
      end
    end
  end
end