Skip to content

Commit 799227a

Browse files
committed
finish out scanner (still needs some fixes)
1 parent 1b4bc58 commit 799227a

File tree

3 files changed

+56
-96
lines changed

3 files changed

+56
-96
lines changed

lib/coderay/scanners/\

Lines changed: 0 additions & 1 deletion
This file was deleted.

lib/coderay/scanners/]

Lines changed: 0 additions & 67 deletions
This file was deleted.

lib/coderay/scanners/liquid.rb

Lines changed: 56 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ class Liquid < Scanner
55

66
register_for :liquid
77

8-
DIRECTIVE_KEYWORDS = /(
8+
DIRECTIVE_KEYWORDS = /
99
list|
1010
endlist|
1111
for|
@@ -26,9 +26,9 @@ class Liquid < Scanner
2626
iflist|
2727
endiflist|
2828
else|
29-
)/
29+
/x
3030

31-
DIRECTIVE_OPERATORS = /(
31+
DIRECTIVE_OPERATORS = /
3232
=|
3333
==|
3434
!=|
@@ -37,10 +37,19 @@ class Liquid < Scanner
3737
<=|
3838
>=|
3939
contains|
40-
with
41-
)/
40+
/x
4241

43-
FILTER_KEYWORDS = /(
42+
MATH = /
43+
=|
44+
==|
45+
!=|
46+
>|
47+
<|
48+
<=|
49+
>|
50+
/x
51+
52+
FILTER_KEYWORDS = /
4453
date|
4554
capitalize|
4655
downcase|
@@ -70,50 +79,67 @@ class Liquid < Scanner
7079
divided_by|
7180
split|
7281
modulo
73-
)/
74-
75-
SELECTORS =
82+
/x
7683

7784
LIQUID_DIRECTIVE_BLOCK = /
7885
{%
7986
(.*?)
8087
%}
81-
/
88+
/x
8289

8390
def setup
8491
@html_scanner = CodeRay.scanner(:html, tokens: @tokens, keep_tokens: true, keep_state: false)
8592
end
8693

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-
9194
def scan_spaces(encoder)
9295
if match = scan(/\s+/)
9396
encoder.text_token match, :space
9497
end
9598
end
9699

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+
97117
def scan_directive(encoder, options, match)
118+
Rails.logger.debug 'DEBUG: Scanning directive'
98119
encoder.text_token match, :key
99120
state = :liquid
100121
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/)
103124
encoder.text_token match, :directive
104125
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
110134
scan_spaces(encoder)
111135
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)
114139
end
115140
end
116141
end
142+
scan_selector(encoder, options, match)
117143
scan_spaces(encoder)
118144
if match = scan(/%}/)
119145
encoder.text_token match, :key
@@ -124,8 +150,10 @@ def scan_directive(encoder, options, match)
124150
def scan_output_filters(encoder, options, match)
125151
encoder.text_token match, :delimiter
126152
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}/)
129157
encoder.text_token directive, :directive
130158
end
131159
if delimiter = scan(/:/)
@@ -141,6 +169,7 @@ def scan_output_filters(encoder, options, match)
141169
end
142170

143171
def scan_output(encoder, options, match)
172+
Rails.logger.debug 'DEBUG: Scanning output'
144173
encoder.text_token match, :key
145174
state = :liquid
146175
scan_spaces(encoder)
@@ -158,8 +187,8 @@ def scan_output(encoder, options, match)
158187
end
159188

160189
def scan_tokens(encoder, options)
190+
Rails.logger.debug "DEBUG: Scan started: #{self.string}"
161191
state = :initial
162-
debug_cycle = 0
163192

164193
until eos?
165194
if (match = scan_until(/(?=({{|{%))/) || scan_rest) and not match.empty? and state != :liquid
@@ -171,9 +200,8 @@ def scan_tokens(encoder, options)
171200
elsif match = scan(/{{/)
172201
scan_output(encoder, options, match)
173202
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}."
175204
end
176-
debug_cycle += 1
177205
end
178206
encoder
179207
end

0 commit comments

Comments
 (0)