http.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. Language: HTTP
  3. Description: HTTP request and response headers with automatic body highlighting
  4. Author: Ivan Sagalaev <maniac@softwaremaniacs.org>
  5. Category: protocols, web
  6. Website: https://developer.mozilla.org/en-US/docs/Web/HTTP/Overview
  7. */
  8. function http(hljs) {
  9. const regex = hljs.regex;
  10. const VERSION = 'HTTP/([32]|1\\.[01])';
  11. const HEADER_NAME = /[A-Za-z][A-Za-z0-9-]*/;
  12. const HEADER = {
  13. className: 'attribute',
  14. begin: regex.concat('^', HEADER_NAME, '(?=\\:\\s)'),
  15. starts: { contains: [
  16. {
  17. className: "punctuation",
  18. begin: /: /,
  19. relevance: 0,
  20. starts: {
  21. end: '$',
  22. relevance: 0
  23. }
  24. }
  25. ] }
  26. };
  27. const HEADERS_AND_BODY = [
  28. HEADER,
  29. {
  30. begin: '\\n\\n',
  31. starts: {
  32. subLanguage: [],
  33. endsWithParent: true
  34. }
  35. }
  36. ];
  37. return {
  38. name: 'HTTP',
  39. aliases: [ 'https' ],
  40. illegal: /\S/,
  41. contains: [
  42. // response
  43. {
  44. begin: '^(?=' + VERSION + " \\d{3})",
  45. end: /$/,
  46. contains: [
  47. {
  48. className: "meta",
  49. begin: VERSION
  50. },
  51. {
  52. className: 'number',
  53. begin: '\\b\\d{3}\\b'
  54. }
  55. ],
  56. starts: {
  57. end: /\b\B/,
  58. illegal: /\S/,
  59. contains: HEADERS_AND_BODY
  60. }
  61. },
  62. // request
  63. {
  64. begin: '(?=^[A-Z]+ (.*?) ' + VERSION + '$)',
  65. end: /$/,
  66. contains: [
  67. {
  68. className: 'string',
  69. begin: ' ',
  70. end: ' ',
  71. excludeBegin: true,
  72. excludeEnd: true
  73. },
  74. {
  75. className: "meta",
  76. begin: VERSION
  77. },
  78. {
  79. className: 'keyword',
  80. begin: '[A-Z]+'
  81. }
  82. ],
  83. starts: {
  84. end: /\b\B/,
  85. illegal: /\S/,
  86. contains: HEADERS_AND_BODY
  87. }
  88. },
  89. // to allow headers to work even without a preamble
  90. hljs.inherit(HEADER, { relevance: 0 })
  91. ]
  92. };
  93. }
  94. export { http as default };