css.py 25 KB

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