|
1 # -*- coding: utf-8 -*- |
|
2 """ |
|
3 pygments.lexers.css |
|
4 ~~~~~~~~~~~~~~~~~~~ |
|
5 |
|
6 Lexers for CSS and related stylesheet formats. |
|
7 |
|
8 :copyright: Copyright 2006-2014 by the Pygments team, see AUTHORS. |
|
9 :license: BSD, see LICENSE for details. |
|
10 """ |
|
11 |
|
12 import re |
|
13 import copy |
|
14 |
|
15 from pygments.lexer import ExtendedRegexLexer, RegexLexer, include, bygroups, \ |
|
16 default, words |
|
17 from pygments.token import Text, Comment, Operator, Keyword, Name, String, \ |
|
18 Number, Punctuation |
|
19 from pygments.util import iteritems |
|
20 |
|
21 __all__ = ['CssLexer', 'SassLexer', 'ScssLexer'] |
|
22 |
|
23 |
|
24 class CssLexer(RegexLexer): |
|
25 """ |
|
26 For CSS (Cascading Style Sheets). |
|
27 """ |
|
28 |
|
29 name = 'CSS' |
|
30 aliases = ['css'] |
|
31 filenames = ['*.css'] |
|
32 mimetypes = ['text/css'] |
|
33 |
|
34 tokens = { |
|
35 'root': [ |
|
36 include('basics'), |
|
37 ], |
|
38 'basics': [ |
|
39 (r'\s+', Text), |
|
40 (r'/\*(?:.|\n)*?\*/', Comment), |
|
41 (r'\{', Punctuation, 'content'), |
|
42 (r'\:[\w-]+', Name.Decorator), |
|
43 (r'\.[\w-]+', Name.Class), |
|
44 (r'\#[\w-]+', Name.Function), |
|
45 (r'@[\w-]+', Keyword, 'atrule'), |
|
46 (r'[\w-]+', Name.Tag), |
|
47 (r'[~^*!%&$\[\]()<>|+=@:;,./?-]', Operator), |
|
48 (r'"(\\\\|\\"|[^"])*"', String.Double), |
|
49 (r"'(\\\\|\\'|[^'])*'", String.Single) |
|
50 ], |
|
51 'atrule': [ |
|
52 (r'\{', Punctuation, 'atcontent'), |
|
53 (r';', Punctuation, '#pop'), |
|
54 include('basics'), |
|
55 ], |
|
56 'atcontent': [ |
|
57 include('basics'), |
|
58 (r'\}', Punctuation, '#pop:2'), |
|
59 ], |
|
60 'content': [ |
|
61 (r'\s+', Text), |
|
62 (r'\}', Punctuation, '#pop'), |
|
63 (r'url\(.*?\)', String.Other), |
|
64 (r'^@.*?$', Comment.Preproc), |
|
65 (words(( |
|
66 'azimuth', 'background-attachment', 'background-color', |
|
67 'background-image', 'background-position', 'background-repeat', |
|
68 'background', 'border-bottom-color', 'border-bottom-style', |
|
69 'border-bottom-width', 'border-left-color', 'border-left-style', |
|
70 'border-left-width', 'border-right', 'border-right-color', |
|
71 'border-right-style', 'border-right-width', 'border-top-color', |
|
72 'border-top-style', 'border-top-width', 'border-bottom', |
|
73 'border-collapse', 'border-left', 'border-width', 'border-color', |
|
74 'border-spacing', 'border-style', 'border-top', 'border', 'caption-side', |
|
75 'clear', 'clip', 'color', 'content', 'counter-increment', 'counter-reset', |
|
76 'cue-after', 'cue-before', 'cue', 'cursor', 'direction', 'display', |
|
77 'elevation', 'empty-cells', 'float', 'font-family', 'font-size', |
|
78 'font-size-adjust', 'font-stretch', 'font-style', 'font-variant', |
|
79 'font-weight', 'font', 'height', 'letter-spacing', 'line-height', |
|
80 'list-style-type', 'list-style-image', 'list-style-position', |
|
81 'list-style', 'margin-bottom', 'margin-left', 'margin-right', |
|
82 'margin-top', 'margin', 'marker-offset', 'marks', 'max-height', 'max-width', |
|
83 'min-height', 'min-width', 'opacity', 'orphans', 'outline-color', |
|
84 'outline-style', 'outline-width', 'outline', 'overflow', 'overflow-x', |
|
85 'overflow-y', 'padding-bottom', 'padding-left', 'padding-right', 'padding-top', |
|
86 'padding', 'page', 'page-break-after', 'page-break-before', 'page-break-inside', |
|
87 'pause-after', 'pause-before', 'pause', 'pitch-range', 'pitch', |
|
88 'play-during', 'position', 'quotes', 'richness', 'right', 'size', |
|
89 'speak-header', 'speak-numeral', 'speak-punctuation', 'speak', |
|
90 'speech-rate', 'stress', 'table-layout', 'text-align', 'text-decoration', |
|
91 'text-indent', 'text-shadow', 'text-transform', 'top', 'unicode-bidi', |
|
92 'vertical-align', 'visibility', 'voice-family', 'volume', 'white-space', |
|
93 'widows', 'width', 'word-spacing', 'z-index', 'bottom', |
|
94 'above', 'absolute', 'always', 'armenian', 'aural', 'auto', 'avoid', 'baseline', |
|
95 'behind', 'below', 'bidi-override', 'blink', 'block', 'bolder', 'bold', 'both', |
|
96 'capitalize', 'center-left', 'center-right', 'center', 'circle', |
|
97 'cjk-ideographic', 'close-quote', 'collapse', 'condensed', 'continuous', |
|
98 'crop', 'crosshair', 'cross', 'cursive', 'dashed', 'decimal-leading-zero', |
|
99 'decimal', 'default', 'digits', 'disc', 'dotted', 'double', 'e-resize', 'embed', |
|
100 'extra-condensed', 'extra-expanded', 'expanded', 'fantasy', 'far-left', |
|
101 'far-right', 'faster', 'fast', 'fixed', 'georgian', 'groove', 'hebrew', 'help', |
|
102 'hidden', 'hide', 'higher', 'high', 'hiragana-iroha', 'hiragana', 'icon', |
|
103 'inherit', 'inline-table', 'inline', 'inset', 'inside', 'invert', 'italic', |
|
104 'justify', 'katakana-iroha', 'katakana', 'landscape', 'larger', 'large', |
|
105 'left-side', 'leftwards', 'left', 'level', 'lighter', 'line-through', 'list-item', |
|
106 'loud', 'lower-alpha', 'lower-greek', 'lower-roman', 'lowercase', 'ltr', |
|
107 'lower', 'low', 'medium', 'message-box', 'middle', 'mix', 'monospace', |
|
108 'n-resize', 'narrower', 'ne-resize', 'no-close-quote', 'no-open-quote', |
|
109 'no-repeat', 'none', 'normal', 'nowrap', 'nw-resize', 'oblique', 'once', |
|
110 'open-quote', 'outset', 'outside', 'overline', 'pointer', 'portrait', 'px', |
|
111 'relative', 'repeat-x', 'repeat-y', 'repeat', 'rgb', 'ridge', 'right-side', |
|
112 'rightwards', 's-resize', 'sans-serif', 'scroll', 'se-resize', |
|
113 'semi-condensed', 'semi-expanded', 'separate', 'serif', 'show', 'silent', |
|
114 'slower', 'slow', 'small-caps', 'small-caption', 'smaller', 'soft', 'solid', |
|
115 'spell-out', 'square', 'static', 'status-bar', 'super', 'sw-resize', |
|
116 'table-caption', 'table-cell', 'table-column', 'table-column-group', |
|
117 'table-footer-group', 'table-header-group', 'table-row', |
|
118 'table-row-group', 'text-bottom', 'text-top', 'text', 'thick', 'thin', |
|
119 'transparent', 'ultra-condensed', 'ultra-expanded', 'underline', |
|
120 'upper-alpha', 'upper-latin', 'upper-roman', 'uppercase', 'url', |
|
121 'visible', 'w-resize', 'wait', 'wider', 'x-fast', 'x-high', 'x-large', 'x-loud', |
|
122 'x-low', 'x-small', 'x-soft', 'xx-large', 'xx-small', 'yes'), suffix=r'\b'), |
|
123 Keyword), |
|
124 (words(( |
|
125 'indigo', 'gold', 'firebrick', 'indianred', 'yellow', 'darkolivegreen', |
|
126 'darkseagreen', 'mediumvioletred', 'mediumorchid', 'chartreuse', |
|
127 'mediumslateblue', 'black', 'springgreen', 'crimson', 'lightsalmon', 'brown', |
|
128 'turquoise', 'olivedrab', 'cyan', 'silver', 'skyblue', 'gray', 'darkturquoise', |
|
129 'goldenrod', 'darkgreen', 'darkviolet', 'darkgray', 'lightpink', 'teal', |
|
130 'darkmagenta', 'lightgoldenrodyellow', 'lavender', 'yellowgreen', 'thistle', |
|
131 'violet', 'navy', 'orchid', 'blue', 'ghostwhite', 'honeydew', 'cornflowerblue', |
|
132 'darkblue', 'darkkhaki', 'mediumpurple', 'cornsilk', 'red', 'bisque', 'slategray', |
|
133 'darkcyan', 'khaki', 'wheat', 'deepskyblue', 'darkred', 'steelblue', 'aliceblue', |
|
134 'gainsboro', 'mediumturquoise', 'floralwhite', 'coral', 'purple', 'lightgrey', |
|
135 'lightcyan', 'darksalmon', 'beige', 'azure', 'lightsteelblue', 'oldlace', |
|
136 'greenyellow', 'royalblue', 'lightseagreen', 'mistyrose', 'sienna', |
|
137 'lightcoral', 'orangered', 'navajowhite', 'lime', 'palegreen', 'burlywood', |
|
138 'seashell', 'mediumspringgreen', 'fuchsia', 'papayawhip', 'blanchedalmond', |
|
139 'peru', 'aquamarine', 'white', 'darkslategray', 'ivory', 'dodgerblue', |
|
140 'lemonchiffon', 'chocolate', 'orange', 'forestgreen', 'slateblue', 'olive', |
|
141 'mintcream', 'antiquewhite', 'darkorange', 'cadetblue', 'moccasin', |
|
142 'limegreen', 'saddlebrown', 'darkslateblue', 'lightskyblue', 'deeppink', |
|
143 'plum', 'aqua', 'darkgoldenrod', 'maroon', 'sandybrown', 'magenta', 'tan', |
|
144 'rosybrown', 'pink', 'lightblue', 'palevioletred', 'mediumseagreen', |
|
145 'dimgray', 'powderblue', 'seagreen', 'snow', 'mediumblue', 'midnightblue', |
|
146 'paleturquoise', 'palegoldenrod', 'whitesmoke', 'darkorchid', 'salmon', |
|
147 'lightslategray', 'lawngreen', 'lightgreen', 'tomato', 'hotpink', |
|
148 'lightyellow', 'lavenderblush', 'linen', 'mediumaquamarine', 'green', |
|
149 'blueviolet', 'peachpuff'), suffix=r'\b'), |
|
150 Name.Builtin), |
|
151 (r'\!important', Comment.Preproc), |
|
152 (r'/\*(?:.|\n)*?\*/', Comment), |
|
153 (r'\#[a-zA-Z0-9]{1,6}', Number), |
|
154 (r'[.-]?[0-9]*[.]?[0-9]+(em|px|pt|pc|in|mm|cm|ex|s)\b', Number), |
|
155 # Separate regex for percentages, as can't do word boundaries with % |
|
156 (r'[.-]?[0-9]*[.]?[0-9]+%', Number), |
|
157 (r'-?[0-9]+', Number), |
|
158 (r'[~^*!%&<>|+=@:,./?-]+', Operator), |
|
159 (r'[\[\]();]+', Punctuation), |
|
160 (r'"(\\\\|\\"|[^"])*"', String.Double), |
|
161 (r"'(\\\\|\\'|[^'])*'", String.Single), |
|
162 (r'[a-zA-Z_]\w*', Name) |
|
163 ] |
|
164 } |
|
165 |
|
166 |
|
167 common_sass_tokens = { |
|
168 'value': [ |
|
169 (r'[ \t]+', Text), |
|
170 (r'[!$][\w-]+', Name.Variable), |
|
171 (r'url\(', String.Other, 'string-url'), |
|
172 (r'[a-z_-][\w-]*(?=\()', Name.Function), |
|
173 (words(( |
|
174 'azimuth', 'background-attachment', 'background-color', |
|
175 'background-image', 'background-position', 'background-repeat', |
|
176 'background', 'border-bottom-color', 'border-bottom-style', |
|
177 'border-bottom-width', 'border-left-color', 'border-left-style', |
|
178 'border-left-width', 'border-right', 'border-right-color', |
|
179 'border-right-style', 'border-right-width', 'border-top-color', |
|
180 'border-top-style', 'border-top-width', 'border-bottom', |
|
181 'border-collapse', 'border-left', 'border-width', 'border-color', |
|
182 'border-spacing', 'border-style', 'border-top', 'border', 'caption-side', |
|
183 'clear', 'clip', 'color', 'content', 'counter-increment', 'counter-reset', |
|
184 'cue-after', 'cue-before', 'cue', 'cursor', 'direction', 'display', |
|
185 'elevation', 'empty-cells', 'float', 'font-family', 'font-size', |
|
186 'font-size-adjust', 'font-stretch', 'font-style', 'font-variant', |
|
187 'font-weight', 'font', 'height', 'letter-spacing', 'line-height', |
|
188 'list-style-type', 'list-style-image', 'list-style-position', |
|
189 'list-style', 'margin-bottom', 'margin-left', 'margin-right', |
|
190 'margin-top', 'margin', 'marker-offset', 'marks', 'max-height', 'max-width', |
|
191 'min-height', 'min-width', 'opacity', 'orphans', 'outline', 'outline-color', |
|
192 'outline-style', 'outline-width', 'overflow', 'padding-bottom', |
|
193 'padding-left', 'padding-right', 'padding-top', 'padding', 'page', |
|
194 'page-break-after', 'page-break-before', 'page-break-inside', |
|
195 'pause-after', 'pause-before', 'pause', 'pitch', 'pitch-range', |
|
196 'play-during', 'position', 'quotes', 'richness', 'right', 'size', |
|
197 'speak-header', 'speak-numeral', 'speak-punctuation', 'speak', |
|
198 'speech-rate', 'stress', 'table-layout', 'text-align', 'text-decoration', |
|
199 'text-indent', 'text-shadow', 'text-transform', 'top', 'unicode-bidi', |
|
200 'vertical-align', 'visibility', 'voice-family', 'volume', 'white-space', |
|
201 'widows', 'width', 'word-spacing', 'z-index', 'bottom', 'left', |
|
202 'above', 'absolute', 'always', 'armenian', 'aural', 'auto', 'avoid', 'baseline', |
|
203 'behind', 'below', 'bidi-override', 'blink', 'block', 'bold', 'bolder', 'both', |
|
204 'capitalize', 'center-left', 'center-right', 'center', 'circle', |
|
205 'cjk-ideographic', 'close-quote', 'collapse', 'condensed', 'continuous', |
|
206 'crop', 'crosshair', 'cross', 'cursive', 'dashed', 'decimal-leading-zero', |
|
207 'decimal', 'default', 'digits', 'disc', 'dotted', 'double', 'e-resize', 'embed', |
|
208 'extra-condensed', 'extra-expanded', 'expanded', 'fantasy', 'far-left', |
|
209 'far-right', 'faster', 'fast', 'fixed', 'georgian', 'groove', 'hebrew', 'help', |
|
210 'hidden', 'hide', 'higher', 'high', 'hiragana-iroha', 'hiragana', 'icon', |
|
211 'inherit', 'inline-table', 'inline', 'inset', 'inside', 'invert', 'italic', |
|
212 'justify', 'katakana-iroha', 'katakana', 'landscape', 'larger', 'large', |
|
213 'left-side', 'leftwards', 'level', 'lighter', 'line-through', 'list-item', |
|
214 'loud', 'lower-alpha', 'lower-greek', 'lower-roman', 'lowercase', 'ltr', |
|
215 'lower', 'low', 'medium', 'message-box', 'middle', 'mix', 'monospace', |
|
216 'n-resize', 'narrower', 'ne-resize', 'no-close-quote', 'no-open-quote', |
|
217 'no-repeat', 'none', 'normal', 'nowrap', 'nw-resize', 'oblique', 'once', |
|
218 'open-quote', 'outset', 'outside', 'overline', 'pointer', 'portrait', 'px', |
|
219 'relative', 'repeat-x', 'repeat-y', 'repeat', 'rgb', 'ridge', 'right-side', |
|
220 'rightwards', 's-resize', 'sans-serif', 'scroll', 'se-resize', |
|
221 'semi-condensed', 'semi-expanded', 'separate', 'serif', 'show', 'silent', |
|
222 'slow', 'slower', 'small-caps', 'small-caption', 'smaller', 'soft', 'solid', |
|
223 'spell-out', 'square', 'static', 'status-bar', 'super', 'sw-resize', |
|
224 'table-caption', 'table-cell', 'table-column', 'table-column-group', |
|
225 'table-footer-group', 'table-header-group', 'table-row', |
|
226 'table-row-group', 'text', 'text-bottom', 'text-top', 'thick', 'thin', |
|
227 'transparent', 'ultra-condensed', 'ultra-expanded', 'underline', |
|
228 'upper-alpha', 'upper-latin', 'upper-roman', 'uppercase', 'url', |
|
229 'visible', 'w-resize', 'wait', 'wider', 'x-fast', 'x-high', 'x-large', 'x-loud', |
|
230 'x-low', 'x-small', 'x-soft', 'xx-large', 'xx-small', 'yes'), suffix=r'\b'), |
|
231 Name.Constant), |
|
232 (words(( |
|
233 'indigo', 'gold', 'firebrick', 'indianred', 'darkolivegreen', |
|
234 'darkseagreen', 'mediumvioletred', 'mediumorchid', 'chartreuse', |
|
235 'mediumslateblue', 'springgreen', 'crimson', 'lightsalmon', 'brown', |
|
236 'turquoise', 'olivedrab', 'cyan', 'skyblue', 'darkturquoise', |
|
237 'goldenrod', 'darkgreen', 'darkviolet', 'darkgray', 'lightpink', |
|
238 'darkmagenta', 'lightgoldenrodyellow', 'lavender', 'yellowgreen', 'thistle', |
|
239 'violet', 'orchid', 'ghostwhite', 'honeydew', 'cornflowerblue', |
|
240 'darkblue', 'darkkhaki', 'mediumpurple', 'cornsilk', 'bisque', 'slategray', |
|
241 'darkcyan', 'khaki', 'wheat', 'deepskyblue', 'darkred', 'steelblue', 'aliceblue', |
|
242 'gainsboro', 'mediumturquoise', 'floralwhite', 'coral', 'lightgrey', |
|
243 'lightcyan', 'darksalmon', 'beige', 'azure', 'lightsteelblue', 'oldlace', |
|
244 'greenyellow', 'royalblue', 'lightseagreen', 'mistyrose', 'sienna', |
|
245 'lightcoral', 'orangered', 'navajowhite', 'palegreen', 'burlywood', |
|
246 'seashell', 'mediumspringgreen', 'papayawhip', 'blanchedalmond', |
|
247 'peru', 'aquamarine', 'darkslategray', 'ivory', 'dodgerblue', |
|
248 'lemonchiffon', 'chocolate', 'orange', 'forestgreen', 'slateblue', |
|
249 'mintcream', 'antiquewhite', 'darkorange', 'cadetblue', 'moccasin', |
|
250 'limegreen', 'saddlebrown', 'darkslateblue', 'lightskyblue', 'deeppink', |
|
251 'plum', 'darkgoldenrod', 'sandybrown', 'magenta', 'tan', |
|
252 'rosybrown', 'pink', 'lightblue', 'palevioletred', 'mediumseagreen', |
|
253 'dimgray', 'powderblue', 'seagreen', 'snow', 'mediumblue', 'midnightblue', |
|
254 'paleturquoise', 'palegoldenrod', 'whitesmoke', 'darkorchid', 'salmon', |
|
255 'lightslategray', 'lawngreen', 'lightgreen', 'tomato', 'hotpink', |
|
256 'lightyellow', 'lavenderblush', 'linen', 'mediumaquamarine', |
|
257 'blueviolet', 'peachpuff'), suffix=r'\b'), |
|
258 Name.Entity), |
|
259 (words(( |
|
260 'black', 'silver', 'gray', 'white', 'maroon', 'red', 'purple', 'fuchsia', 'green', |
|
261 'lime', 'olive', 'yellow', 'navy', 'blue', 'teal', 'aqua'), suffix=r'\b'), |
|
262 Name.Builtin), |
|
263 (r'\!(important|default)', Name.Exception), |
|
264 (r'(true|false)', Name.Pseudo), |
|
265 (r'(and|or|not)', Operator.Word), |
|
266 (r'/\*', Comment.Multiline, 'inline-comment'), |
|
267 (r'//[^\n]*', Comment.Single), |
|
268 (r'\#[a-z0-9]{1,6}', Number.Hex), |
|
269 (r'(-?\d+)(\%|[a-z]+)?', bygroups(Number.Integer, Keyword.Type)), |
|
270 (r'(-?\d*\.\d+)(\%|[a-z]+)?', bygroups(Number.Float, Keyword.Type)), |
|
271 (r'#\{', String.Interpol, 'interpolation'), |
|
272 (r'[~^*!&%<>|+=@:,./?-]+', Operator), |
|
273 (r'[\[\]()]+', Punctuation), |
|
274 (r'"', String.Double, 'string-double'), |
|
275 (r"'", String.Single, 'string-single'), |
|
276 (r'[a-z_-][\w-]*', Name), |
|
277 ], |
|
278 |
|
279 'interpolation': [ |
|
280 (r'\}', String.Interpol, '#pop'), |
|
281 include('value'), |
|
282 ], |
|
283 |
|
284 'selector': [ |
|
285 (r'[ \t]+', Text), |
|
286 (r'\:', Name.Decorator, 'pseudo-class'), |
|
287 (r'\.', Name.Class, 'class'), |
|
288 (r'\#', Name.Namespace, 'id'), |
|
289 (r'[\w-]+', Name.Tag), |
|
290 (r'#\{', String.Interpol, 'interpolation'), |
|
291 (r'&', Keyword), |
|
292 (r'[~^*!&\[\]()<>|+=@:;,./?-]', Operator), |
|
293 (r'"', String.Double, 'string-double'), |
|
294 (r"'", String.Single, 'string-single'), |
|
295 ], |
|
296 |
|
297 'string-double': [ |
|
298 (r'(\\.|#(?=[^\n{])|[^\n"#])+', String.Double), |
|
299 (r'#\{', String.Interpol, 'interpolation'), |
|
300 (r'"', String.Double, '#pop'), |
|
301 ], |
|
302 |
|
303 'string-single': [ |
|
304 (r"(\\.|#(?=[^\n{])|[^\n'#])+", String.Double), |
|
305 (r'#\{', String.Interpol, 'interpolation'), |
|
306 (r"'", String.Double, '#pop'), |
|
307 ], |
|
308 |
|
309 'string-url': [ |
|
310 (r'(\\#|#(?=[^\n{])|[^\n#)])+', String.Other), |
|
311 (r'#\{', String.Interpol, 'interpolation'), |
|
312 (r'\)', String.Other, '#pop'), |
|
313 ], |
|
314 |
|
315 'pseudo-class': [ |
|
316 (r'[\w-]+', Name.Decorator), |
|
317 (r'#\{', String.Interpol, 'interpolation'), |
|
318 default('#pop'), |
|
319 ], |
|
320 |
|
321 'class': [ |
|
322 (r'[\w-]+', Name.Class), |
|
323 (r'#\{', String.Interpol, 'interpolation'), |
|
324 default('#pop'), |
|
325 ], |
|
326 |
|
327 'id': [ |
|
328 (r'[\w-]+', Name.Namespace), |
|
329 (r'#\{', String.Interpol, 'interpolation'), |
|
330 default('#pop'), |
|
331 ], |
|
332 |
|
333 'for': [ |
|
334 (r'(from|to|through)', Operator.Word), |
|
335 include('value'), |
|
336 ], |
|
337 } |
|
338 |
|
339 |
|
340 def _indentation(lexer, match, ctx): |
|
341 indentation = match.group(0) |
|
342 yield match.start(), Text, indentation |
|
343 ctx.last_indentation = indentation |
|
344 ctx.pos = match.end() |
|
345 |
|
346 if hasattr(ctx, 'block_state') and ctx.block_state and \ |
|
347 indentation.startswith(ctx.block_indentation) and \ |
|
348 indentation != ctx.block_indentation: |
|
349 ctx.stack.append(ctx.block_state) |
|
350 else: |
|
351 ctx.block_state = None |
|
352 ctx.block_indentation = None |
|
353 ctx.stack.append('content') |
|
354 |
|
355 |
|
356 def _starts_block(token, state): |
|
357 def callback(lexer, match, ctx): |
|
358 yield match.start(), token, match.group(0) |
|
359 |
|
360 if hasattr(ctx, 'last_indentation'): |
|
361 ctx.block_indentation = ctx.last_indentation |
|
362 else: |
|
363 ctx.block_indentation = '' |
|
364 |
|
365 ctx.block_state = state |
|
366 ctx.pos = match.end() |
|
367 |
|
368 return callback |
|
369 |
|
370 |
|
371 class SassLexer(ExtendedRegexLexer): |
|
372 """ |
|
373 For Sass stylesheets. |
|
374 |
|
375 .. versionadded:: 1.3 |
|
376 """ |
|
377 |
|
378 name = 'Sass' |
|
379 aliases = ['sass'] |
|
380 filenames = ['*.sass'] |
|
381 mimetypes = ['text/x-sass'] |
|
382 |
|
383 flags = re.IGNORECASE | re.MULTILINE |
|
384 |
|
385 tokens = { |
|
386 'root': [ |
|
387 (r'[ \t]*\n', Text), |
|
388 (r'[ \t]*', _indentation), |
|
389 ], |
|
390 |
|
391 'content': [ |
|
392 (r'//[^\n]*', _starts_block(Comment.Single, 'single-comment'), |
|
393 'root'), |
|
394 (r'/\*[^\n]*', _starts_block(Comment.Multiline, 'multi-comment'), |
|
395 'root'), |
|
396 (r'@import', Keyword, 'import'), |
|
397 (r'@for', Keyword, 'for'), |
|
398 (r'@(debug|warn|if|while)', Keyword, 'value'), |
|
399 (r'(@mixin)( [\w-]+)', bygroups(Keyword, Name.Function), 'value'), |
|
400 (r'(@include)( [\w-]+)', bygroups(Keyword, Name.Decorator), 'value'), |
|
401 (r'@extend', Keyword, 'selector'), |
|
402 (r'@[\w-]+', Keyword, 'selector'), |
|
403 (r'=[\w-]+', Name.Function, 'value'), |
|
404 (r'\+[\w-]+', Name.Decorator, 'value'), |
|
405 (r'([!$][\w-]\w*)([ \t]*(?:(?:\|\|)?=|:))', |
|
406 bygroups(Name.Variable, Operator), 'value'), |
|
407 (r':', Name.Attribute, 'old-style-attr'), |
|
408 (r'(?=.+?[=:]([^a-z]|$))', Name.Attribute, 'new-style-attr'), |
|
409 default('selector'), |
|
410 ], |
|
411 |
|
412 'single-comment': [ |
|
413 (r'.+', Comment.Single), |
|
414 (r'\n', Text, 'root'), |
|
415 ], |
|
416 |
|
417 'multi-comment': [ |
|
418 (r'.+', Comment.Multiline), |
|
419 (r'\n', Text, 'root'), |
|
420 ], |
|
421 |
|
422 'import': [ |
|
423 (r'[ \t]+', Text), |
|
424 (r'\S+', String), |
|
425 (r'\n', Text, 'root'), |
|
426 ], |
|
427 |
|
428 'old-style-attr': [ |
|
429 (r'[^\s:="\[]+', Name.Attribute), |
|
430 (r'#\{', String.Interpol, 'interpolation'), |
|
431 (r'[ \t]*=', Operator, 'value'), |
|
432 default('value'), |
|
433 ], |
|
434 |
|
435 'new-style-attr': [ |
|
436 (r'[^\s:="\[]+', Name.Attribute), |
|
437 (r'#\{', String.Interpol, 'interpolation'), |
|
438 (r'[ \t]*[=:]', Operator, 'value'), |
|
439 ], |
|
440 |
|
441 'inline-comment': [ |
|
442 (r"(\\#|#(?=[^\n{])|\*(?=[^\n/])|[^\n#*])+", Comment.Multiline), |
|
443 (r'#\{', String.Interpol, 'interpolation'), |
|
444 (r"\*/", Comment, '#pop'), |
|
445 ], |
|
446 } |
|
447 for group, common in iteritems(common_sass_tokens): |
|
448 tokens[group] = copy.copy(common) |
|
449 tokens['value'].append((r'\n', Text, 'root')) |
|
450 tokens['selector'].append((r'\n', Text, 'root')) |
|
451 |
|
452 |
|
453 class ScssLexer(RegexLexer): |
|
454 """ |
|
455 For SCSS stylesheets. |
|
456 """ |
|
457 |
|
458 name = 'SCSS' |
|
459 aliases = ['scss'] |
|
460 filenames = ['*.scss'] |
|
461 mimetypes = ['text/x-scss'] |
|
462 |
|
463 flags = re.IGNORECASE | re.DOTALL |
|
464 tokens = { |
|
465 'root': [ |
|
466 (r'\s+', Text), |
|
467 (r'//.*?\n', Comment.Single), |
|
468 (r'/\*.*?\*/', Comment.Multiline), |
|
469 (r'@import', Keyword, 'value'), |
|
470 (r'@for', Keyword, 'for'), |
|
471 (r'@(debug|warn|if|while)', Keyword, 'value'), |
|
472 (r'(@mixin)( [\w-]+)', bygroups(Keyword, Name.Function), 'value'), |
|
473 (r'(@include)( [\w-]+)', bygroups(Keyword, Name.Decorator), 'value'), |
|
474 (r'@extend', Keyword, 'selector'), |
|
475 (r'(@media)(\s+)', bygroups(Keyword, Text), 'value'), |
|
476 (r'@[\w-]+', Keyword, 'selector'), |
|
477 (r'(\$[\w-]*\w)([ \t]*:)', bygroups(Name.Variable, Operator), 'value'), |
|
478 (r'(?=[^;{}][;}])', Name.Attribute, 'attr'), |
|
479 (r'(?=[^;{}:]+:[^a-z])', Name.Attribute, 'attr'), |
|
480 default('selector'), |
|
481 ], |
|
482 |
|
483 'attr': [ |
|
484 (r'[^\s:="\[]+', Name.Attribute), |
|
485 (r'#\{', String.Interpol, 'interpolation'), |
|
486 (r'[ \t]*:', Operator, 'value'), |
|
487 ], |
|
488 |
|
489 'inline-comment': [ |
|
490 (r"(\\#|#(?=[^{])|\*(?=[^/])|[^#*])+", Comment.Multiline), |
|
491 (r'#\{', String.Interpol, 'interpolation'), |
|
492 (r"\*/", Comment, '#pop'), |
|
493 ], |
|
494 } |
|
495 for group, common in iteritems(common_sass_tokens): |
|
496 tokens[group] = copy.copy(common) |
|
497 tokens['value'].extend([(r'\n', Text), (r'[;{}]', Punctuation, '#pop')]) |
|
498 tokens['selector'].extend([(r'\n', Text), (r'[;{}]', Punctuation, '#pop')]) |