forked from highlightjs/highlight.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathless.js
234 lines (213 loc) · 6.22 KB
/
less.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/*
Language: Less
Description: It's CSS, with just a little more.
Author: Max Mikhailov <[email protected]>
Website: http://lesscss.org
Category: common, css, web
*/
import * as css from "./lib/css-shared.js";
/** @type LanguageFn */
export default function(hljs) {
const modes = css.MODES(hljs);
const PSEUDO_SELECTORS = css.PSEUDO_SELECTORS;
const AT_MODIFIERS = "and or not only";
const IDENT_RE = '[\\w-]+'; // yes, Less identifiers may begin with a digit
const INTERP_IDENT_RE = '(' + IDENT_RE + '|@\\{' + IDENT_RE + '\\})';
/* Generic Modes */
const RULES = []; const VALUE_MODES = []; // forward def. for recursive modes
const STRING_MODE = function(c) {
return {
// Less strings are not multiline (also include '~' for more consistent coloring of "escaped" strings)
className: 'string',
begin: '~?' + c + '.*?' + c
};
};
const IDENT_MODE = function(name, begin, relevance) {
return {
className: name,
begin: begin,
relevance: relevance
};
};
const AT_KEYWORDS = {
$pattern: /[a-z-]+/,
keyword: AT_MODIFIERS,
attribute: css.MEDIA_FEATURES.join(" ")
};
const PARENS_MODE = {
// used only to properly balance nested parens inside mixin call, def. arg list
begin: '\\(',
end: '\\)',
contains: VALUE_MODES,
keywords: AT_KEYWORDS,
relevance: 0
};
// generic Less highlighter (used almost everywhere except selectors):
VALUE_MODES.push(
hljs.C_LINE_COMMENT_MODE,
hljs.C_BLOCK_COMMENT_MODE,
STRING_MODE("'"),
STRING_MODE('"'),
modes.CSS_NUMBER_MODE, // fixme: it does not include dot for numbers like .5em :(
{
begin: '(url|data-uri)\\(',
starts: {
className: 'string',
end: '[\\)\\n]',
excludeEnd: true
}
},
modes.HEXCOLOR,
PARENS_MODE,
IDENT_MODE('variable', '@@?' + IDENT_RE, 10),
IDENT_MODE('variable', '@\\{' + IDENT_RE + '\\}'),
IDENT_MODE('built_in', '~?`[^`]*?`'), // inline javascript (or whatever host language) *multiline* string
{ // @media features (it’s here to not duplicate things in AT_RULE_MODE with extra PARENS_MODE overriding):
className: 'attribute',
begin: IDENT_RE + '\\s*:',
end: ':',
returnBegin: true,
excludeEnd: true
},
modes.IMPORTANT,
{ beginKeywords: 'and not' },
modes.FUNCTION_DISPATCH
);
const VALUE_WITH_RULESETS = VALUE_MODES.concat({
begin: /\{/,
end: /\}/,
contains: RULES
});
const MIXIN_GUARD_MODE = {
beginKeywords: 'when',
endsWithParent: true,
contains: [ { beginKeywords: 'and not' } ].concat(VALUE_MODES) // using this form to override VALUE’s 'function' match
};
/* Rule-Level Modes */
const RULE_MODE = {
begin: INTERP_IDENT_RE + '\\s*:',
returnBegin: true,
end: /[;}]/,
relevance: 0,
contains: [
{ begin: /-(webkit|moz|ms|o)-/ },
modes.CSS_VARIABLE,
{
className: 'attribute',
begin: '\\b(' + css.ATTRIBUTES.join('|') + ')\\b',
end: /(?=:)/,
starts: {
endsWithParent: true,
illegal: '[<=$]',
relevance: 0,
contains: VALUE_MODES
}
}
]
};
const AT_RULE_MODE = {
className: 'keyword',
begin: '@(import|media|charset|font-face|(-[a-z]+-)?keyframes|supports|document|namespace|page|viewport|host)\\b',
starts: {
end: '[;{}]',
keywords: AT_KEYWORDS,
returnEnd: true,
contains: VALUE_MODES,
relevance: 0
}
};
// variable definitions and calls
const VAR_RULE_MODE = {
className: 'variable',
variants: [
// using more strict pattern for higher relevance to increase chances of Less detection.
// this is *the only* Less specific statement used in most of the sources, so...
// (we’ll still often loose to the css-parser unless there's '//' comment,
// simply because 1 variable just can't beat 99 properties :)
{
begin: '@' + IDENT_RE + '\\s*:',
relevance: 15
},
{ begin: '@' + IDENT_RE }
],
starts: {
end: '[;}]',
returnEnd: true,
contains: VALUE_WITH_RULESETS
}
};
const SELECTOR_MODE = {
// first parse unambiguous selectors (i.e. those not starting with tag)
// then fall into the scary lookahead-discriminator variant.
// this mode also handles mixin definitions and calls
variants: [
{
begin: '[\\.#:&\\[>]',
end: '[;{}]' // mixin calls end with ';'
},
{
begin: INTERP_IDENT_RE,
end: /\{/
}
],
returnBegin: true,
returnEnd: true,
illegal: '[<=\'$"]',
relevance: 0,
contains: [
hljs.C_LINE_COMMENT_MODE,
hljs.C_BLOCK_COMMENT_MODE,
MIXIN_GUARD_MODE,
IDENT_MODE('keyword', 'all\\b'),
IDENT_MODE('variable', '@\\{' + IDENT_RE + '\\}'), // otherwise it’s identified as tag
{
begin: '\\b(' + css.TAGS.join('|') + ')\\b',
className: 'selector-tag'
},
modes.CSS_NUMBER_MODE,
IDENT_MODE('selector-tag', INTERP_IDENT_RE, 0),
IDENT_MODE('selector-id', '#' + INTERP_IDENT_RE),
IDENT_MODE('selector-class', '\\.' + INTERP_IDENT_RE, 0),
IDENT_MODE('selector-tag', '&', 0),
modes.ATTRIBUTE_SELECTOR_MODE,
{
className: 'selector-pseudo',
begin: ':(' + css.PSEUDO_CLASSES.join('|') + ')'
},
{
className: 'selector-pseudo',
begin: ':(:)?(' + css.PSEUDO_ELEMENTS.join('|') + ')'
},
{
begin: /\(/,
end: /\)/,
relevance: 0,
contains: VALUE_WITH_RULESETS
}, // argument list of parametric mixins
{ begin: '!important' }, // eat !important after mixin call or it will be colored as tag
modes.FUNCTION_DISPATCH
]
};
const PSEUDO_SELECTOR_MODE = {
begin: IDENT_RE + ':(:)?' + `(${PSEUDO_SELECTORS.join('|')})`,
returnBegin: true,
contains: [ SELECTOR_MODE ]
};
RULES.push(
hljs.C_LINE_COMMENT_MODE,
hljs.C_BLOCK_COMMENT_MODE,
AT_RULE_MODE,
VAR_RULE_MODE,
PSEUDO_SELECTOR_MODE,
RULE_MODE,
SELECTOR_MODE,
MIXIN_GUARD_MODE,
modes.FUNCTION_DISPATCH
);
return {
name: 'Less',
case_insensitive: true,
illegal: '[=>\'/<($"]',
contains: RULES
};
}