css.py 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
  1. """
  2. pygments.lexers.css
  3. ~~~~~~~~~~~~~~~~~~~
  4. Lexers for CSS and related stylesheet formats.
  5. :copyright: Copyright 2006-2023 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. tokens = {
  167. 'root': [
  168. include('basics'),
  169. ],
  170. 'basics': [
  171. (r'\s+', Whitespace),
  172. (r'/\*(?:.|\n)*?\*/', Comment),
  173. (r'\{', Punctuation, 'content'),
  174. (r'(\:{1,2})([\w-]+)', bygroups(Punctuation, Name.Decorator)),
  175. (r'(\.)([\w-]+)', bygroups(Punctuation, Name.Class)),
  176. (r'(\#)([\w-]+)', bygroups(Punctuation, Name.Namespace)),
  177. (r'(@)([\w-]+)', bygroups(Punctuation, Keyword), 'atrule'),
  178. (r'[\w-]+', Name.Tag),
  179. (r'[~^*!%&$\[\]()<>|+=@:;,./?-]', Operator),
  180. (r'"(\\\\|\\[^\\]|[^"\\])*"', String.Double),
  181. (r"'(\\\\|\\[^\\]|[^'\\])*'", String.Single),
  182. ],
  183. 'atrule': [
  184. (r'\{', Punctuation, 'atcontent'),
  185. (r';', Punctuation, '#pop'),
  186. include('basics'),
  187. ],
  188. 'atcontent': [
  189. include('basics'),
  190. (r'\}', Punctuation, '#pop:2'),
  191. ],
  192. 'content': [
  193. (r'\s+', Whitespace),
  194. (r'\}', Punctuation, '#pop'),
  195. (r';', Punctuation),
  196. (r'^@.*?$', Comment.Preproc),
  197. (words(_vendor_prefixes,), Keyword.Pseudo),
  198. (r'('+r'|'.join(_css_properties)+r')(\s*)(\:)',
  199. bygroups(Keyword, Whitespace, Punctuation), 'value-start'),
  200. (r'([-]+[a-zA-Z_][\w-]*)(\s*)(\:)', bygroups(Name.Variable, Whitespace, Punctuation),
  201. 'value-start'),
  202. (r'([a-zA-Z_][\w-]*)(\s*)(\:)', bygroups(Name, Whitespace, Punctuation),
  203. 'value-start'),
  204. (r'/\*(?:.|\n)*?\*/', Comment),
  205. ],
  206. 'value-start': [
  207. (r'\s+', Whitespace),
  208. (words(_vendor_prefixes,), Name.Builtin.Pseudo),
  209. include('urls'),
  210. (r'('+r'|'.join(_functional_notation_keyword_values)+r')(\()',
  211. bygroups(Name.Builtin, Punctuation), 'function-start'),
  212. (r'([a-zA-Z_][\w-]+)(\()',
  213. bygroups(Name.Function, Punctuation), 'function-start'),
  214. (words(_keyword_values, suffix=r'\b'), Keyword.Constant),
  215. (words(_other_keyword_values, suffix=r'\b'), Keyword.Constant),
  216. (words(_color_keywords, suffix=r'\b'), Keyword.Constant),
  217. # for transition-property etc.
  218. (words(_css_properties, suffix=r'\b'), Keyword),
  219. (r'\!important', Comment.Preproc),
  220. (r'/\*(?:.|\n)*?\*/', Comment),
  221. include('numeric-values'),
  222. (r'[~^*!%&<>|+=@:./?-]+', Operator),
  223. (r'[\[\](),]+', Punctuation),
  224. (r'"(\\\\|\\[^\\]|[^"\\])*"', String.Double),
  225. (r"'(\\\\|\\[^\\]|[^'\\])*'", String.Single),
  226. (r'[a-zA-Z_][\w-]*', Name),
  227. (r';', Punctuation, '#pop'),
  228. (r'\}', Punctuation, '#pop:2'),
  229. ],
  230. 'function-start': [
  231. (r'\s+', Whitespace),
  232. (r'[-]+([A-Za-z][\w+]*[-]*)+', Name.Variable),
  233. include('urls'),
  234. (words(_vendor_prefixes,), Keyword.Pseudo),
  235. (words(_keyword_values, suffix=r'\b'), Keyword.Constant),
  236. (words(_other_keyword_values, suffix=r'\b'), Keyword.Constant),
  237. (words(_color_keywords, suffix=r'\b'), Keyword.Constant),
  238. # function-start may be entered recursively
  239. (r'(' + r'|'.join(_functional_notation_keyword_values) + r')(\()',
  240. bygroups(Name.Builtin, Punctuation), 'function-start'),
  241. (r'([a-zA-Z_][\w-]+)(\()',
  242. bygroups(Name.Function, Punctuation), 'function-start'),
  243. (r'/\*(?:.|\n)*?\*/', Comment),
  244. include('numeric-values'),
  245. (r'[*+/-]', Operator),
  246. (r',', Punctuation),
  247. (r'"(\\\\|\\[^\\]|[^"\\])*"', String.Double),
  248. (r"'(\\\\|\\[^\\]|[^'\\])*'", String.Single),
  249. (r'[a-zA-Z_-]\w*', Name),
  250. (r'\)', Punctuation, '#pop'),
  251. ],
  252. 'urls': [
  253. (r'(url)(\()(".*?")(\))', bygroups(Name.Builtin, Punctuation,
  254. String.Double, Punctuation)),
  255. (r"(url)(\()('.*?')(\))", bygroups(Name.Builtin, Punctuation,
  256. String.Single, Punctuation)),
  257. (r'(url)(\()(.*?)(\))', bygroups(Name.Builtin, Punctuation,
  258. String.Other, Punctuation)),
  259. ],
  260. 'numeric-values': [
  261. (r'\#[a-zA-Z0-9]{1,6}', Number.Hex),
  262. (r'[+\-]?[0-9]*[.][0-9]+', Number.Float, 'numeric-end'),
  263. (r'[+\-]?[0-9]+', Number.Integer, 'numeric-end'),
  264. ],
  265. 'numeric-end': [
  266. (words(_all_units, suffix=r'\b'), Keyword.Type),
  267. (r'%', Keyword.Type),
  268. default('#pop'),
  269. ],
  270. }
  271. common_sass_tokens = {
  272. 'value': [
  273. (r'[ \t]+', Whitespace),
  274. (r'[!$][\w-]+', Name.Variable),
  275. (r'url\(', String.Other, 'string-url'),
  276. (r'[a-z_-][\w-]*(?=\()', Name.Function),
  277. (words(_css_properties + (
  278. 'above', 'absolute', 'always', 'armenian', 'aural', 'auto', 'avoid', 'baseline',
  279. 'behind', 'below', 'bidi-override', 'blink', 'block', 'bold', 'bolder', 'both',
  280. 'capitalize', 'center-left', 'center-right', 'center', 'circle',
  281. 'cjk-ideographic', 'close-quote', 'collapse', 'condensed', 'continuous',
  282. 'crosshair', 'cross', 'cursive', 'dashed', 'decimal-leading-zero',
  283. 'decimal', 'default', 'digits', 'disc', 'dotted', 'double', 'e-resize', 'embed',
  284. 'extra-condensed', 'extra-expanded', 'expanded', 'fantasy', 'far-left',
  285. 'far-right', 'faster', 'fast', 'fixed', 'georgian', 'groove', 'hebrew', 'help',
  286. 'hidden', 'hide', 'higher', 'high', 'hiragana-iroha', 'hiragana', 'icon',
  287. 'inherit', 'inline-table', 'inline', 'inset', 'inside', 'invert', 'italic',
  288. 'justify', 'katakana-iroha', 'katakana', 'landscape', 'larger', 'large',
  289. 'left-side', 'leftwards', 'level', 'lighter', 'line-through', 'list-item',
  290. 'loud', 'lower-alpha', 'lower-greek', 'lower-roman', 'lowercase', 'ltr',
  291. 'lower', 'low', 'medium', 'message-box', 'middle', 'mix', 'monospace',
  292. 'n-resize', 'narrower', 'ne-resize', 'no-close-quote', 'no-open-quote',
  293. 'no-repeat', 'none', 'normal', 'nowrap', 'nw-resize', 'oblique', 'once',
  294. 'open-quote', 'outset', 'outside', 'overline', 'pointer', 'portrait', 'px',
  295. 'relative', 'repeat-x', 'repeat-y', 'repeat', 'rgb', 'ridge', 'right-side',
  296. 'rightwards', 's-resize', 'sans-serif', 'scroll', 'se-resize',
  297. 'semi-condensed', 'semi-expanded', 'separate', 'serif', 'show', 'silent',
  298. 'slow', 'slower', 'small-caps', 'small-caption', 'smaller', 'soft', 'solid',
  299. 'spell-out', 'square', 'static', 'status-bar', 'super', 'sw-resize',
  300. 'table-caption', 'table-cell', 'table-column', 'table-column-group',
  301. 'table-footer-group', 'table-header-group', 'table-row',
  302. 'table-row-group', 'text', 'text-bottom', 'text-top', 'thick', 'thin',
  303. 'transparent', 'ultra-condensed', 'ultra-expanded', 'underline',
  304. 'upper-alpha', 'upper-latin', 'upper-roman', 'uppercase', 'url',
  305. 'visible', 'w-resize', 'wait', 'wider', 'x-fast', 'x-high', 'x-large', 'x-loud',
  306. 'x-low', 'x-small', 'x-soft', 'xx-large', 'xx-small', 'yes'), suffix=r'\b'),
  307. Name.Constant),
  308. (words(_color_keywords, suffix=r'\b'), Name.Entity),
  309. (words((
  310. 'black', 'silver', 'gray', 'white', 'maroon', 'red', 'purple', 'fuchsia', 'green',
  311. 'lime', 'olive', 'yellow', 'navy', 'blue', 'teal', 'aqua'), suffix=r'\b'),
  312. Name.Builtin),
  313. (r'\!(important|default)', Name.Exception),
  314. (r'(true|false)', Name.Pseudo),
  315. (r'(and|or|not)', Operator.Word),
  316. (r'/\*', Comment.Multiline, 'inline-comment'),
  317. (r'//[^\n]*', Comment.Single),
  318. (r'\#[a-z0-9]{1,6}', Number.Hex),
  319. (r'(-?\d+)(\%|[a-z]+)?', bygroups(Number.Integer, Keyword.Type)),
  320. (r'(-?\d*\.\d+)(\%|[a-z]+)?', bygroups(Number.Float, Keyword.Type)),
  321. (r'#\{', String.Interpol, 'interpolation'),
  322. (r'[~^*!&%<>|+=@:,./?-]+', Operator),
  323. (r'[\[\]()]+', Punctuation),
  324. (r'"', String.Double, 'string-double'),
  325. (r"'", String.Single, 'string-single'),
  326. (r'[a-z_-][\w-]*', Name),
  327. ],
  328. 'interpolation': [
  329. (r'\}', String.Interpol, '#pop'),
  330. include('value'),
  331. ],
  332. 'selector': [
  333. (r'[ \t]+', Whitespace),
  334. (r'\:', Name.Decorator, 'pseudo-class'),
  335. (r'\.', Name.Class, 'class'),
  336. (r'\#', Name.Namespace, 'id'),
  337. (r'[\w-]+', Name.Tag),
  338. (r'#\{', String.Interpol, 'interpolation'),
  339. (r'&', Keyword),
  340. (r'[~^*!&\[\]()<>|+=@:;,./?-]', Operator),
  341. (r'"', String.Double, 'string-double'),
  342. (r"'", String.Single, 'string-single'),
  343. ],
  344. 'string-double': [
  345. (r'(\\.|#(?=[^\n{])|[^\n"#])+', String.Double),
  346. (r'#\{', String.Interpol, 'interpolation'),
  347. (r'"', String.Double, '#pop'),
  348. ],
  349. 'string-single': [
  350. (r"(\\.|#(?=[^\n{])|[^\n'#])+", String.Single),
  351. (r'#\{', String.Interpol, 'interpolation'),
  352. (r"'", String.Single, '#pop'),
  353. ],
  354. 'string-url': [
  355. (r'(\\#|#(?=[^\n{])|[^\n#)])+', String.Other),
  356. (r'#\{', String.Interpol, 'interpolation'),
  357. (r'\)', String.Other, '#pop'),
  358. ],
  359. 'pseudo-class': [
  360. (r'[\w-]+', Name.Decorator),
  361. (r'#\{', String.Interpol, 'interpolation'),
  362. default('#pop'),
  363. ],
  364. 'class': [
  365. (r'[\w-]+', Name.Class),
  366. (r'#\{', String.Interpol, 'interpolation'),
  367. default('#pop'),
  368. ],
  369. 'id': [
  370. (r'[\w-]+', Name.Namespace),
  371. (r'#\{', String.Interpol, 'interpolation'),
  372. default('#pop'),
  373. ],
  374. 'for': [
  375. (r'(from|to|through)', Operator.Word),
  376. include('value'),
  377. ],
  378. }
  379. def _indentation(lexer, match, ctx):
  380. indentation = match.group(0)
  381. yield match.start(), Whitespace, indentation
  382. ctx.last_indentation = indentation
  383. ctx.pos = match.end()
  384. if hasattr(ctx, 'block_state') and ctx.block_state and \
  385. indentation.startswith(ctx.block_indentation) and \
  386. indentation != ctx.block_indentation:
  387. ctx.stack.append(ctx.block_state)
  388. else:
  389. ctx.block_state = None
  390. ctx.block_indentation = None
  391. ctx.stack.append('content')
  392. def _starts_block(token, state):
  393. def callback(lexer, match, ctx):
  394. yield match.start(), token, match.group(0)
  395. if hasattr(ctx, 'last_indentation'):
  396. ctx.block_indentation = ctx.last_indentation
  397. else:
  398. ctx.block_indentation = ''
  399. ctx.block_state = state
  400. ctx.pos = match.end()
  401. return callback
  402. class SassLexer(ExtendedRegexLexer):
  403. """
  404. For Sass stylesheets.
  405. .. versionadded:: 1.3
  406. """
  407. name = 'Sass'
  408. url = 'https://sass-lang.com/'
  409. aliases = ['sass']
  410. filenames = ['*.sass']
  411. mimetypes = ['text/x-sass']
  412. flags = re.IGNORECASE | re.MULTILINE
  413. tokens = {
  414. 'root': [
  415. (r'[ \t]*\n', Whitespace),
  416. (r'[ \t]*', _indentation),
  417. ],
  418. 'content': [
  419. (r'//[^\n]*', _starts_block(Comment.Single, 'single-comment'),
  420. 'root'),
  421. (r'/\*[^\n]*', _starts_block(Comment.Multiline, 'multi-comment'),
  422. 'root'),
  423. (r'@import', Keyword, 'import'),
  424. (r'@for', Keyword, 'for'),
  425. (r'@(debug|warn|if|while)', Keyword, 'value'),
  426. (r'(@mixin)( )([\w-]+)', bygroups(Keyword, Whitespace, Name.Function), 'value'),
  427. (r'(@include)( )([\w-]+)', bygroups(Keyword, Whitespace, Name.Decorator), 'value'),
  428. (r'@extend', Keyword, 'selector'),
  429. (r'@[\w-]+', Keyword, 'selector'),
  430. (r'=[\w-]+', Name.Function, 'value'),
  431. (r'\+[\w-]+', Name.Decorator, 'value'),
  432. (r'([!$][\w-]\w*)([ \t]*(?:(?:\|\|)?=|:))',
  433. bygroups(Name.Variable, Operator), 'value'),
  434. (r':', Name.Attribute, 'old-style-attr'),
  435. (r'(?=.+?[=:]([^a-z]|$))', Name.Attribute, 'new-style-attr'),
  436. default('selector'),
  437. ],
  438. 'single-comment': [
  439. (r'.+', Comment.Single),
  440. (r'\n', Whitespace, 'root'),
  441. ],
  442. 'multi-comment': [
  443. (r'.+', Comment.Multiline),
  444. (r'\n', Whitespace, 'root'),
  445. ],
  446. 'import': [
  447. (r'[ \t]+', Whitespace),
  448. (r'\S+', String),
  449. (r'\n', Whitespace, 'root'),
  450. ],
  451. 'old-style-attr': [
  452. (r'[^\s:="\[]+', Name.Attribute),
  453. (r'#\{', String.Interpol, 'interpolation'),
  454. (r'([ \t]*)(=)', bygroups(Whitespace, Operator), 'value'),
  455. default('value'),
  456. ],
  457. 'new-style-attr': [
  458. (r'[^\s:="\[]+', Name.Attribute),
  459. (r'#\{', String.Interpol, 'interpolation'),
  460. (r'([ \t]*)([=:])', bygroups(Whitespace, Operator), 'value'),
  461. ],
  462. 'inline-comment': [
  463. (r"(\\#|#(?=[^\n{])|\*(?=[^\n/])|[^\n#*])+", Comment.Multiline),
  464. (r'#\{', String.Interpol, 'interpolation'),
  465. (r"\*/", Comment, '#pop'),
  466. ],
  467. }
  468. for group, common in common_sass_tokens.items():
  469. tokens[group] = copy.copy(common)
  470. tokens['value'].append((r'\n', Whitespace, 'root'))
  471. tokens['selector'].append((r'\n', Whitespace, 'root'))
  472. class ScssLexer(RegexLexer):
  473. """
  474. For SCSS stylesheets.
  475. """
  476. name = 'SCSS'
  477. url = 'https://sass-lang.com/'
  478. aliases = ['scss']
  479. filenames = ['*.scss']
  480. mimetypes = ['text/x-scss']
  481. flags = re.IGNORECASE | re.DOTALL
  482. tokens = {
  483. 'root': [
  484. (r'\s+', Whitespace),
  485. (r'//.*?\n', Comment.Single),
  486. (r'/\*.*?\*/', Comment.Multiline),
  487. (r'@import', Keyword, 'value'),
  488. (r'@for', Keyword, 'for'),
  489. (r'@(debug|warn|if|while)', Keyword, 'value'),
  490. (r'(@mixin)( [\w-]+)', bygroups(Keyword, Name.Function), 'value'),
  491. (r'(@include)( [\w-]+)', bygroups(Keyword, Name.Decorator), 'value'),
  492. (r'@extend', Keyword, 'selector'),
  493. (r'(@media)(\s+)', bygroups(Keyword, Whitespace), 'value'),
  494. (r'@[\w-]+', Keyword, 'selector'),
  495. (r'(\$[\w-]*\w)([ \t]*:)', bygroups(Name.Variable, Operator), 'value'),
  496. # TODO: broken, and prone to infinite loops.
  497. # (r'(?=[^;{}][;}])', Name.Attribute, 'attr'),
  498. # (r'(?=[^;{}:]+:[^a-z])', Name.Attribute, 'attr'),
  499. default('selector'),
  500. ],
  501. 'attr': [
  502. (r'[^\s:="\[]+', Name.Attribute),
  503. (r'#\{', String.Interpol, 'interpolation'),
  504. (r'[ \t]*:', Operator, 'value'),
  505. default('#pop'),
  506. ],
  507. 'inline-comment': [
  508. (r"(\\#|#(?=[^{])|\*(?=[^/])|[^#*])+", Comment.Multiline),
  509. (r'#\{', String.Interpol, 'interpolation'),
  510. (r"\*/", Comment, '#pop'),
  511. ],
  512. }
  513. for group, common in common_sass_tokens.items():
  514. tokens[group] = copy.copy(common)
  515. tokens['value'].extend([(r'\n', Whitespace), (r'[;{}]', Punctuation, '#pop')])
  516. tokens['selector'].extend([(r'\n', Whitespace), (r'[;{}]', Punctuation, '#pop')])
  517. class LessCssLexer(CssLexer):
  518. """
  519. For LESS styleshets.
  520. .. versionadded:: 2.1
  521. """
  522. name = 'LessCss'
  523. url = 'http://lesscss.org/'
  524. aliases = ['less']
  525. filenames = ['*.less']
  526. mimetypes = ['text/x-less-css']
  527. tokens = {
  528. 'root': [
  529. (r'@\w+', Name.Variable),
  530. inherit,
  531. ],
  532. 'content': [
  533. (r'\{', Punctuation, '#push'),
  534. (r'//.*\n', Comment.Single),
  535. inherit,
  536. ],
  537. }