Implement line numbers without the Liquid::Token class.

This commit is contained in:
Dylan Thacker-Smith
2015-07-08 19:21:59 -04:00
parent afda01adbb
commit cebf75b8d7
19 changed files with 186 additions and 194 deletions
+6 -12
View File
@@ -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