nginx.js 3.1 KB

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