mirror of
https://github.com/Shopify/liquid.git
synced 2026-09-19 02:40:41 -07:00
Implement line numbers without the Liquid::Token class.
This commit is contained in:
+6
-12
@@ -1,13 +1,17 @@
|
||||
module Liquid
|
||||
class Tokenizer
|
||||
attr_reader :line_number
|
||||
|
||||
def initialize(source, line_numbers = false)
|
||||
@source = source
|
||||
@line_numbers = line_numbers
|
||||
@line_number = 1 if line_numbers
|
||||
@tokens = tokenize
|
||||
end
|
||||
|
||||
def shift
|
||||
@tokens.shift
|
||||
token = @tokens.shift
|
||||
@line_number += token.count("\n") if @line_number && token
|
||||
token
|
||||
end
|
||||
|
||||
private
|
||||
@@ -17,21 +21,11 @@ module Liquid
|
||||
return [] if @source.to_s.empty?
|
||||
|
||||
tokens = @source.split(TemplateParser)
|
||||
tokens = @line_numbers ? calculate_line_numbers(tokens) : tokens
|
||||
|
||||
# removes the rogue empty element at the beginning of the array
|
||||
tokens.shift if tokens[0] && tokens[0].empty?
|
||||
|
||||
tokens
|
||||
end
|
||||
|
||||
def calculate_line_numbers(tokens)
|
||||
current_line = 1
|
||||
tokens.map do |token|
|
||||
Token.new(token, current_line).tap do
|
||||
current_line += token.count("\n")
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user