Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/pygments/lexers/css.py: 97%
75 statements
« prev ^ index » next coverage.py v7.2.7, created at 2023-07-01 06:54 +0000
« prev ^ index » next coverage.py v7.2.7, created at 2023-07-01 06:54 +0000
1"""
2 pygments.lexers.css
3 ~~~~~~~~~~~~~~~~~~~
5 Lexers for CSS and related stylesheet formats.
7 :copyright: Copyright 2006-2023 by the Pygments team, see AUTHORS.
8 :license: BSD, see LICENSE for details.
9"""
11import re
12import copy
14from pygments.lexer import ExtendedRegexLexer, RegexLexer, include, bygroups, \
15 default, words, inherit
16from pygments.token import Comment, Operator, Keyword, Name, String, Number, \
17 Punctuation, Whitespace
18from pygments.lexers._css_builtins import _css_properties
20__all__ = ['CssLexer', 'SassLexer', 'ScssLexer', 'LessCssLexer']
23# List of vendor prefixes obtained from:
24# https://www.w3.org/TR/CSS21/syndata.html#vendor-keyword-history
25_vendor_prefixes = (
26 '-ms-', 'mso-', '-moz-', '-o-', '-xv-', '-atsc-', '-wap-', '-khtml-',
27 '-webkit-', 'prince-', '-ah-', '-hp-', '-ro-', '-rim-', '-tc-',
28)
30# List of extended color keywords obtained from:
31# https://drafts.csswg.org/css-color/#named-colors
32_color_keywords = (
33 'aliceblue', 'antiquewhite', 'aqua', 'aquamarine', 'azure', 'beige',
34 'bisque', 'black', 'blanchedalmond', 'blue', 'blueviolet', 'brown',
35 'burlywood', 'cadetblue', 'chartreuse', 'chocolate', 'coral',
36 'cornflowerblue', 'cornsilk', 'crimson', 'cyan', 'darkblue', 'darkcyan',
37 'darkgoldenrod', 'darkgray', 'darkgreen', 'darkgrey', 'darkkhaki',
38 'darkmagenta', 'darkolivegreen', 'darkorange', 'darkorchid', 'darkred',
39 'darksalmon', 'darkseagreen', 'darkslateblue', 'darkslategray',
40 'darkslategrey', 'darkturquoise', 'darkviolet', 'deeppink', 'deepskyblue',
41 'dimgray', 'dimgrey', 'dodgerblue', 'firebrick', 'floralwhite',
42 'forestgreen', 'fuchsia', 'gainsboro', 'ghostwhite', 'gold', 'goldenrod',
43 'gray', 'green', 'greenyellow', 'grey', 'honeydew', 'hotpink', 'indianred',
44 'indigo', 'ivory', 'khaki', 'lavender', 'lavenderblush', 'lawngreen',
45 'lemonchiffon', 'lightblue', 'lightcoral', 'lightcyan',
46 'lightgoldenrodyellow', 'lightgray', 'lightgreen', 'lightgrey',
47 'lightpink', 'lightsalmon', 'lightseagreen', 'lightskyblue',
48 'lightslategray', 'lightslategrey', 'lightsteelblue', 'lightyellow',
49 'lime', 'limegreen', 'linen', 'magenta', 'maroon', 'mediumaquamarine',
50 'mediumblue', 'mediumorchid', 'mediumpurple', 'mediumseagreen',
51 'mediumslateblue', 'mediumspringgreen', 'mediumturquoise',
52 'mediumvioletred', 'midnightblue', 'mintcream', 'mistyrose', 'moccasin',
53 'navajowhite', 'navy', 'oldlace', 'olive', 'olivedrab', 'orange',
54 'orangered', 'orchid', 'palegoldenrod', 'palegreen', 'paleturquoise',
55 'palevioletred', 'papayawhip', 'peachpuff', 'peru', 'pink', 'plum',
56 'powderblue', 'purple', 'rebeccapurple', 'red', 'rosybrown', 'royalblue',
57 'saddlebrown', 'salmon', 'sandybrown', 'seagreen', 'seashell', 'sienna',
58 'silver', 'skyblue', 'slateblue', 'slategray', 'slategrey', 'snow',
59 'springgreen', 'steelblue', 'tan', 'teal', 'thistle', 'tomato', 'turquoise',
60 'violet', 'wheat', 'white', 'whitesmoke', 'yellow', 'yellowgreen',
61) + ('transparent',)
63# List of keyword values obtained from:
64# http://cssvalues.com/
65_keyword_values = (
66 'absolute', 'alias', 'all', 'all-petite-caps', 'all-scroll',
67 'all-small-caps', 'allow-end', 'alpha', 'alternate', 'alternate-reverse',
68 'always', 'armenian', 'auto', 'avoid', 'avoid-column', 'avoid-page',
69 'backwards', 'balance', 'baseline', 'below', 'blink', 'block', 'bold',
70 'bolder', 'border-box', 'both', 'bottom', 'box-decoration', 'break-word',
71 'capitalize', 'cell', 'center', 'circle', 'clip', 'clone', 'close-quote',
72 'col-resize', 'collapse', 'color', 'color-burn', 'color-dodge', 'column',
73 'column-reverse', 'compact', 'condensed', 'contain', 'container',
74 'content-box', 'context-menu', 'copy', 'cover', 'crisp-edges', 'crosshair',
75 'currentColor', 'cursive', 'darken', 'dashed', 'decimal',
76 'decimal-leading-zero', 'default', 'descendants', 'difference', 'digits',
77 'disc', 'distribute', 'dot', 'dotted', 'double', 'double-circle', 'e-resize',
78 'each-line', 'ease', 'ease-in', 'ease-in-out', 'ease-out', 'edges',
79 'ellipsis', 'end', 'ew-resize', 'exclusion', 'expanded', 'extra-condensed',
80 'extra-expanded', 'fantasy', 'fill', 'fill-box', 'filled', 'first', 'fixed',
81 'flat', 'flex', 'flex-end', 'flex-start', 'flip', 'force-end', 'forwards',
82 'from-image', 'full-width', 'geometricPrecision', 'georgian', 'groove',
83 'hanging', 'hard-light', 'help', 'hidden', 'hide', 'horizontal', 'hue',
84 'icon', 'infinite', 'inherit', 'initial', 'ink', 'inline', 'inline-block',
85 'inline-flex', 'inline-table', 'inset', 'inside', 'inter-word', 'invert',
86 'isolate', 'italic', 'justify', 'large', 'larger', 'last', 'left',
87 'lighten', 'lighter', 'line-through', 'linear', 'list-item', 'local',
88 'loose', 'lower-alpha', 'lower-greek', 'lower-latin', 'lower-roman',
89 'lowercase', 'ltr', 'luminance', 'luminosity', 'mandatory', 'manipulation',
90 'manual', 'margin-box', 'match-parent', 'medium', 'mixed', 'monospace',
91 'move', 'multiply', 'n-resize', 'ne-resize', 'nesw-resize',
92 'no-close-quote', 'no-drop', 'no-open-quote', 'no-repeat', 'none', 'normal',
93 'not-allowed', 'nowrap', 'ns-resize', 'nw-resize', 'nwse-resize', 'objects',
94 'oblique', 'off', 'on', 'open', 'open-quote', 'optimizeLegibility',
95 'optimizeSpeed', 'outset', 'outside', 'over', 'overlay', 'overline',
96 'padding-box', 'page', 'pan-down', 'pan-left', 'pan-right', 'pan-up',
97 'pan-x', 'pan-y', 'paused', 'petite-caps', 'pixelated', 'pointer',
98 'preserve-3d', 'progress', 'proximity', 'relative', 'repeat',
99 'repeat no-repeat', 'repeat-x', 'repeat-y', 'reverse', 'ridge', 'right',
100 'round', 'row', 'row-resize', 'row-reverse', 'rtl', 'ruby', 'ruby-base',
101 'ruby-base-container', 'ruby-text', 'ruby-text-container', 'run-in',
102 'running', 's-resize', 'sans-serif', 'saturation', 'scale-down', 'screen',
103 'scroll', 'se-resize', 'semi-condensed', 'semi-expanded', 'separate',
104 'serif', 'sesame', 'show', 'sideways', 'sideways-left', 'sideways-right',
105 'slice', 'small', 'small-caps', 'smaller', 'smooth', 'snap', 'soft-light',
106 'solid', 'space', 'space-around', 'space-between', 'spaces', 'square',
107 'start', 'static', 'step-end', 'step-start', 'sticky', 'stretch', 'strict',
108 'stroke-box', 'style', 'sw-resize', 'table', 'table-caption', 'table-cell',
109 'table-column', 'table-column-group', 'table-footer-group',
110 'table-header-group', 'table-row', 'table-row-group', 'text', 'thick',
111 'thin', 'titling-caps', 'to', 'top', 'triangle', 'ultra-condensed',
112 'ultra-expanded', 'under', 'underline', 'unicase', 'unset', 'upper-alpha',
113 'upper-latin', 'upper-roman', 'uppercase', 'upright', 'use-glyph-orientation',
114 'vertical', 'vertical-text', 'view-box', 'visible', 'w-resize', 'wait',
115 'wavy', 'weight', 'weight style', 'wrap', 'wrap-reverse', 'x-large',
116 'x-small', 'xx-large', 'xx-small', 'zoom-in', 'zoom-out',
117)
119# List of other keyword values from other sources:
120_other_keyword_values = (
121 'above', 'aural', 'behind', 'bidi-override', 'center-left', 'center-right',
122 'cjk-ideographic', 'continuous', 'crop', 'cross', 'embed', 'far-left',
123 'far-right', 'fast', 'faster', 'hebrew', 'high', 'higher', 'hiragana',
124 'hiragana-iroha', 'katakana', 'katakana-iroha', 'landscape', 'left-side',
125 'leftwards', 'level', 'loud', 'low', 'lower', 'message-box', 'middle',
126 'mix', 'narrower', 'once', 'portrait', 'right-side', 'rightwards', 'silent',
127 'slow', 'slower', 'small-caption', 'soft', 'spell-out', 'status-bar',
128 'super', 'text-bottom', 'text-top', 'wider', 'x-fast', 'x-high', 'x-loud',
129 'x-low', 'x-soft', 'yes', 'pre', 'pre-wrap', 'pre-line',
130)
132# List of functional notation and function keyword values:
133_functional_notation_keyword_values = (
134 'attr', 'blackness', 'blend', 'blenda', 'blur', 'brightness', 'calc',
135 'circle', 'color-mod', 'contrast', 'counter', 'cubic-bezier', 'device-cmyk',
136 'drop-shadow', 'ellipse', 'gray', 'grayscale', 'hsl', 'hsla', 'hue',
137 'hue-rotate', 'hwb', 'image', 'inset', 'invert', 'lightness',
138 'linear-gradient', 'matrix', 'matrix3d', 'opacity', 'perspective',
139 'polygon', 'radial-gradient', 'rect', 'repeating-linear-gradient',
140 'repeating-radial-gradient', 'rgb', 'rgba', 'rotate', 'rotate3d', 'rotateX',
141 'rotateY', 'rotateZ', 'saturate', 'saturation', 'scale', 'scale3d',
142 'scaleX', 'scaleY', 'scaleZ', 'sepia', 'shade', 'skewX', 'skewY', 'steps',
143 'tint', 'toggle', 'translate', 'translate3d', 'translateX', 'translateY',
144 'translateZ', 'whiteness',
145)
146# Note! Handle url(...) separately.
148# List of units obtained from:
149# https://www.w3.org/TR/css3-values/
150_angle_units = (
151 'deg', 'grad', 'rad', 'turn',
152)
153_frequency_units = (
154 'Hz', 'kHz',
155)
156_length_units = (
157 'em', 'ex', 'ch', 'rem',
158 'vh', 'vw', 'vmin', 'vmax',
159 'px', 'mm', 'cm', 'in', 'pt', 'pc', 'q',
160)
161_resolution_units = (
162 'dpi', 'dpcm', 'dppx',
163)
164_time_units = (
165 's', 'ms',
166)
167_all_units = _angle_units + _frequency_units + _length_units + \
168 _resolution_units + _time_units
171class CssLexer(RegexLexer):
172 """
173 For CSS (Cascading Style Sheets).
174 """
176 name = 'CSS'
177 url = 'https://www.w3.org/TR/CSS/#css'
178 aliases = ['css']
179 filenames = ['*.css']
180 mimetypes = ['text/css']
182 tokens = {
183 'root': [
184 include('basics'),
185 ],
186 'basics': [
187 (r'\s+', Whitespace),
188 (r'/\*(?:.|\n)*?\*/', Comment),
189 (r'\{', Punctuation, 'content'),
190 (r'(\:{1,2})([\w-]+)', bygroups(Punctuation, Name.Decorator)),
191 (r'(\.)([\w-]+)', bygroups(Punctuation, Name.Class)),
192 (r'(\#)([\w-]+)', bygroups(Punctuation, Name.Namespace)),
193 (r'(@)([\w-]+)', bygroups(Punctuation, Keyword), 'atrule'),
194 (r'[\w-]+', Name.Tag),
195 (r'[~^*!%&$\[\]()<>|+=@:;,./?-]', Operator),
196 (r'"(\\\\|\\[^\\]|[^"\\])*"', String.Double),
197 (r"'(\\\\|\\[^\\]|[^'\\])*'", String.Single),
198 ],
199 'atrule': [
200 (r'\{', Punctuation, 'atcontent'),
201 (r';', Punctuation, '#pop'),
202 include('basics'),
203 ],
204 'atcontent': [
205 include('basics'),
206 (r'\}', Punctuation, '#pop:2'),
207 ],
208 'content': [
209 (r'\s+', Whitespace),
210 (r'\}', Punctuation, '#pop'),
211 (r';', Punctuation),
212 (r'^@.*?$', Comment.Preproc),
214 (words(_vendor_prefixes,), Keyword.Pseudo),
215 (r'('+r'|'.join(_css_properties)+r')(\s*)(\:)',
216 bygroups(Keyword, Whitespace, Punctuation), 'value-start'),
217 (r'([-]+[a-zA-Z_][\w-]*)(\s*)(\:)', bygroups(Name.Variable, Whitespace, Punctuation),
218 'value-start'),
219 (r'([a-zA-Z_][\w-]*)(\s*)(\:)', bygroups(Name, Whitespace, Punctuation),
220 'value-start'),
222 (r'/\*(?:.|\n)*?\*/', Comment),
223 ],
224 'value-start': [
225 (r'\s+', Whitespace),
226 (words(_vendor_prefixes,), Name.Builtin.Pseudo),
227 include('urls'),
228 (r'('+r'|'.join(_functional_notation_keyword_values)+r')(\()',
229 bygroups(Name.Builtin, Punctuation), 'function-start'),
230 (r'([a-zA-Z_][\w-]+)(\()',
231 bygroups(Name.Function, Punctuation), 'function-start'),
232 (words(_keyword_values, suffix=r'\b'), Keyword.Constant),
233 (words(_other_keyword_values, suffix=r'\b'), Keyword.Constant),
234 (words(_color_keywords, suffix=r'\b'), Keyword.Constant),
235 # for transition-property etc.
236 (words(_css_properties, suffix=r'\b'), Keyword),
237 (r'\!important', Comment.Preproc),
238 (r'/\*(?:.|\n)*?\*/', Comment),
240 include('numeric-values'),
242 (r'[~^*!%&<>|+=@:./?-]+', Operator),
243 (r'[\[\](),]+', Punctuation),
244 (r'"(\\\\|\\[^\\]|[^"\\])*"', String.Double),
245 (r"'(\\\\|\\[^\\]|[^'\\])*'", String.Single),
246 (r'[a-zA-Z_][\w-]*', Name),
247 (r';', Punctuation, '#pop'),
248 (r'\}', Punctuation, '#pop:2'),
249 ],
250 'function-start': [
251 (r'\s+', Whitespace),
252 (r'[-]+([A-Za-z][\w+]*[-]*)+', Name.Variable),
253 include('urls'),
254 (words(_vendor_prefixes,), Keyword.Pseudo),
255 (words(_keyword_values, suffix=r'\b'), Keyword.Constant),
256 (words(_other_keyword_values, suffix=r'\b'), Keyword.Constant),
257 (words(_color_keywords, suffix=r'\b'), Keyword.Constant),
259 # function-start may be entered recursively
260 (r'(' + r'|'.join(_functional_notation_keyword_values) + r')(\()',
261 bygroups(Name.Builtin, Punctuation), 'function-start'),
262 (r'([a-zA-Z_][\w-]+)(\()',
263 bygroups(Name.Function, Punctuation), 'function-start'),
265 (r'/\*(?:.|\n)*?\*/', Comment),
266 include('numeric-values'),
267 (r'[*+/-]', Operator),
268 (r',', Punctuation),
269 (r'"(\\\\|\\[^\\]|[^"\\])*"', String.Double),
270 (r"'(\\\\|\\[^\\]|[^'\\])*'", String.Single),
271 (r'[a-zA-Z_-]\w*', Name),
272 (r'\)', Punctuation, '#pop'),
273 ],
274 'urls': [
275 (r'(url)(\()(".*?")(\))', bygroups(Name.Builtin, Punctuation,
276 String.Double, Punctuation)),
277 (r"(url)(\()('.*?')(\))", bygroups(Name.Builtin, Punctuation,
278 String.Single, Punctuation)),
279 (r'(url)(\()(.*?)(\))', bygroups(Name.Builtin, Punctuation,
280 String.Other, Punctuation)),
281 ],
282 'numeric-values': [
283 (r'\#[a-zA-Z0-9]{1,6}', Number.Hex),
284 (r'[+\-]?[0-9]*[.][0-9]+', Number.Float, 'numeric-end'),
285 (r'[+\-]?[0-9]+', Number.Integer, 'numeric-end'),
286 ],
287 'numeric-end': [
288 (words(_all_units, suffix=r'\b'), Keyword.Type),
289 (r'%', Keyword.Type),
290 default('#pop'),
291 ],
292 }
295common_sass_tokens = {
296 'value': [
297 (r'[ \t]+', Whitespace),
298 (r'[!$][\w-]+', Name.Variable),
299 (r'url\(', String.Other, 'string-url'),
300 (r'[a-z_-][\w-]*(?=\()', Name.Function),
301 (words(_css_properties + (
302 'above', 'absolute', 'always', 'armenian', 'aural', 'auto', 'avoid', 'baseline',
303 'behind', 'below', 'bidi-override', 'blink', 'block', 'bold', 'bolder', 'both',
304 'capitalize', 'center-left', 'center-right', 'center', 'circle',
305 'cjk-ideographic', 'close-quote', 'collapse', 'condensed', 'continuous',
306 'crosshair', 'cross', 'cursive', 'dashed', 'decimal-leading-zero',
307 'decimal', 'default', 'digits', 'disc', 'dotted', 'double', 'e-resize', 'embed',
308 'extra-condensed', 'extra-expanded', 'expanded', 'fantasy', 'far-left',
309 'far-right', 'faster', 'fast', 'fixed', 'georgian', 'groove', 'hebrew', 'help',
310 'hidden', 'hide', 'higher', 'high', 'hiragana-iroha', 'hiragana', 'icon',
311 'inherit', 'inline-table', 'inline', 'inset', 'inside', 'invert', 'italic',
312 'justify', 'katakana-iroha', 'katakana', 'landscape', 'larger', 'large',
313 'left-side', 'leftwards', 'level', 'lighter', 'line-through', 'list-item',
314 'loud', 'lower-alpha', 'lower-greek', 'lower-roman', 'lowercase', 'ltr',
315 'lower', 'low', 'medium', 'message-box', 'middle', 'mix', 'monospace',
316 'n-resize', 'narrower', 'ne-resize', 'no-close-quote', 'no-open-quote',
317 'no-repeat', 'none', 'normal', 'nowrap', 'nw-resize', 'oblique', 'once',
318 'open-quote', 'outset', 'outside', 'overline', 'pointer', 'portrait', 'px',
319 'relative', 'repeat-x', 'repeat-y', 'repeat', 'rgb', 'ridge', 'right-side',
320 'rightwards', 's-resize', 'sans-serif', 'scroll', 'se-resize',
321 'semi-condensed', 'semi-expanded', 'separate', 'serif', 'show', 'silent',
322 'slow', 'slower', 'small-caps', 'small-caption', 'smaller', 'soft', 'solid',
323 'spell-out', 'square', 'static', 'status-bar', 'super', 'sw-resize',
324 'table-caption', 'table-cell', 'table-column', 'table-column-group',
325 'table-footer-group', 'table-header-group', 'table-row',
326 'table-row-group', 'text', 'text-bottom', 'text-top', 'thick', 'thin',
327 'transparent', 'ultra-condensed', 'ultra-expanded', 'underline',
328 'upper-alpha', 'upper-latin', 'upper-roman', 'uppercase', 'url',
329 'visible', 'w-resize', 'wait', 'wider', 'x-fast', 'x-high', 'x-large', 'x-loud',
330 'x-low', 'x-small', 'x-soft', 'xx-large', 'xx-small', 'yes'), suffix=r'\b'),
331 Name.Constant),
332 (words(_color_keywords, suffix=r'\b'), Name.Entity),
333 (words((
334 'black', 'silver', 'gray', 'white', 'maroon', 'red', 'purple', 'fuchsia', 'green',
335 'lime', 'olive', 'yellow', 'navy', 'blue', 'teal', 'aqua'), suffix=r'\b'),
336 Name.Builtin),
337 (r'\!(important|default)', Name.Exception),
338 (r'(true|false)', Name.Pseudo),
339 (r'(and|or|not)', Operator.Word),
340 (r'/\*', Comment.Multiline, 'inline-comment'),
341 (r'//[^\n]*', Comment.Single),
342 (r'\#[a-z0-9]{1,6}', Number.Hex),
343 (r'(-?\d+)(\%|[a-z]+)?', bygroups(Number.Integer, Keyword.Type)),
344 (r'(-?\d*\.\d+)(\%|[a-z]+)?', bygroups(Number.Float, Keyword.Type)),
345 (r'#\{', String.Interpol, 'interpolation'),
346 (r'[~^*!&%<>|+=@:,./?-]+', Operator),
347 (r'[\[\]()]+', Punctuation),
348 (r'"', String.Double, 'string-double'),
349 (r"'", String.Single, 'string-single'),
350 (r'[a-z_-][\w-]*', Name),
351 ],
353 'interpolation': [
354 (r'\}', String.Interpol, '#pop'),
355 include('value'),
356 ],
358 'selector': [
359 (r'[ \t]+', Whitespace),
360 (r'\:', Name.Decorator, 'pseudo-class'),
361 (r'\.', Name.Class, 'class'),
362 (r'\#', Name.Namespace, 'id'),
363 (r'[\w-]+', Name.Tag),
364 (r'#\{', String.Interpol, 'interpolation'),
365 (r'&', Keyword),
366 (r'[~^*!&\[\]()<>|+=@:;,./?-]', Operator),
367 (r'"', String.Double, 'string-double'),
368 (r"'", String.Single, 'string-single'),
369 ],
371 'string-double': [
372 (r'(\\.|#(?=[^\n{])|[^\n"#])+', String.Double),
373 (r'#\{', String.Interpol, 'interpolation'),
374 (r'"', String.Double, '#pop'),
375 ],
377 'string-single': [
378 (r"(\\.|#(?=[^\n{])|[^\n'#])+", String.Single),
379 (r'#\{', String.Interpol, 'interpolation'),
380 (r"'", String.Single, '#pop'),
381 ],
383 'string-url': [
384 (r'(\\#|#(?=[^\n{])|[^\n#)])+', String.Other),
385 (r'#\{', String.Interpol, 'interpolation'),
386 (r'\)', String.Other, '#pop'),
387 ],
389 'pseudo-class': [
390 (r'[\w-]+', Name.Decorator),
391 (r'#\{', String.Interpol, 'interpolation'),
392 default('#pop'),
393 ],
395 'class': [
396 (r'[\w-]+', Name.Class),
397 (r'#\{', String.Interpol, 'interpolation'),
398 default('#pop'),
399 ],
401 'id': [
402 (r'[\w-]+', Name.Namespace),
403 (r'#\{', String.Interpol, 'interpolation'),
404 default('#pop'),
405 ],
407 'for': [
408 (r'(from|to|through)', Operator.Word),
409 include('value'),
410 ],
411}
414def _indentation(lexer, match, ctx):
415 indentation = match.group(0)
416 yield match.start(), Whitespace, indentation
417 ctx.last_indentation = indentation
418 ctx.pos = match.end()
420 if hasattr(ctx, 'block_state') and ctx.block_state and \
421 indentation.startswith(ctx.block_indentation) and \
422 indentation != ctx.block_indentation:
423 ctx.stack.append(ctx.block_state)
424 else:
425 ctx.block_state = None
426 ctx.block_indentation = None
427 ctx.stack.append('content')
430def _starts_block(token, state):
431 def callback(lexer, match, ctx):
432 yield match.start(), token, match.group(0)
434 if hasattr(ctx, 'last_indentation'):
435 ctx.block_indentation = ctx.last_indentation
436 else:
437 ctx.block_indentation = ''
439 ctx.block_state = state
440 ctx.pos = match.end()
442 return callback
445class SassLexer(ExtendedRegexLexer):
446 """
447 For Sass stylesheets.
449 .. versionadded:: 1.3
450 """
452 name = 'Sass'
453 url = 'https://sass-lang.com/'
454 aliases = ['sass']
455 filenames = ['*.sass']
456 mimetypes = ['text/x-sass']
458 flags = re.IGNORECASE | re.MULTILINE
460 tokens = {
461 'root': [
462 (r'[ \t]*\n', Whitespace),
463 (r'[ \t]*', _indentation),
464 ],
466 'content': [
467 (r'//[^\n]*', _starts_block(Comment.Single, 'single-comment'),
468 'root'),
469 (r'/\*[^\n]*', _starts_block(Comment.Multiline, 'multi-comment'),
470 'root'),
471 (r'@import', Keyword, 'import'),
472 (r'@for', Keyword, 'for'),
473 (r'@(debug|warn|if|while)', Keyword, 'value'),
474 (r'(@mixin)( )([\w-]+)', bygroups(Keyword, Whitespace, Name.Function), 'value'),
475 (r'(@include)( )([\w-]+)', bygroups(Keyword, Whitespace, Name.Decorator), 'value'),
476 (r'@extend', Keyword, 'selector'),
477 (r'@[\w-]+', Keyword, 'selector'),
478 (r'=[\w-]+', Name.Function, 'value'),
479 (r'\+[\w-]+', Name.Decorator, 'value'),
480 (r'([!$][\w-]\w*)([ \t]*(?:(?:\|\|)?=|:))',
481 bygroups(Name.Variable, Operator), 'value'),
482 (r':', Name.Attribute, 'old-style-attr'),
483 (r'(?=.+?[=:]([^a-z]|$))', Name.Attribute, 'new-style-attr'),
484 default('selector'),
485 ],
487 'single-comment': [
488 (r'.+', Comment.Single),
489 (r'\n', Whitespace, 'root'),
490 ],
492 'multi-comment': [
493 (r'.+', Comment.Multiline),
494 (r'\n', Whitespace, 'root'),
495 ],
497 'import': [
498 (r'[ \t]+', Whitespace),
499 (r'\S+', String),
500 (r'\n', Whitespace, 'root'),
501 ],
503 'old-style-attr': [
504 (r'[^\s:="\[]+', Name.Attribute),
505 (r'#\{', String.Interpol, 'interpolation'),
506 (r'([ \t]*)(=)', bygroups(Whitespace, Operator), 'value'),
507 default('value'),
508 ],
510 'new-style-attr': [
511 (r'[^\s:="\[]+', Name.Attribute),
512 (r'#\{', String.Interpol, 'interpolation'),
513 (r'([ \t]*)([=:])', bygroups(Whitespace, Operator), 'value'),
514 ],
516 'inline-comment': [
517 (r"(\\#|#(?=[^\n{])|\*(?=[^\n/])|[^\n#*])+", Comment.Multiline),
518 (r'#\{', String.Interpol, 'interpolation'),
519 (r"\*/", Comment, '#pop'),
520 ],
521 }
522 for group, common in common_sass_tokens.items():
523 tokens[group] = copy.copy(common)
524 tokens['value'].append((r'\n', Whitespace, 'root'))
525 tokens['selector'].append((r'\n', Whitespace, 'root'))
528class ScssLexer(RegexLexer):
529 """
530 For SCSS stylesheets.
531 """
533 name = 'SCSS'
534 url = 'https://sass-lang.com/'
535 aliases = ['scss']
536 filenames = ['*.scss']
537 mimetypes = ['text/x-scss']
539 flags = re.IGNORECASE | re.DOTALL
540 tokens = {
541 'root': [
542 (r'\s+', Whitespace),
543 (r'//.*?\n', Comment.Single),
544 (r'/\*.*?\*/', Comment.Multiline),
545 (r'@import', Keyword, 'value'),
546 (r'@for', Keyword, 'for'),
547 (r'@(debug|warn|if|while)', Keyword, 'value'),
548 (r'(@mixin)( [\w-]+)', bygroups(Keyword, Name.Function), 'value'),
549 (r'(@include)( [\w-]+)', bygroups(Keyword, Name.Decorator), 'value'),
550 (r'@extend', Keyword, 'selector'),
551 (r'(@media)(\s+)', bygroups(Keyword, Whitespace), 'value'),
552 (r'@[\w-]+', Keyword, 'selector'),
553 (r'(\$[\w-]*\w)([ \t]*:)', bygroups(Name.Variable, Operator), 'value'),
554 # TODO: broken, and prone to infinite loops.
555 # (r'(?=[^;{}][;}])', Name.Attribute, 'attr'),
556 # (r'(?=[^;{}:]+:[^a-z])', Name.Attribute, 'attr'),
557 default('selector'),
558 ],
560 'attr': [
561 (r'[^\s:="\[]+', Name.Attribute),
562 (r'#\{', String.Interpol, 'interpolation'),
563 (r'[ \t]*:', Operator, 'value'),
564 default('#pop'),
565 ],
567 'inline-comment': [
568 (r"(\\#|#(?=[^{])|\*(?=[^/])|[^#*])+", Comment.Multiline),
569 (r'#\{', String.Interpol, 'interpolation'),
570 (r"\*/", Comment, '#pop'),
571 ],
572 }
573 for group, common in common_sass_tokens.items():
574 tokens[group] = copy.copy(common)
575 tokens['value'].extend([(r'\n', Whitespace), (r'[;{}]', Punctuation, '#pop')])
576 tokens['selector'].extend([(r'\n', Whitespace), (r'[;{}]', Punctuation, '#pop')])
579class LessCssLexer(CssLexer):
580 """
581 For LESS styleshets.
583 .. versionadded:: 2.1
584 """
586 name = 'LessCss'
587 url = 'http://lesscss.org/'
588 aliases = ['less']
589 filenames = ['*.less']
590 mimetypes = ['text/x-less-css']
592 tokens = {
593 'root': [
594 (r'@\w+', Name.Variable),
595 inherit,
596 ],
597 'content': [
598 (r'\{', Punctuation, '#push'),
599 (r'//.*\n', Comment.Single),
600 inherit,
601 ],
602 }