Skip to content

Commit 3e3ca19

Browse files
committed
2 parents d34ad69 + c8ed291 commit 3e3ca19

File tree

1 file changed

+393
-0
lines changed

1 file changed

+393
-0
lines changed

cshtml.js

Lines changed: 393 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,393 @@
1+
/*
2+
* Language: CSHTML
3+
* Requires: xml.js, cs.js
4+
* Author: Roman Resh <[email protected]>
5+
*/
6+
7+
var module = module ? module : {}; // shim for browser use
8+
9+
function hljsDefineRazorCshtml(hljs) {
10+
var SPECIAL_SYMBOL_CLASSNAME = "built_in";
11+
12+
var BLOCK_TEXT = {
13+
begin: "[@]{0,1}<text>",
14+
returnBegin: true,
15+
end: "</text>",
16+
returnEnd: true,
17+
subLanguage: "cshtml",
18+
contains: [
19+
{
20+
begin: "[@]{0,1}<text>",
21+
className: SPECIAL_SYMBOL_CLASSNAME
22+
},
23+
{
24+
begin: "</text>",
25+
className: SPECIAL_SYMBOL_CLASSNAME,
26+
endsParent: true,
27+
}
28+
]
29+
};
30+
31+
var DIRECTIVES = {
32+
begin: "^@(model|using|inherits|inject)[^\\r\\n{\\(]*$",
33+
end: "$",
34+
className: SPECIAL_SYMBOL_CLASSNAME,
35+
returnBegin: true,
36+
returnEnd: true,
37+
contains: [
38+
{
39+
begin: "@(model|using|inherits|inject)",
40+
className: SPECIAL_SYMBOL_CLASSNAME,
41+
},
42+
{
43+
variants: [
44+
{
45+
begin: "\\s+",
46+
end: "$",
47+
},
48+
{
49+
begin: "$"
50+
},
51+
],
52+
className: "type",
53+
endsParent: true
54+
}
55+
]
56+
};
57+
58+
var EXCEPTIONS = {
59+
variants: [
60+
{ begin: "@@" },
61+
{ begin: "[a-zA-Z]+@" },
62+
],
63+
skip: true
64+
}
65+
66+
var ONE_LINE_EXPRESSION = {
67+
begin: "@[a-zA-Z]+",
68+
returnBegin: true,
69+
end: "(\\r|\\n|<|\\s)",
70+
subLanguage: 'cs',
71+
contains: [
72+
{
73+
begin: '@',
74+
className: SPECIAL_SYMBOL_CLASSNAME
75+
},
76+
{
77+
begin: '".*(?!$)"',
78+
skip: true
79+
},
80+
{
81+
begin: '"',
82+
endsParent: true
83+
}
84+
],
85+
returnEnd: true
86+
};
87+
88+
var ONE_LINE_AWAIT = {
89+
begin: "@await ",
90+
returnBegin: true,
91+
subLanguage: 'cs',
92+
end: "(\\r|\\n|<|\\s)",
93+
contains: [
94+
{
95+
begin: "@await ",
96+
className: SPECIAL_SYMBOL_CLASSNAME
97+
},
98+
{
99+
begin: "[<\\r\\n]",
100+
endsParent: true
101+
}
102+
]
103+
};
104+
105+
var BLOCK_ROUND_BRACKET = {
106+
begin: "@\\(",
107+
end: "\\)",
108+
returnBegin: true,
109+
returnEnd: true,
110+
subLanguage: 'cs',
111+
contains: [
112+
{
113+
begin: "@\\(",
114+
className: SPECIAL_SYMBOL_CLASSNAME
115+
},
116+
{
117+
begin: "\\(",
118+
end: "\\)",
119+
subLanguage: 'cs',
120+
contains: [hljs.QUOTE_STRING_MODE, BLOCK_TEXT, 'self']
121+
},
122+
BLOCK_TEXT,
123+
{
124+
begin: "\\)",
125+
className: SPECIAL_SYMBOL_CLASSNAME,
126+
endsParent: true
127+
}
128+
]
129+
};
130+
131+
var BLOCK_FIGURE_BRACKET = {
132+
begin: "@\\{",
133+
returnBegin: true,
134+
returnEnd: true,
135+
end: "\\}",
136+
subLanguage: 'cs',
137+
contains: [
138+
{
139+
begin: "@\\{",
140+
className: SPECIAL_SYMBOL_CLASSNAME
141+
},
142+
{
143+
begin: "{",
144+
end: "}",
145+
contains: [hljs.QUOTE_STRING_MODE, BLOCK_TEXT, 'self']
146+
},
147+
BLOCK_TEXT,
148+
{
149+
begin: "\\}",
150+
className: SPECIAL_SYMBOL_CLASSNAME,
151+
endsParent: true
152+
}
153+
],
154+
};
155+
156+
157+
158+
var BUILT_IN_CODE_BLOCKS_VARIANTS = [
159+
{
160+
begin: "@for[\\s]*\\([^{]+[\\s]*{",
161+
end: "}"
162+
},
163+
{
164+
begin: "@if[\\s]*\\([^{]+[\\s]*{",
165+
end: "}"
166+
},
167+
{
168+
begin: "@switch[\\s]*\\([^{]+[\\s]*{",
169+
end: "}"
170+
},
171+
{
172+
begin: "@while[\\s]*\\([^{]+[\\s]*{",
173+
end: "}"
174+
},
175+
{
176+
begin: "@using[\\s]*\\([^{]+[\\s]*{",
177+
end: "}"
178+
},
179+
{
180+
begin: "@lock[\\s]*\\([^{]+[\\s]*{",
181+
end: "}"
182+
},
183+
{
184+
begin: "@foreach[\\s]*\\([^{]+[\\s]*{",
185+
end: "}"
186+
},
187+
];
188+
var BUILT_IN_CODE_BLOCKS = {
189+
variants: BUILT_IN_CODE_BLOCKS_VARIANTS,
190+
returnBegin: true,
191+
returnEnd: true,
192+
subLanguage: "cshtml",
193+
contains: [
194+
{
195+
variants: BUILT_IN_CODE_BLOCKS_VARIANTS.map(function(v) {
196+
return {
197+
begin: v.begin
198+
};
199+
}),
200+
returnBegin: true,
201+
contains: [
202+
{ begin: "@", className: SPECIAL_SYMBOL_CLASSNAME },
203+
{
204+
variants: BUILT_IN_CODE_BLOCKS_VARIANTS.map(function(v) {
205+
return {
206+
begin: v.begin.substr(1, v.begin.length - 2)
207+
}; }),
208+
subLanguage: "cs" },
209+
{ begin: "{", className: SPECIAL_SYMBOL_CLASSNAME }
210+
]
211+
},
212+
{
213+
begin: "{",
214+
end: "}",
215+
contains: [hljs.QUOTE_STRING_MODE, 'self']
216+
},
217+
BLOCK_TEXT,
218+
219+
{
220+
variants: [
221+
{
222+
begin: "}[\\s]*else\\sif[\\s]*\\([^\\)]+\\)[\\s]*{"
223+
},
224+
{
225+
begin: "}[\\s]*else[\\s]*{"
226+
}
227+
],
228+
returnBegin: true,
229+
contains: [
230+
{
231+
begin: "}",
232+
className: SPECIAL_SYMBOL_CLASSNAME
233+
},
234+
{
235+
variants: [
236+
{
237+
begin: "[\\s]*else\\sif[\\s]*\\([^\\)]+\\)[\\s]*",
238+
},
239+
{
240+
begin: "[\\s]*else[\\s]*",
241+
},
242+
],
243+
subLanguage: "cs"
244+
},
245+
{
246+
begin: "{",
247+
className: SPECIAL_SYMBOL_CLASSNAME
248+
}
249+
]
250+
},
251+
252+
{
253+
begin: "}",
254+
className: SPECIAL_SYMBOL_CLASSNAME,
255+
endsParent: true
256+
},
257+
]
258+
};
259+
260+
261+
var BLOCK_TRY = {
262+
begin: "@try[\\s]*{",
263+
end: "}",
264+
returnBegin: true,
265+
returnEnd: true,
266+
subLanguage: "cs",
267+
contains: [
268+
{
269+
begin: "@",
270+
className: SPECIAL_SYMBOL_CLASSNAME
271+
},
272+
{
273+
begin: "try[\\s]*{",
274+
subLanguage: "cs"
275+
},
276+
{
277+
begin: "{",
278+
end: "}",
279+
contains: [hljs.QUOTE_STRING_MODE, 'self']
280+
},
281+
{
282+
variants: [
283+
{
284+
begin: "}[\\s]*catch[\\s]*\\([^\\)]+\\)[\\s]*{"
285+
},
286+
{
287+
begin: "}[\\s]*finally[\\s]*{"
288+
}
289+
],
290+
returnBegin: true,
291+
contains: [
292+
{
293+
begin: "}",
294+
className: SPECIAL_SYMBOL_CLASSNAME
295+
},
296+
{
297+
variants: [
298+
{
299+
begin: "[\\s]*catch[\\s]*\\([^\\)]+\\)[\\s]*",
300+
},
301+
{
302+
begin: "[\\s]*finally[\\s]*",
303+
},
304+
],
305+
subLanguage: "cs"
306+
},
307+
{
308+
begin: "{",
309+
className: SPECIAL_SYMBOL_CLASSNAME
310+
}
311+
]
312+
},
313+
{
314+
begin: "}",
315+
className: SPECIAL_SYMBOL_CLASSNAME,
316+
endsParent: true
317+
},
318+
]
319+
};
320+
321+
322+
var BLOCK_FUNCTIONS = {
323+
begin: "@functions[\\s]*{",
324+
end: "}",
325+
returnBegin: true,
326+
returnEnd: true,
327+
subLanguage: "cs",
328+
contains: [
329+
{
330+
begin: "@functions[\\s]*{",
331+
className: SPECIAL_SYMBOL_CLASSNAME
332+
},
333+
{
334+
begin: "{",
335+
end: "}",
336+
contains: [hljs.QUOTE_STRING_MODE, 'self']
337+
},
338+
{
339+
begin: "}",
340+
className: SPECIAL_SYMBOL_CLASSNAME,
341+
endsParent: true
342+
}
343+
]
344+
};
345+
346+
var BLOCK_SECTION = {
347+
begin: "@section[\\s]+[a-zA-Z0-9]+[\\s]*{",
348+
returnBegin: true,
349+
returnEnd: true,
350+
subLanguage: "cshtml",
351+
end: "}",
352+
contains: [
353+
{
354+
begin: "@section[\\s]+[a-zA-Z0-9]+[\\s]*{",
355+
className: SPECIAL_SYMBOL_CLASSNAME
356+
},
357+
{
358+
begin: "{",
359+
end: "}",
360+
contains: [hljs.QUOTE_STRING_MODE, 'self']
361+
},
362+
{
363+
begin: "}",
364+
className: SPECIAL_SYMBOL_CLASSNAME,
365+
endsParent: true
366+
}
367+
]
368+
}
369+
370+
return {
371+
subLanguage: 'xml',
372+
contains: [
373+
hljs.COMMENT("@\\*", "\\*@"),
374+
EXCEPTIONS,
375+
DIRECTIVES,
376+
BLOCK_FUNCTIONS,
377+
BLOCK_SECTION,
378+
BLOCK_TRY,
379+
BUILT_IN_CODE_BLOCKS,
380+
ONE_LINE_AWAIT,
381+
ONE_LINE_EXPRESSION,
382+
BLOCK_ROUND_BRACKET,
383+
BLOCK_FIGURE_BRACKET,
384+
BLOCK_TEXT
385+
]
386+
};
387+
}
388+
389+
module.exports = function(hljs) {
390+
hljs.registerLanguage('razor-cshtml', hljsDefineRazorCshtml);
391+
};
392+
393+
module.exports.definer = hljsDefineRazorCshtml;

0 commit comments

Comments
 (0)