summaryrefslogtreecommitdiff
path: root/vendor/bundle/ruby/3.4.0/gems/liquid-4.0.4/lib/liquid/tokenizer.rb
blob: d03657e8436c4159714f8ba677343755fc95b8f9 (plain)
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
module Liquid
  class Tokenizer
    attr_reader :line_number

    def initialize(source, line_numbers = false)
      @source = source
      @line_number = line_numbers ? 1 : nil
      @tokens = tokenize
    end

    def shift
      token = @tokens.shift
      @line_number += token.count("\n") if @line_number && token
      token
    end

    private

    def tokenize
      @source = @source.source if @source.respond_to?(:source)
      return [] if @source.to_s.empty?

      tokens = @source.split(TemplateParser)

      # removes the rogue empty element at the beginning of the array
      tokens.shift if tokens[0] && tokens[0].empty?

      tokens
    end
  end
end