@@ -5,7 +5,7 @@ class Liquid < Scanner
5
5
6
6
register_for :liquid
7
7
8
- DIRECTIVE_KEYWORDS = /(
8
+ DIRECTIVE_KEYWORDS = /
9
9
list|
10
10
endlist|
11
11
for|
@@ -26,9 +26,9 @@ class Liquid < Scanner
26
26
iflist|
27
27
endiflist|
28
28
else|
29
- )/
29
+ /x
30
30
31
- DIRECTIVE_OPERATORS = /(
31
+ DIRECTIVE_OPERATORS = /
32
32
=|
33
33
==|
34
34
!=|
@@ -37,10 +37,19 @@ class Liquid < Scanner
37
37
<=|
38
38
>=|
39
39
contains|
40
- with
41
- )/
40
+ /x
42
41
43
- FILTER_KEYWORDS = /(
42
+ MATH = /
43
+ =|
44
+ ==|
45
+ !=|
46
+ >|
47
+ <|
48
+ <=|
49
+ >|
50
+ /x
51
+
52
+ FILTER_KEYWORDS = /
44
53
date|
45
54
capitalize|
46
55
downcase|
@@ -70,50 +79,67 @@ class Liquid < Scanner
70
79
divided_by|
71
80
split|
72
81
modulo
73
- )/
74
-
75
- SELECTORS =
82
+ /x
76
83
77
84
LIQUID_DIRECTIVE_BLOCK = /
78
85
{%
79
86
(.*?)
80
87
%}
81
- /
88
+ /x
82
89
83
90
def setup
84
91
@html_scanner = CodeRay . scanner ( :html , tokens : @tokens , keep_tokens : true , keep_state : false )
85
92
end
86
93
87
- def debug ( match , debug_cycle , state )
88
- raise "Match: #{ match } , left to scan: '#{ post_match } ', cycle: #{ debug_cycle . to_s } , state: #{ state . to_s } ."
89
- end
90
-
91
94
def scan_spaces ( encoder )
92
95
if match = scan ( /\s +/ )
93
96
encoder . text_token match , :space
94
97
end
95
98
end
96
99
100
+ def scan_selector ( encoder , options , match )
101
+ scan_spaces ( encoder )
102
+ if match = scan ( /in|with/ )
103
+ Rails . logger . debug 'DEBUG: Scanning selector'
104
+ scan_spaces ( encoder )
105
+ encoder . text_token match , :type
106
+ if delimiter = scan ( /:/ )
107
+ encoder . text_token delimiter , :delimiter
108
+ scan_spaces ( encoder )
109
+ end
110
+ if variable = scan ( /(\w +)|('\S +')|("\w +")/ )
111
+ encoder . text_token variable , :variable
112
+ end
113
+ scan_selector ( encoder , options , match )
114
+ end
115
+ end
116
+
97
117
def scan_directive ( encoder , options , match )
118
+ Rails . logger . debug 'DEBUG: Scanning directive'
98
119
encoder . text_token match , :key
99
120
state = :liquid
100
121
scan_spaces ( encoder )
101
- #This should use the DIRECTIVE_KEYWORDS regex, not sure why it doesn't work
102
- if match = scan ( /( wrap|endwrap) / )
122
+ #Replace with DIRECTIVES_KEYWORDS regex
123
+ if match = scan ( /wrap|if|endif| endwrap/ )
103
124
encoder . text_token match , :directive
104
125
scan_spaces ( encoder )
105
- #Replace with DIRECTIVE_OPERATORS
106
- if match = scan ( /with/ )
107
- encoder . text_token match , :operator
108
- if delimiter = scan ( /:/ )
109
- encoder . text_token delimiter , :delimiter
126
+ if match =~ /if/
127
+ if match = scan ( /\w +\. ?\w */ )
128
+ encoder . text_token match , :variable
129
+ end
130
+ scan_spaces ( encoder )
131
+ #Replace with MATH regex
132
+ if match = scan ( /!=/ )
133
+ encoder . text_token match , :char
110
134
scan_spaces ( encoder )
111
135
end
112
- if variable = scan ( /(\w +)|('\S +')|("\w +")/ )
113
- encoder . text_token variable , :variable
136
+ if match = scan ( /(\w +)|('\S +')|(".+")/ )
137
+ encoder . text_token match , :variable
138
+ scan_spaces ( encoder )
114
139
end
115
140
end
116
141
end
142
+ scan_selector ( encoder , options , match )
117
143
scan_spaces ( encoder )
118
144
if match = scan ( /%}/ )
119
145
encoder . text_token match , :key
@@ -124,8 +150,10 @@ def scan_directive(encoder, options, match)
124
150
def scan_output_filters ( encoder , options , match )
125
151
encoder . text_token match , :delimiter
126
152
scan_spaces ( encoder )
127
- #Replace with OUTPUT_KEYWORDS regex
128
- if directive = scan ( /prepend|replace_first/ )
153
+ #Replace with FILTER_KEYWORDS regex
154
+ testx = /replace_first|prepend/
155
+ if directive = scan ( /#{ testx } / )
156
+ #if directive = scan(/#{FILTER_KEYWORDS}/)
129
157
encoder . text_token directive , :directive
130
158
end
131
159
if delimiter = scan ( /:/ )
@@ -141,6 +169,7 @@ def scan_output_filters(encoder, options, match)
141
169
end
142
170
143
171
def scan_output ( encoder , options , match )
172
+ Rails . logger . debug 'DEBUG: Scanning output'
144
173
encoder . text_token match , :key
145
174
state = :liquid
146
175
scan_spaces ( encoder )
@@ -158,8 +187,8 @@ def scan_output(encoder, options, match)
158
187
end
159
188
160
189
def scan_tokens ( encoder , options )
190
+ Rails . logger . debug "DEBUG: Scan started: #{ self . string } "
161
191
state = :initial
162
- debug_cycle = 0
163
192
164
193
until eos?
165
194
if ( match = scan_until ( /(?=({{|{%))/ ) || scan_rest ) and not match . empty? and state != :liquid
@@ -171,9 +200,8 @@ def scan_tokens(encoder, options)
171
200
elsif match = scan ( /{{/ )
172
201
scan_output ( encoder , options , match )
173
202
else
174
- raise "Else-case reached. #{ debug_cycle . to_s } cycles run. State: #{ state . to_s } ."
203
+ raise "Else-case reached. State: #{ state . to_s } ."
175
204
end
176
- debug_cycle += 1
177
205
end
178
206
encoder
179
207
end
0 commit comments