8000 working scanner · trilogyinteractive/coderay@1b4bc58 · GitHub
[go: up one dir, main page]

Skip to content

Commit 1b4bc58

Browse files
committed
working scanner
1 parent 2dffd7e commit 1b4bc58

File tree

1 file changed

+49
-15
lines changed

1 file changed

+49
-15
lines changed

lib/coderay/scanners/liquid.rb

Lines changed: 49 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,18 @@ class Liquid < Scanner
2626
iflist|
2727
endiflist|
2828
else|
29+
)/
30+
31+
DIRECTIVE_OPERATORS = /(
2932
=|
3033
==|
3134
!=|
3235
>|
3336
<|
3437
<=|
35-
>=
38+
>=|
39+
contains|
40+
with
3641
)/
3742

3843
FILTER_KEYWORDS = /(
@@ -67,6 +72,8 @@ class Liquid < Scanner
6772
modulo
6873
)/
6974

75+
SELECTORS =
76+
7077
LIQUID_DIRECTIVE_BLOCK = /
7178
{%
7279
(.*?)
@@ -94,32 +101,60 @@ def scan_directive(encoder, options, match)
94101
#This should use the DIRECTIVE_KEYWORDS regex, not sure why it doesn't work
95102
if match = scan(/(wrap|endwrap)/)
96103
encoder.text_token match, :directive
97-
if match = scan(/(\w+)(:)('\D+')/)
98-
selector = match[0]
99-
delimiter = match[1]
100-
variable = match[2]
101-
102-
encoder.text_token selector, :operator
103-
encoder.text_token delimiter, :delimiter
104-
encoder.text_token variable, :variable
104+
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
110+
scan_spaces(encoder)
111+
end
112+
if variable = scan(/(\w+)|('\S+')|("\w+")/)
113+
encoder.text_token variable, :variable
114+
end
105115
end
106116
end
107117
scan_spaces(encoder)
108-
if match = scan(/(%}|}})/)
118+
if match = scan(/%}/)
109119
encoder.text_token match, :key
110120
state = :initial
111121
end
112122
end
113123

124+
def scan_output_filters(encoder, options, match)
125+
encoder.text_token match, :delimiter
126+
scan_spaces(encoder)
127+
#Replace with OUTPUT_KEYWORDS regex
128+
if directive = scan(/prepend|replace_first/)
129+
encoder.text_token directive, :directive
130+
end
131+
if delimiter = scan(/:/)
132+
encoder.text_token delimiter, :delimiter
133+
end
134+
scan_spaces(encoder)
135+
if variable = scan(/(\w+)|('\S+')|(".+")/)
136+
encoder.text_token variable, :variable
137+
end
138+
if next_filter = scan(/\s\|\s/)
139+
scan_output_filters(encoder, options, next_filter)
140+
end
141+
end
142+
114143
def scan_output(encoder, options, match)
115144
encoder.text_token match, :key
116145
state = :liquid
117146
scan_spaces(encoder)
118-
if match = scan(/\D+/)
147+
if match = scan(/(\w+)|('\S+')|("\w+")/)
119148
encoder.text_token match, :variable
120149
end
121-
if scan(/\|/)
150+
if match = scan(/(\s\|\s)/)
151+
scan_output_filters(encoder, options, match)
122152
end
153+
scan_spaces(encoder)
154+
if match = scan(/}}/)
155+
encoder.text_token match, :key
156+
end
157+
state = :initial
123158
end
124159

125160
def scan_tokens(encoder, options)
@@ -129,12 +164,11 @@ def scan_tokens(encoder, options)
129164
until eos?
130165
if (match = scan_until(/(?=({{|{%))/) || scan_rest) and not match.empty? and state != :liquid
131166
@html_scanner.tokenize(match, tokens: encoder)
132-
debug match, debug_cycle, state if debug_cycle == 1
133167
state = :initial
134168
scan_spaces(encoder)
135-
elsif match = scan(/({%|{{)/)
169+
elsif match = scan(/{%/)
136170
scan_directive(encoder, 562D options, match)
137-
elsif match = scan(/({{|{{)/)
171+
elsif match = scan(/{{/)
138172
scan_output(encoder, options, match)
139173
else
140174
raise "Else-case reached. #{debug_cycle.to_s} cycles run. State: #{state.to_s}."

0 commit comments

Comments
 (0)
0