UNPKG

3.25 kBJavaScriptView Raw
1/*
2Language: Nginx config
3Author: Peter Leonov <gojpeg@yandex.ru>
4Contributors: Ivan Sagalaev <maniac@softwaremaniacs.org>
5Category: config, web
6Website: https://www.nginx.com
7*/
8
9/** @type LanguageFn */
10function nginx(hljs) {
11 const regex = hljs.regex;
12 const VAR = {
13 className: 'variable',
14 variants: [
15 {
16 begin: /\$\d+/
17 },
18 {
19 begin: /\$\{\w+\}/
20 },
21 {
22 begin: regex.concat(/[$@]/, hljs.UNDERSCORE_IDENT_RE)
23 }
24 ]
25 };
26 const LITERALS = [
27 "on",
28 "off",
29 "yes",
30 "no",
31 "true",
32 "false",
33 "none",
34 "blocked",
35 "debug",
36 "info",
37 "notice",
38 "warn",
39 "error",
40 "crit",
41 "select",
42 "break",
43 "last",
44 "permanent",
45 "redirect",
46 "kqueue",
47 "rtsig",
48 "epoll",
49 "poll",
50 "/dev/poll"
51 ];
52 const DEFAULT = {
53 endsWithParent: true,
54 keywords: {
55 $pattern: /[a-z_]{2,}|\/dev\/poll/,
56 literal: LITERALS
57 },
58 relevance: 0,
59 illegal: '=>',
60 contains: [
61 hljs.HASH_COMMENT_MODE,
62 {
63 className: 'string',
64 contains: [
65 hljs.BACKSLASH_ESCAPE,
66 VAR
67 ],
68 variants: [
69 {
70 begin: /"/,
71 end: /"/
72 },
73 {
74 begin: /'/,
75 end: /'/
76 }
77 ]
78 },
79 // this swallows entire URLs to avoid detecting numbers within
80 {
81 begin: '([a-z]+):/',
82 end: '\\s',
83 endsWithParent: true,
84 excludeEnd: true,
85 contains: [ VAR ]
86 },
87 {
88 className: 'regexp',
89 contains: [
90 hljs.BACKSLASH_ESCAPE,
91 VAR
92 ],
93 variants: [
94 {
95 begin: "\\s\\^",
96 end: "\\s|\\{|;",
97 returnEnd: true
98 },
99 // regexp locations (~, ~*)
100 {
101 begin: "~\\*?\\s+",
102 end: "\\s|\\{|;",
103 returnEnd: true
104 },
105 // *.example.com
106 {
107 begin: "\\*(\\.[a-z\\-]+)+"
108 },
109 // sub.example.*
110 {
111 begin: "([a-z\\-]+\\.)+\\*"
112 }
113 ]
114 },
115 // IP
116 {
117 className: 'number',
118 begin: '\\b\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}(:\\d{1,5})?\\b'
119 },
120 // units
121 {
122 className: 'number',
123 begin: '\\b\\d+[kKmMgGdshdwy]?\\b',
124 relevance: 0
125 },
126 VAR
127 ]
128 };
129
130 return {
131 name: 'Nginx config',
132 aliases: [ 'nginxconf' ],
133 contains: [
134 hljs.HASH_COMMENT_MODE,
135 {
136 beginKeywords: "upstream location",
137 end: /;|\{/,
138 contains: DEFAULT.contains,
139 keywords: {
140 section: "upstream location"
141 }
142 },
143 {
144 className: 'section',
145 begin: regex.concat(hljs.UNDERSCORE_IDENT_RE + regex.lookahead(/\s+\{/)),
146 relevance: 0
147 },
148 {
149 begin: regex.lookahead(hljs.UNDERSCORE_IDENT_RE + '\\s'),
150 end: ';|\\{',
151 contains: [
152 {
153 className: 'attribute',
154 begin: hljs.UNDERSCORE_IDENT_RE,
155 starts: DEFAULT
156 }
157 ],
158 relevance: 0
159 }
160 ],
161 illegal: '[^\\s\\}\\{]'
162 };
163}
164
165export { nginx as default };