css.py 31 KB

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