10000 first pass at liquid scanner · trilogyinteractive/coderay@22d6b90 · GitHub
[go: up one dir, main page]

Skip to content

Commit 22d6b90

Browse files
committed
first pass at liquid scanner
1 parent cab119c commit 22d6b90

File tree

3 files changed

+76
-0
lines changed

3 files changed

+76
-0
lines changed

lib/coderay/helpers/file_type.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ def shebang filename
9696
'java' => :java,
9797
'js' => :java_script,
9898
'json' => :json,
99+
'amps' => :amps,
99100
'mab' => :ruby,
100101
'pas' => :delphi,
101102
'patch' => :diff,

lib/coderay/scanners/amps.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
module CodeRay
2+
module Scanners
3+
4+
load :html
5+
6+
class Amps < Scanner
7+
8+
register_for :amps
9+
title 'Amps Template'
10+
11+
KINDS_NOT_LOC = HTML::KINDS_NOT_LOC
12+
13+
AMPS_BLOCK = /
14+
({[{|%])
15+
(.*?)
16+
([%|}]})
17+
/xm
18+
19+
AMPS_COMMENT_BLOCK = /
20+
{%\s#
21+
(.*?)
22+

lib/coderay/scanners/liquid.rb

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
module CodeRay
2+
module Scanners
3+
4+
load :html
5+
6+
class Liquid < Scanner
7+
8+
register_for :liquid
9+
title 'Liquid Template'
10+
11+
KINDS_NOT_LOC = HTML::KINDS_NOT_LOC
12+
13+
LIQUID_BLOCK = /
14+
({[{|%])
15+
(.*?)
16+
([%|}]})
17+
/
18+
19+
START_OF_LIQUID = /{{|{%/
20+
21+
protected
22+
23+
def setup
24+
@html_scanner = CodeRay.scanner :html, tokens: @tokens, keep_tokens: true, keep_state: true
25+
@liquid_attribute_scanner = CodeRay.scanner :html, tokens: @tokens, keep_tokens: true, keep_stat: true
26+
end
27+
28+
def scan_tokens
29+
until eos?
30+
if (match = scan_until(/(?=#{START_OF_LIQUID})/o) || scan_reset) and not match.empty?
31+
@html_scanner.tokenize match, tokens: encoder
32+
elsif match = scan(/#{LIQUID_BLOCK}/o)
33+
start_tag = self[1]
34+
code = self[2]
35+
end_tag = self[3]
36+
37+
encoder.begin_group :inline
38+
encoder.text_token start_tag, :inline_delimiter
39+
40+
unless code.empty?
41+
@liquid_attribute_scanner.tokenize code, tokens: encoder, state: :attribute
42+
end
43+
44+
encoder.text_token end_tag, :inline_delimiter unless end_tag.empty?
45+
encoder.end_group :inline
46+
else
47+
raise_inspect 'else-case reached!', encoder
48+
end
49+
end
50+
end
51+
end
52+
end
53+
end

0 commit comments

Comments
 (0)
0