css.py 31 KB

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