|
1 # -*- coding: utf-8 -*- |
|
2 """ |
|
3 pygments.lexers.css |
|
4 ~~~~~~~~~~~~~~~~~~~ |
|
5 |
|
6 Lexers for CSS and related stylesheet formats. |
|
7 |
|
8 :copyright: Copyright 2006-2017 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, inherit |
|
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', 'LessCssLexer'] |
|
22 |
|
23 |
|
24 # List of vendor prefixes obtained from: |
|
25 # https://www.w3.org/TR/CSS21/syndata.html#vendor-keyword-history |
|
26 _vendor_prefixes = ( |
|
27 '-ms-', 'mso-', '-moz-', '-o-', '-xv-', '-atsc-', '-wap-', '-khtml-', |
|
28 '-webkit-', 'prince-', '-ah-', '-hp-', '-ro-', '-rim-', '-tc-', |
|
29 ) |
|
30 |
|
31 # List of CSS properties obtained from: |
|
32 # https://www.w3.org/Style/CSS/all-properties.en.html |
|
33 # Note: handle --* separately |
|
34 _css_properties = ( |
|
35 'align-content', 'align-items', 'align-self', 'alignment-baseline', 'all', |
|
36 'animation', 'animation-delay', 'animation-direction', |
|
37 'animation-duration', 'animation-fill-mode', 'animation-iteration-count', |
|
38 'animation-name', 'animation-play-state', 'animation-timing-function', |
|
39 'appearance', 'azimuth', 'backface-visibility', 'background', |
|
40 'background-attachment', 'background-blend-mode', 'background-clip', |
|
41 'background-color', 'background-image', 'background-origin', |
|
42 'background-position', 'background-repeat', 'background-size', |
|
43 'baseline-shift', 'bookmark-label', 'bookmark-level', 'bookmark-state', |
|
44 'border', 'border-bottom', 'border-bottom-color', |
|
45 'border-bottom-left-radius', 'border-bottom-right-radius', |
|
46 'border-bottom-style', 'border-bottom-width', 'border-boundary', |
|
47 'border-collapse', 'border-color', 'border-image', 'border-image-outset', |
|
48 'border-image-repeat', 'border-image-slice', 'border-image-source', |
|
49 'border-image-width', 'border-left', 'border-left-color', |
|
50 'border-left-style', 'border-left-width', 'border-radius', 'border-right', |
|
51 'border-right-color', 'border-right-style', 'border-right-width', |
|
52 'border-spacing', 'border-style', 'border-top', 'border-top-color', |
|
53 'border-top-left-radius', 'border-top-right-radius', 'border-top-style', |
|
54 'border-top-width', 'border-width', 'bottom', 'box-decoration-break', |
|
55 'box-shadow', 'box-sizing', 'box-snap', 'box-suppress', 'break-after', |
|
56 'break-before', 'break-inside', 'caption-side', 'caret', 'caret-animation', |
|
57 'caret-color', 'caret-shape', 'chains', 'clear', 'clip', 'clip-path', |
|
58 'clip-rule', 'color', 'color-interpolation-filters', 'column-count', |
|
59 'column-fill', 'column-gap', 'column-rule', 'column-rule-color', |
|
60 'column-rule-style', 'column-rule-width', 'column-span', 'column-width', |
|
61 'columns', 'content', 'counter-increment', 'counter-reset', 'counter-set', |
|
62 'crop', 'cue', 'cue-after', 'cue-before', 'cursor', 'direction', 'display', |
|
63 'dominant-baseline', 'elevation', 'empty-cells', 'filter', 'flex', |
|
64 'flex-basis', 'flex-direction', 'flex-flow', 'flex-grow', 'flex-shrink', |
|
65 'flex-wrap', 'float', 'float-defer', 'float-offset', 'float-reference', |
|
66 'flood-color', 'flood-opacity', 'flow', 'flow-from', 'flow-into', 'font', |
|
67 'font-family', 'font-feature-settings', 'font-kerning', |
|
68 'font-language-override', 'font-size', 'font-size-adjust', 'font-stretch', |
|
69 'font-style', 'font-synthesis', 'font-variant', 'font-variant-alternates', |
|
70 'font-variant-caps', 'font-variant-east-asian', 'font-variant-ligatures', |
|
71 'font-variant-numeric', 'font-variant-position', 'font-weight', |
|
72 'footnote-display', 'footnote-policy', 'glyph-orientation-vertical', |
|
73 'grid', 'grid-area', 'grid-auto-columns', 'grid-auto-flow', |
|
74 'grid-auto-rows', 'grid-column', 'grid-column-end', 'grid-column-gap', |
|
75 'grid-column-start', 'grid-gap', 'grid-row', 'grid-row-end', |
|
76 'grid-row-gap', 'grid-row-start', 'grid-template', 'grid-template-areas', |
|
77 'grid-template-columns', 'grid-template-rows', 'hanging-punctuation', |
|
78 'height', 'hyphenate-character', 'hyphenate-limit-chars', |
|
79 'hyphenate-limit-last', 'hyphenate-limit-lines', 'hyphenate-limit-zone', |
|
80 'hyphens', 'image-orientation', 'image-resolution', 'initial-letter', |
|
81 'initial-letter-align', 'initial-letter-wrap', 'isolation', |
|
82 'justify-content', 'justify-items', 'justify-self', 'left', |
|
83 'letter-spacing', 'lighting-color', 'line-break', 'line-grid', |
|
84 'line-height', 'line-snap', 'list-style', 'list-style-image', |
|
85 'list-style-position', 'list-style-type', 'margin', 'margin-bottom', |
|
86 'margin-left', 'margin-right', 'margin-top', 'marker-side', |
|
87 'marquee-direction', 'marquee-loop', 'marquee-speed', 'marquee-style', |
|
88 'mask', 'mask-border', 'mask-border-mode', 'mask-border-outset', |
|
89 'mask-border-repeat', 'mask-border-slice', 'mask-border-source', |
|
90 'mask-border-width', 'mask-clip', 'mask-composite', 'mask-image', |
|
91 'mask-mode', 'mask-origin', 'mask-position', 'mask-repeat', 'mask-size', |
|
92 'mask-type', 'max-height', 'max-lines', 'max-width', 'min-height', |
|
93 'min-width', 'mix-blend-mode', 'motion', 'motion-offset', 'motion-path', |
|
94 'motion-rotation', 'move-to', 'nav-down', 'nav-left', 'nav-right', |
|
95 'nav-up', 'object-fit', 'object-position', 'offset-after', 'offset-before', |
|
96 'offset-end', 'offset-start', 'opacity', 'order', 'orphans', 'outline', |
|
97 'outline-color', 'outline-offset', 'outline-style', 'outline-width', |
|
98 'overflow', 'overflow-style', 'overflow-wrap', 'overflow-x', 'overflow-y', |
|
99 'padding', 'padding-bottom', 'padding-left', 'padding-right', 'padding-top', |
|
100 'page', 'page-break-after', 'page-break-before', 'page-break-inside', |
|
101 'page-policy', 'pause', 'pause-after', 'pause-before', 'perspective', |
|
102 'perspective-origin', 'pitch', 'pitch-range', 'play-during', 'polar-angle', |
|
103 'polar-distance', 'position', 'presentation-level', 'quotes', |
|
104 'region-fragment', 'resize', 'rest', 'rest-after', 'rest-before', |
|
105 'richness', 'right', 'rotation', 'rotation-point', 'ruby-align', |
|
106 'ruby-merge', 'ruby-position', 'running', 'scroll-snap-coordinate', |
|
107 'scroll-snap-destination', 'scroll-snap-points-x', 'scroll-snap-points-y', |
|
108 'scroll-snap-type', 'shape-image-threshold', 'shape-inside', 'shape-margin', |
|
109 'shape-outside', 'size', 'speak', 'speak-as', 'speak-header', |
|
110 'speak-numeral', 'speak-punctuation', 'speech-rate', 'stress', 'string-set', |
|
111 'tab-size', 'table-layout', 'text-align', 'text-align-last', |
|
112 'text-combine-upright', 'text-decoration', 'text-decoration-color', |
|
113 'text-decoration-line', 'text-decoration-skip', 'text-decoration-style', |
|
114 'text-emphasis', 'text-emphasis-color', 'text-emphasis-position', |
|
115 'text-emphasis-style', 'text-indent', 'text-justify', 'text-orientation', |
|
116 'text-overflow', 'text-shadow', 'text-space-collapse', 'text-space-trim', |
|
117 'text-spacing', 'text-transform', 'text-underline-position', 'text-wrap', |
|
118 'top', 'transform', 'transform-origin', 'transform-style', 'transition', |
|
119 'transition-delay', 'transition-duration', 'transition-property', |
|
120 'transition-timing-function', 'unicode-bidi', 'user-select', |
|
121 'vertical-align', 'visibility', 'voice-balance', 'voice-duration', |
|
122 'voice-family', 'voice-pitch', 'voice-range', 'voice-rate', 'voice-stress', |
|
123 'voice-volume', 'volume', 'white-space', 'widows', 'width', 'will-change', |
|
124 'word-break', 'word-spacing', 'word-wrap', 'wrap-after', 'wrap-before', |
|
125 'wrap-flow', 'wrap-inside', 'wrap-through', 'writing-mode', 'z-index', |
|
126 ) |
|
127 |
|
128 # List of keyword values obtained from: |
|
129 # http://cssvalues.com/ |
|
130 _keyword_values = ( |
|
131 'absolute', 'alias', 'all', 'all-petite-caps', 'all-scroll', |
|
132 'all-small-caps', 'allow-end', 'alpha', 'alternate', 'alternate-reverse', |
|
133 'always', 'armenian', 'auto', 'avoid', 'avoid-column', 'avoid-page', |
|
134 'backwards', 'balance', 'baseline', 'below', 'blink', 'block', 'bold', |
|
135 'bolder', 'border-box', 'both', 'bottom', 'box-decoration', 'break-word', |
|
136 'capitalize', 'cell', 'center', 'circle', 'clip', 'clone', 'close-quote', |
|
137 'col-resize', 'collapse', 'color', 'color-burn', 'color-dodge', 'column', |
|
138 'column-reverse', 'compact', 'condensed', 'contain', 'container', |
|
139 'content-box', 'context-menu', 'copy', 'cover', 'crisp-edges', 'crosshair', |
|
140 'currentColor', 'cursive', 'darken', 'dashed', 'decimal', |
|
141 'decimal-leading-zero', 'default', 'descendants', 'difference', 'digits', |
|
142 'disc', 'distribute', 'dot', 'dotted', 'double', 'double-circle', 'e-resize', |
|
143 'each-line', 'ease', 'ease-in', 'ease-in-out', 'ease-out', 'edges', |
|
144 'ellipsis', 'end', 'ew-resize', 'exclusion', 'expanded', 'extra-condensed', |
|
145 'extra-expanded', 'fantasy', 'fill', 'fill-box', 'filled', 'first', 'fixed', |
|
146 'flat', 'flex', 'flex-end', 'flex-start', 'flip', 'force-end', 'forwards', |
|
147 'from-image', 'full-width', 'geometricPrecision', 'georgian', 'groove', |
|
148 'hanging', 'hard-light', 'help', 'hidden', 'hide', 'horizontal', 'hue', |
|
149 'icon', 'infinite', 'inherit', 'initial', 'ink', 'inline', 'inline-block', |
|
150 'inline-flex', 'inline-table', 'inset', 'inside', 'inter-word', 'invert', |
|
151 'isolate', 'italic', 'justify', 'large', 'larger', 'last', 'left', |
|
152 'lighten', 'lighter', 'line-through', 'linear', 'list-item', 'local', |
|
153 'loose', 'lower-alpha', 'lower-greek', 'lower-latin', 'lower-roman', |
|
154 'lowercase', 'ltr', 'luminance', 'luminosity', 'mandatory', 'manipulation', |
|
155 'manual', 'margin-box', 'match-parent', 'medium', 'mixed', 'monospace', |
|
156 'move', 'multiply', 'n-resize', 'ne-resize', 'nesw-resize', |
|
157 'no-close-quote', 'no-drop', 'no-open-quote', 'no-repeat', 'none', 'normal', |
|
158 'not-allowed', 'nowrap', 'ns-resize', 'nw-resize', 'nwse-resize', 'objects', |
|
159 'oblique', 'off', 'on', 'open', 'open-quote', 'optimizeLegibility', |
|
160 'optimizeSpeed', 'outset', 'outside', 'over', 'overlay', 'overline', |
|
161 'padding-box', 'page', 'pan-down', 'pan-left', 'pan-right', 'pan-up', |
|
162 'pan-x', 'pan-y', 'paused', 'petite-caps', 'pixelated', 'pointer', |
|
163 'preserve-3d', 'progress', 'proximity', 'relative', 'repeat', |
|
164 'repeat no-repeat', 'repeat-x', 'repeat-y', 'reverse', 'ridge', 'right', |
|
165 'round', 'row', 'row-resize', 'row-reverse', 'rtl', 'ruby', 'ruby-base', |
|
166 'ruby-base-container', 'ruby-text', 'ruby-text-container', 'run-in', |
|
167 'running', 's-resize', 'sans-serif', 'saturation', 'scale-down', 'screen', |
|
168 'scroll', 'se-resize', 'semi-condensed', 'semi-expanded', 'separate', |
|
169 'serif', 'sesame', 'show', 'sideways', 'sideways-left', 'sideways-right', |
|
170 'slice', 'small', 'small-caps', 'smaller', 'smooth', 'snap', 'soft-light', |
|
171 'solid', 'space', 'space-around', 'space-between', 'spaces', 'square', |
|
172 'start', 'static', 'step-end', 'step-start', 'sticky', 'stretch', 'strict', |
|
173 'stroke-box', 'style', 'sw-resize', 'table', 'table-caption', 'table-cell', |
|
174 'table-column', 'table-column-group', 'table-footer-group', |
|
175 'table-header-group', 'table-row', 'table-row-group', 'text', 'thick', |
|
176 'thin', 'titling-caps', 'to', 'top', 'triangle', 'ultra-condensed', |
|
177 'ultra-expanded', 'under', 'underline', 'unicase', 'unset', 'upper-alpha', |
|
178 'upper-latin', 'upper-roman', 'uppercase', 'upright', 'use-glyph-orientation', |
|
179 'vertical', 'vertical-text', 'view-box', 'visible', 'w-resize', 'wait', |
|
180 'wavy', 'weight', 'weight style', 'wrap', 'wrap-reverse', 'x-large', |
|
181 'x-small', 'xx-large', 'xx-small', 'zoom-in', 'zoom-out', |
|
182 ) |
|
183 |
|
184 # List of extended color keywords obtained from: |
|
185 # https://drafts.csswg.org/css-color/#named-colors |
|
186 _color_keywords = ( |
|
187 'aliceblue', 'antiquewhite', 'aqua', 'aquamarine', 'azure', 'beige', |
|
188 'bisque', 'black', 'blanchedalmond', 'blue', 'blueviolet', 'brown', |
|
189 'burlywood', 'cadetblue', 'chartreuse', 'chocolate', 'coral', |
|
190 'cornflowerblue', 'cornsilk', 'crimson', 'cyan', 'darkblue', 'darkcyan', |
|
191 'darkgoldenrod', 'darkgray', 'darkgreen', 'darkgrey', 'darkkhaki', |
|
192 'darkmagenta', 'darkolivegreen', 'darkorange', 'darkorchid', 'darkred', |
|
193 'darksalmon', 'darkseagreen', 'darkslateblue', 'darkslategray', |
|
194 'darkslategrey', 'darkturquoise', 'darkviolet', 'deeppink', 'deepskyblue', |
|
195 'dimgray', 'dimgrey', 'dodgerblue', 'firebrick', 'floralwhite', |
|
196 'forestgreen', 'fuchsia', 'gainsboro', 'ghostwhite', 'gold', 'goldenrod', |
|
197 'gray', 'green', 'greenyellow', 'grey', 'honeydew', 'hotpink', 'indianred', |
|
198 'indigo', 'ivory', 'khaki', 'lavender', 'lavenderblush', 'lawngreen', |
|
199 'lemonchiffon', 'lightblue', 'lightcoral', 'lightcyan', |
|
200 'lightgoldenrodyellow', 'lightgray', 'lightgreen', 'lightgrey', |
|
201 'lightpink', 'lightsalmon', 'lightseagreen', 'lightskyblue', |
|
202 'lightslategray', 'lightslategrey', 'lightsteelblue', 'lightyellow', |
|
203 'lime', 'limegreen', 'linen', 'magenta', 'maroon', 'mediumaquamarine', |
|
204 'mediumblue', 'mediumorchid', 'mediumpurple', 'mediumseagreen', |
|
205 'mediumslateblue', 'mediumspringgreen', 'mediumturquoise', |
|
206 'mediumvioletred', 'midnightblue', 'mintcream', 'mistyrose', 'moccasin', |
|
207 'navajowhite', 'navy', 'oldlace', 'olive', 'olivedrab', 'orange', |
|
208 'orangered', 'orchid', 'palegoldenrod', 'palegreen', 'paleturquoise', |
|
209 'palevioletred', 'papayawhip', 'peachpuff', 'peru', 'pink', 'plum', |
|
210 'powderblue', 'purple', 'rebeccapurple', 'red', 'rosybrown', 'royalblue', |
|
211 'saddlebrown', 'salmon', 'sandybrown', 'seagreen', 'seashell', 'sienna', |
|
212 'silver', 'skyblue', 'slateblue', 'slategray', 'slategrey', 'snow', |
|
213 'springgreen', 'steelblue', 'tan', 'teal', 'thistle', 'tomato', 'turquoise', |
|
214 'violet', 'wheat', 'white', 'whitesmoke', 'yellow', 'yellowgreen', |
|
215 ) + ('transparent',) |
|
216 |
|
217 # List of other keyword values from other sources: |
|
218 _other_keyword_values = ( |
|
219 'above', 'aural', 'behind', 'bidi-override', 'center-left', 'center-right', |
|
220 'cjk-ideographic', 'continuous', 'crop', 'cross', 'embed', 'far-left', |
|
221 'far-right', 'fast', 'faster', 'hebrew', 'high', 'higher', 'hiragana', |
|
222 'hiragana-iroha', 'katakana', 'katakana-iroha', 'landscape', 'left-side', |
|
223 'leftwards', 'level', 'loud', 'low', 'lower', 'message-box', 'middle', |
|
224 'mix', 'narrower', 'once', 'portrait', 'right-side', 'rightwards', 'silent', |
|
225 'slow', 'slower', 'small-caption', 'soft', 'spell-out', 'status-bar', |
|
226 'super', 'text-bottom', 'text-top', 'wider', 'x-fast', 'x-high', 'x-loud', |
|
227 'x-low', 'x-soft', 'yes', 'pre', 'pre-wrap', 'pre-line', |
|
228 ) |
|
229 |
|
230 # List of functional notation and function keyword values: |
|
231 _functional_notation_keyword_values = ( |
|
232 'attr', 'blackness', 'blend', 'blenda', 'blur', 'brightness', 'calc', |
|
233 'circle', 'color-mod', 'contrast', 'counter', 'cubic-bezier', 'device-cmyk', |
|
234 'drop-shadow', 'ellipse', 'gray', 'grayscale', 'hsl', 'hsla', 'hue', |
|
235 'hue-rotate', 'hwb', 'image', 'inset', 'invert', 'lightness', |
|
236 'linear-gradient', 'matrix', 'matrix3d', 'opacity', 'perspective', |
|
237 'polygon', 'radial-gradient', 'rect', 'repeating-linear-gradient', |
|
238 'repeating-radial-gradient', 'rgb', 'rgba', 'rotate', 'rotate3d', 'rotateX', |
|
239 'rotateY', 'rotateZ', 'saturate', 'saturation', 'scale', 'scale3d', |
|
240 'scaleX', 'scaleY', 'scaleZ', 'sepia', 'shade', 'skewX', 'skewY', 'steps', |
|
241 'tint', 'toggle', 'translate', 'translate3d', 'translateX', 'translateY', |
|
242 'translateZ', 'whiteness', |
|
243 ) |
|
244 # Note! Handle url(...) separately. |
|
245 |
|
246 # List of units obtained from: |
|
247 # https://www.w3.org/TR/css3-values/ |
|
248 _angle_units = ( |
|
249 'deg', 'grad', 'rad', 'turn', |
|
250 ) |
|
251 _frequency_units = ( |
|
252 'Hz', 'kHz', |
|
253 ) |
|
254 _length_units = ( |
|
255 'em', 'ex', 'ch', 'rem', |
|
256 'vh', 'vw', 'vmin', 'vmax', |
|
257 'px', 'mm', 'cm', 'in', 'pt', 'pc', 'q', |
|
258 ) |
|
259 _resolution_units = ( |
|
260 'dpi', 'dpcm', 'dppx', |
|
261 ) |
|
262 _time_units = ( |
|
263 's', 'ms', |
|
264 ) |
|
265 _all_units = _angle_units + _frequency_units + _length_units + \ |
|
266 _resolution_units + _time_units |
|
267 |
|
268 |
|
269 class CssLexer(RegexLexer): |
|
270 """ |
|
271 For CSS (Cascading Style Sheets). |
|
272 """ |
|
273 |
|
274 name = 'CSS' |
|
275 aliases = ['css'] |
|
276 filenames = ['*.css'] |
|
277 mimetypes = ['text/css'] |
|
278 |
|
279 tokens = { |
|
280 'root': [ |
|
281 include('basics'), |
|
282 ], |
|
283 'basics': [ |
|
284 (r'\s+', Text), |
|
285 (r'/\*(?:.|\n)*?\*/', Comment), |
|
286 (r'\{', Punctuation, 'content'), |
|
287 (r'(\:{1,2})([\w-]+)', bygroups(Punctuation, Name.Decorator)), |
|
288 (r'(\.)([\w-]+)', bygroups(Punctuation, Name.Class)), |
|
289 (r'(\#)([\w-]+)', bygroups(Punctuation, Name.Namespace)), |
|
290 (r'(@)([\w-]+)', bygroups(Punctuation, Keyword), 'atrule'), |
|
291 (r'[\w-]+', Name.Tag), |
|
292 (r'[~^*!%&$\[\]()<>|+=@:;,./?-]', Operator), |
|
293 (r'"(\\\\|\\"|[^"])*"', String.Double), |
|
294 (r"'(\\\\|\\'|[^'])*'", String.Single) |
|
295 ], |
|
296 'atrule': [ |
|
297 (r'\{', Punctuation, 'atcontent'), |
|
298 (r';', Punctuation, '#pop'), |
|
299 include('basics'), |
|
300 ], |
|
301 'atcontent': [ |
|
302 include('basics'), |
|
303 (r'\}', Punctuation, '#pop:2'), |
|
304 ], |
|
305 'content': [ |
|
306 (r'\s+', Text), |
|
307 (r'\}', Punctuation, '#pop'), |
|
308 (r';', Punctuation), |
|
309 (r'^@.*?$', Comment.Preproc), |
|
310 |
|
311 (words(_vendor_prefixes,), Keyword.Pseudo), |
|
312 (r'('+r'|'.join(_css_properties)+r')(\s*)(\:)', |
|
313 bygroups(Keyword, Text, Punctuation), 'value-start'), |
|
314 (r'([a-zA-Z_][\w-]*)(\s*)(\:)', bygroups(Name, Text, Punctuation), |
|
315 'value-start'), |
|
316 |
|
317 (r'/\*(?:.|\n)*?\*/', Comment), |
|
318 ], |
|
319 'value-start': [ |
|
320 (r'\s+', Text), |
|
321 (words(_vendor_prefixes,), Name.Builtin.Pseudo), |
|
322 include('urls'), |
|
323 (r'('+r'|'.join(_functional_notation_keyword_values)+r')(\()', |
|
324 bygroups(Name.Builtin, Punctuation), 'function-start'), |
|
325 (r'([a-zA-Z_][\w-]+)(\()', |
|
326 bygroups(Name.Function, Punctuation), 'function-start'), |
|
327 (words(_keyword_values, suffix=r'\b'), Keyword.Constant), |
|
328 (words(_other_keyword_values, suffix=r'\b'), Keyword.Constant), |
|
329 (words(_color_keywords, suffix=r'\b'), Keyword.Constant), |
|
330 # for transition-property etc. |
|
331 (words(_css_properties, suffix=r'\b'), Keyword), |
|
332 (r'\!important', Comment.Preproc), |
|
333 (r'/\*(?:.|\n)*?\*/', Comment), |
|
334 |
|
335 include('numeric-values'), |
|
336 |
|
337 (r'[~^*!%&<>|+=@:./?-]+', Operator), |
|
338 (r'[\[\](),]+', Punctuation), |
|
339 (r'"(\\\\|\\"|[^"])*"', String.Double), |
|
340 (r"'(\\\\|\\'|[^'])*'", String.Single), |
|
341 (r'[a-zA-Z_][\w-]*', Name), |
|
342 (r';', Punctuation, '#pop'), |
|
343 (r'\}', Punctuation, '#pop:2'), |
|
344 ], |
|
345 'function-start': [ |
|
346 (r'\s+', Text), |
|
347 include('urls'), |
|
348 (words(_vendor_prefixes,), Keyword.Pseudo), |
|
349 (words(_keyword_values, suffix=r'\b'), Keyword.Constant), |
|
350 (words(_other_keyword_values, suffix=r'\b'), Keyword.Constant), |
|
351 (words(_color_keywords, suffix=r'\b'), Keyword.Constant), |
|
352 |
|
353 # function-start may be entered recursively |
|
354 (r'(' + r'|'.join(_functional_notation_keyword_values) + r')(\()', |
|
355 bygroups(Name.Builtin, Punctuation), 'function-start'), |
|
356 (r'([a-zA-Z_][\w-]+)(\()', |
|
357 bygroups(Name.Function, Punctuation), 'function-start'), |
|
358 |
|
359 (r'/\*(?:.|\n)*?\*/', Comment), |
|
360 include('numeric-values'), |
|
361 (r'[*+/-]', Operator), |
|
362 (r'[,]', Punctuation), |
|
363 (r'"(\\\\|\\"|[^"])*"', String.Double), |
|
364 (r"'(\\\\|\\'|[^'])*'", String.Single), |
|
365 (r'[a-zA-Z_-]\w*', Name), |
|
366 (r'\)', Punctuation, '#pop'), |
|
367 ], |
|
368 'urls': [ |
|
369 (r'(url)(\()(".*?")(\))', bygroups(Name.Builtin, Punctuation, |
|
370 String.Double, Punctuation)), |
|
371 (r"(url)(\()('.*?')(\))", bygroups(Name.Builtin, Punctuation, |
|
372 String.Single, Punctuation)), |
|
373 (r'(url)(\()(.*?)(\))', bygroups(Name.Builtin, Punctuation, |
|
374 String.Other, Punctuation)), |
|
375 ], |
|
376 'numeric-values': [ |
|
377 (r'\#[a-zA-Z0-9]{1,6}', Number.Hex), |
|
378 (r'[+\-]?[0-9]*[.][0-9]+', Number.Float, 'numeric-end'), |
|
379 (r'[+\-]?[0-9]+', Number.Integer, 'numeric-end'), |
|
380 ], |
|
381 'numeric-end': [ |
|
382 (words(_all_units, suffix=r'\b'), Keyword.Type), |
|
383 (r'%', Keyword.Type), |
|
384 default('#pop'), |
|
385 ], |
|
386 } |
|
387 |
|
388 |
|
389 common_sass_tokens = { |
|
390 'value': [ |
|
391 (r'[ \t]+', Text), |
|
392 (r'[!$][\w-]+', Name.Variable), |
|
393 (r'url\(', String.Other, 'string-url'), |
|
394 (r'[a-z_-][\w-]*(?=\()', Name.Function), |
|
395 (words(_css_properties + ( |
|
396 'above', 'absolute', 'always', 'armenian', 'aural', 'auto', 'avoid', 'baseline', |
|
397 'behind', 'below', 'bidi-override', 'blink', 'block', 'bold', 'bolder', 'both', |
|
398 'capitalize', 'center-left', 'center-right', 'center', 'circle', |
|
399 'cjk-ideographic', 'close-quote', 'collapse', 'condensed', 'continuous', |
|
400 'crop', 'crosshair', 'cross', 'cursive', 'dashed', 'decimal-leading-zero', |
|
401 'decimal', 'default', 'digits', 'disc', 'dotted', 'double', 'e-resize', 'embed', |
|
402 'extra-condensed', 'extra-expanded', 'expanded', 'fantasy', 'far-left', |
|
403 'far-right', 'faster', 'fast', 'fixed', 'georgian', 'groove', 'hebrew', 'help', |
|
404 'hidden', 'hide', 'higher', 'high', 'hiragana-iroha', 'hiragana', 'icon', |
|
405 'inherit', 'inline-table', 'inline', 'inset', 'inside', 'invert', 'italic', |
|
406 'justify', 'katakana-iroha', 'katakana', 'landscape', 'larger', 'large', |
|
407 'left-side', 'leftwards', 'level', 'lighter', 'line-through', 'list-item', |
|
408 'loud', 'lower-alpha', 'lower-greek', 'lower-roman', 'lowercase', 'ltr', |
|
409 'lower', 'low', 'medium', 'message-box', 'middle', 'mix', 'monospace', |
|
410 'n-resize', 'narrower', 'ne-resize', 'no-close-quote', 'no-open-quote', |
|
411 'no-repeat', 'none', 'normal', 'nowrap', 'nw-resize', 'oblique', 'once', |
|
412 'open-quote', 'outset', 'outside', 'overline', 'pointer', 'portrait', 'px', |
|
413 'relative', 'repeat-x', 'repeat-y', 'repeat', 'rgb', 'ridge', 'right-side', |
|
414 'rightwards', 's-resize', 'sans-serif', 'scroll', 'se-resize', |
|
415 'semi-condensed', 'semi-expanded', 'separate', 'serif', 'show', 'silent', |
|
416 'slow', 'slower', 'small-caps', 'small-caption', 'smaller', 'soft', 'solid', |
|
417 'spell-out', 'square', 'static', 'status-bar', 'super', 'sw-resize', |
|
418 'table-caption', 'table-cell', 'table-column', 'table-column-group', |
|
419 'table-footer-group', 'table-header-group', 'table-row', |
|
420 'table-row-group', 'text', 'text-bottom', 'text-top', 'thick', 'thin', |
|
421 'transparent', 'ultra-condensed', 'ultra-expanded', 'underline', |
|
422 'upper-alpha', 'upper-latin', 'upper-roman', 'uppercase', 'url', |
|
423 'visible', 'w-resize', 'wait', 'wider', 'x-fast', 'x-high', 'x-large', 'x-loud', |
|
424 'x-low', 'x-small', 'x-soft', 'xx-large', 'xx-small', 'yes'), suffix=r'\b'), |
|
425 Name.Constant), |
|
426 (words(_color_keywords, suffix=r'\b'), Name.Entity), |
|
427 (words(( |
|
428 'black', 'silver', 'gray', 'white', 'maroon', 'red', 'purple', 'fuchsia', 'green', |
|
429 'lime', 'olive', 'yellow', 'navy', 'blue', 'teal', 'aqua'), suffix=r'\b'), |
|
430 Name.Builtin), |
|
431 (r'\!(important|default)', Name.Exception), |
|
432 (r'(true|false)', Name.Pseudo), |
|
433 (r'(and|or|not)', Operator.Word), |
|
434 (r'/\*', Comment.Multiline, 'inline-comment'), |
|
435 (r'//[^\n]*', Comment.Single), |
|
436 (r'\#[a-z0-9]{1,6}', Number.Hex), |
|
437 (r'(-?\d+)(\%|[a-z]+)?', bygroups(Number.Integer, Keyword.Type)), |
|
438 (r'(-?\d*\.\d+)(\%|[a-z]+)?', bygroups(Number.Float, Keyword.Type)), |
|
439 (r'#\{', String.Interpol, 'interpolation'), |
|
440 (r'[~^*!&%<>|+=@:,./?-]+', Operator), |
|
441 (r'[\[\]()]+', Punctuation), |
|
442 (r'"', String.Double, 'string-double'), |
|
443 (r"'", String.Single, 'string-single'), |
|
444 (r'[a-z_-][\w-]*', Name), |
|
445 ], |
|
446 |
|
447 'interpolation': [ |
|
448 (r'\}', String.Interpol, '#pop'), |
|
449 include('value'), |
|
450 ], |
|
451 |
|
452 'selector': [ |
|
453 (r'[ \t]+', Text), |
|
454 (r'\:', Name.Decorator, 'pseudo-class'), |
|
455 (r'\.', Name.Class, 'class'), |
|
456 (r'\#', Name.Namespace, 'id'), |
|
457 (r'[\w-]+', Name.Tag), |
|
458 (r'#\{', String.Interpol, 'interpolation'), |
|
459 (r'&', Keyword), |
|
460 (r'[~^*!&\[\]()<>|+=@:;,./?-]', Operator), |
|
461 (r'"', String.Double, 'string-double'), |
|
462 (r"'", String.Single, 'string-single'), |
|
463 ], |
|
464 |
|
465 'string-double': [ |
|
466 (r'(\\.|#(?=[^\n{])|[^\n"#])+', String.Double), |
|
467 (r'#\{', String.Interpol, 'interpolation'), |
|
468 (r'"', String.Double, '#pop'), |
|
469 ], |
|
470 |
|
471 'string-single': [ |
|
472 (r"(\\.|#(?=[^\n{])|[^\n'#])+", String.Single), |
|
473 (r'#\{', String.Interpol, 'interpolation'), |
|
474 (r"'", String.Single, '#pop'), |
|
475 ], |
|
476 |
|
477 'string-url': [ |
|
478 (r'(\\#|#(?=[^\n{])|[^\n#)])+', String.Other), |
|
479 (r'#\{', String.Interpol, 'interpolation'), |
|
480 (r'\)', String.Other, '#pop'), |
|
481 ], |
|
482 |
|
483 'pseudo-class': [ |
|
484 (r'[\w-]+', Name.Decorator), |
|
485 (r'#\{', String.Interpol, 'interpolation'), |
|
486 default('#pop'), |
|
487 ], |
|
488 |
|
489 'class': [ |
|
490 (r'[\w-]+', Name.Class), |
|
491 (r'#\{', String.Interpol, 'interpolation'), |
|
492 default('#pop'), |
|
493 ], |
|
494 |
|
495 'id': [ |
|
496 (r'[\w-]+', Name.Namespace), |
|
497 (r'#\{', String.Interpol, 'interpolation'), |
|
498 default('#pop'), |
|
499 ], |
|
500 |
|
501 'for': [ |
|
502 (r'(from|to|through)', Operator.Word), |
|
503 include('value'), |
|
504 ], |
|
505 } |
|
506 |
|
507 |
|
508 def _indentation(lexer, match, ctx): |
|
509 indentation = match.group(0) |
|
510 yield match.start(), Text, indentation |
|
511 ctx.last_indentation = indentation |
|
512 ctx.pos = match.end() |
|
513 |
|
514 if hasattr(ctx, 'block_state') and ctx.block_state and \ |
|
515 indentation.startswith(ctx.block_indentation) and \ |
|
516 indentation != ctx.block_indentation: |
|
517 ctx.stack.append(ctx.block_state) |
|
518 else: |
|
519 ctx.block_state = None |
|
520 ctx.block_indentation = None |
|
521 ctx.stack.append('content') |
|
522 |
|
523 |
|
524 def _starts_block(token, state): |
|
525 def callback(lexer, match, ctx): |
|
526 yield match.start(), token, match.group(0) |
|
527 |
|
528 if hasattr(ctx, 'last_indentation'): |
|
529 ctx.block_indentation = ctx.last_indentation |
|
530 else: |
|
531 ctx.block_indentation = '' |
|
532 |
|
533 ctx.block_state = state |
|
534 ctx.pos = match.end() |
|
535 |
|
536 return callback |
|
537 |
|
538 |
|
539 class SassLexer(ExtendedRegexLexer): |
|
540 """ |
|
541 For Sass stylesheets. |
|
542 |
|
543 .. versionadded:: 1.3 |
|
544 """ |
|
545 |
|
546 name = 'Sass' |
|
547 aliases = ['sass'] |
|
548 filenames = ['*.sass'] |
|
549 mimetypes = ['text/x-sass'] |
|
550 |
|
551 flags = re.IGNORECASE | re.MULTILINE |
|
552 |
|
553 tokens = { |
|
554 'root': [ |
|
555 (r'[ \t]*\n', Text), |
|
556 (r'[ \t]*', _indentation), |
|
557 ], |
|
558 |
|
559 'content': [ |
|
560 (r'//[^\n]*', _starts_block(Comment.Single, 'single-comment'), |
|
561 'root'), |
|
562 (r'/\*[^\n]*', _starts_block(Comment.Multiline, 'multi-comment'), |
|
563 'root'), |
|
564 (r'@import', Keyword, 'import'), |
|
565 (r'@for', Keyword, 'for'), |
|
566 (r'@(debug|warn|if|while)', Keyword, 'value'), |
|
567 (r'(@mixin)( [\w-]+)', bygroups(Keyword, Name.Function), 'value'), |
|
568 (r'(@include)( [\w-]+)', bygroups(Keyword, Name.Decorator), 'value'), |
|
569 (r'@extend', Keyword, 'selector'), |
|
570 (r'@[\w-]+', Keyword, 'selector'), |
|
571 (r'=[\w-]+', Name.Function, 'value'), |
|
572 (r'\+[\w-]+', Name.Decorator, 'value'), |
|
573 (r'([!$][\w-]\w*)([ \t]*(?:(?:\|\|)?=|:))', |
|
574 bygroups(Name.Variable, Operator), 'value'), |
|
575 (r':', Name.Attribute, 'old-style-attr'), |
|
576 (r'(?=.+?[=:]([^a-z]|$))', Name.Attribute, 'new-style-attr'), |
|
577 default('selector'), |
|
578 ], |
|
579 |
|
580 'single-comment': [ |
|
581 (r'.+', Comment.Single), |
|
582 (r'\n', Text, 'root'), |
|
583 ], |
|
584 |
|
585 'multi-comment': [ |
|
586 (r'.+', Comment.Multiline), |
|
587 (r'\n', Text, 'root'), |
|
588 ], |
|
589 |
|
590 'import': [ |
|
591 (r'[ \t]+', Text), |
|
592 (r'\S+', String), |
|
593 (r'\n', Text, 'root'), |
|
594 ], |
|
595 |
|
596 'old-style-attr': [ |
|
597 (r'[^\s:="\[]+', Name.Attribute), |
|
598 (r'#\{', String.Interpol, 'interpolation'), |
|
599 (r'[ \t]*=', Operator, 'value'), |
|
600 default('value'), |
|
601 ], |
|
602 |
|
603 'new-style-attr': [ |
|
604 (r'[^\s:="\[]+', Name.Attribute), |
|
605 (r'#\{', String.Interpol, 'interpolation'), |
|
606 (r'[ \t]*[=:]', Operator, 'value'), |
|
607 ], |
|
608 |
|
609 'inline-comment': [ |
|
610 (r"(\\#|#(?=[^\n{])|\*(?=[^\n/])|[^\n#*])+", Comment.Multiline), |
|
611 (r'#\{', String.Interpol, 'interpolation'), |
|
612 (r"\*/", Comment, '#pop'), |
|
613 ], |
|
614 } |
|
615 for group, common in iteritems(common_sass_tokens): |
|
616 tokens[group] = copy.copy(common) |
|
617 tokens['value'].append((r'\n', Text, 'root')) |
|
618 tokens['selector'].append((r'\n', Text, 'root')) |
|
619 |
|
620 |
|
621 class ScssLexer(RegexLexer): |
|
622 """ |
|
623 For SCSS stylesheets. |
|
624 """ |
|
625 |
|
626 name = 'SCSS' |
|
627 aliases = ['scss'] |
|
628 filenames = ['*.scss'] |
|
629 mimetypes = ['text/x-scss'] |
|
630 |
|
631 flags = re.IGNORECASE | re.DOTALL |
|
632 tokens = { |
|
633 'root': [ |
|
634 (r'\s+', Text), |
|
635 (r'//.*?\n', Comment.Single), |
|
636 (r'/\*.*?\*/', Comment.Multiline), |
|
637 (r'@import', Keyword, 'value'), |
|
638 (r'@for', Keyword, 'for'), |
|
639 (r'@(debug|warn|if|while)', Keyword, 'value'), |
|
640 (r'(@mixin)( [\w-]+)', bygroups(Keyword, Name.Function), 'value'), |
|
641 (r'(@include)( [\w-]+)', bygroups(Keyword, Name.Decorator), 'value'), |
|
642 (r'@extend', Keyword, 'selector'), |
|
643 (r'(@media)(\s+)', bygroups(Keyword, Text), 'value'), |
|
644 (r'@[\w-]+', Keyword, 'selector'), |
|
645 (r'(\$[\w-]*\w)([ \t]*:)', bygroups(Name.Variable, Operator), 'value'), |
|
646 # TODO: broken, and prone to infinite loops. |
|
647 # (r'(?=[^;{}][;}])', Name.Attribute, 'attr'), |
|
648 # (r'(?=[^;{}:]+:[^a-z])', Name.Attribute, 'attr'), |
|
649 default('selector'), |
|
650 ], |
|
651 |
|
652 'attr': [ |
|
653 (r'[^\s:="\[]+', Name.Attribute), |
|
654 (r'#\{', String.Interpol, 'interpolation'), |
|
655 (r'[ \t]*:', Operator, 'value'), |
|
656 default('#pop'), |
|
657 ], |
|
658 |
|
659 'inline-comment': [ |
|
660 (r"(\\#|#(?=[^{])|\*(?=[^/])|[^#*])+", Comment.Multiline), |
|
661 (r'#\{', String.Interpol, 'interpolation'), |
|
662 (r"\*/", Comment, '#pop'), |
|
663 ], |
|
664 } |
|
665 for group, common in iteritems(common_sass_tokens): |
|
666 tokens[group] = copy.copy(common) |
|
667 tokens['value'].extend([(r'\n', Text), (r'[;{}]', Punctuation, '#pop')]) |
|
668 tokens['selector'].extend([(r'\n', Text), (r'[;{}]', Punctuation, '#pop')]) |
|
669 |
|
670 |
|
671 class LessCssLexer(CssLexer): |
|
672 """ |
|
673 For `LESS <http://lesscss.org/>`_ styleshets. |
|
674 |
|
675 .. versionadded:: 2.1 |
|
676 """ |
|
677 |
|
678 name = 'LessCss' |
|
679 aliases = ['less'] |
|
680 filenames = ['*.less'] |
|
681 mimetypes = ['text/x-less-css'] |
|
682 |
|
683 tokens = { |
|
684 'root': [ |
|
685 (r'@\w+', Name.Variable), |
|
686 inherit, |
|
687 ], |
|
688 'content': [ |
|
689 (r'\{', Punctuation, '#push'), |
|
690 inherit, |
|
691 ], |
|
692 } |