Skip to content

Commit 2e4e83b

Browse files
committed
tweak Sass scanner: comments, includes, functions
1 parent b159057 commit 2e4e83b

File tree

1 file changed

+30
-10
lines changed

1 file changed

+30
-10
lines changed

lib/coderay/scanners/sass.rb

+30-10
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ class Sass < CSS
77
register_for :sass
88
file_extension 'sass'
99

10-
SASS_FUNCTION = /(?:inline-image|linear-gradient|color-stops|mix|lighten|darken|rotate|image-url|image-width|image-height|sprite-url|sprite-path|sprite-file|sprite-map|sprite-position|sprite|unquote|join|round|ceil|floor|nth)/
11-
1210
STRING_CONTENT_PATTERN = {
1311
"'" => /(?:[^\n\'\#]+|\\\n|#{RE::Escape}|#(?!\{))+/,
1412
'"' => /(?:[^\n\"\#]+|\\\n|#{RE::Escape}|#(?!\{))+/,
@@ -26,10 +24,22 @@ def scan_tokens encoder, options
2624

2725
until eos?
2826

29-
if match = scan(/\s+/)
27+
if bol? && (match = scan(/(?>( +)?(\/[\*\/])(.+)?)(?=\n)/))
28+
encoder.text_token self[1], :space if self[1]
29+
encoder.begin_group :comment
30+
encoder.text_token self[2], :delimiter
31+
encoder.text_token self[3], :content if self[3]
32+
if match = scan(/(?:\n+#{self[1]} .*)+/)
33+
encoder.text_token match, :content
34+
end
35+
encoder.end_group :comment
36+
elsif match = scan(/\n|[^\n\S]+\n?/)
3037
encoder.text_token match, :space
31-
value_expected = false if match.index(/\n/)
32-
38+
if match.index(/\n/)
39+
value_expected = false
40+
states.pop if states.last == :include
41+
end
42+
3343
elsif states.last == :sass_inline && (match = scan(/\}/))
3444
encoder.text_token match, :inline_delimiter
3545
encoder.end_group :inline
@@ -61,7 +71,11 @@ def scan_tokens encoder, options
6171
elsif match = scan(/(\=|@mixin +)#{RE::Ident}/o)
6272
encoder.text_token match, :function
6373
next
64-
elsif match = scan(/@media/)
74+
elsif match = scan(/@import\b/)
75+
encoder.text_token match, :directive
76+
states << :include
77+
next
78+
elsif match = scan(/@media\b/)
6579
encoder.text_token match, :directive
6680
# states.push :media_before_name
6781
next
@@ -90,13 +104,19 @@ def scan_tokens encoder, options
90104
encoder.text_token match, :inline_delimiter
91105
states.push :sass_inline
92106
elsif match = scan(/ \\ | $ /x)
93-
encoder.end_group state
107+
encoder.end_group :string
94108
encoder.text_token match, :error unless match.empty?
95109
states.pop
96110
else
97111
raise_inspect "else case #{string_delimiter} reached; %p not handled." % peek(1), encoder
98112
end
99113

114+
when :include
115+
if match = scan(/[^\s'",]+/)
116+
encoder.text_token match, :include
117+
next
118+
end
119+
100120
else
101121
#:nocov:
102122
raise_inspect 'Unknown state', encoder
@@ -148,9 +168,6 @@ def scan_tokens encoder, options
148168
states.push :string
149169
end
150170

151-
elsif match = scan(/#{SASS_FUNCTION}/o)
152-
encoder.text_token match, :predefined
153-
154171
elsif match = scan(/#{RE::Function}/o)
155172
encoder.begin_group :function
156173
start = match[/^[-\w]+\(/]
@@ -163,6 +180,9 @@ def scan_tokens encoder, options
163180
end
164181
encoder.end_group :function
165182

183+
elsif match = scan(/[a-z][-a-z_]*(?=\()/o)
184+
encoder.text_token match, :predefined
185+
166186
elsif match = scan(/(?: #{RE::Dimension} | #{RE::Percentage} | #{RE::Num} )/ox)
167187
encoder.text_token match, :float
168188

0 commit comments

Comments
 (0)