prism-http.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. (function (Prism) {
  2. Prism.languages.http = {
  3. 'request-line': {
  4. pattern: /^(?:POST|GET|PUT|DELETE|OPTIONS|PATCH|TRACE|CONNECT)\s(?:https?:\/\/|\/)\S+\sHTTP\/[0-9.]+/m,
  5. inside: {
  6. // HTTP Verb
  7. 'property': /^(?:POST|GET|PUT|DELETE|OPTIONS|PATCH|TRACE|CONNECT)\b/,
  8. // Path or query argument
  9. 'attr-name': /:\w+/
  10. }
  11. },
  12. 'response-status': {
  13. pattern: /^HTTP\/1.[01] \d+.*/m,
  14. inside: {
  15. // Status, e.g. 200 OK
  16. 'property': {
  17. pattern: /(^HTTP\/1.[01] )\d+.*/i,
  18. lookbehind: true
  19. }
  20. }
  21. },
  22. // HTTP header name
  23. 'header-name': {
  24. pattern: /^[\w-]+:(?=.)/m,
  25. alias: 'keyword'
  26. }
  27. };
  28. // Create a mapping of Content-Type headers to language definitions
  29. var langs = Prism.languages;
  30. var httpLanguages = {
  31. 'application/javascript': langs.javascript,
  32. 'application/json': langs.json || langs.javascript,
  33. 'application/xml': langs.xml,
  34. 'text/xml': langs.xml,
  35. 'text/html': langs.html,
  36. 'text/css': langs.css
  37. };
  38. // Declare which types can also be suffixes
  39. var suffixTypes = {
  40. 'application/json': true,
  41. 'application/xml': true
  42. };
  43. /**
  44. * Returns a pattern for the given content type which matches it and any type which has it as a suffix.
  45. *
  46. * @param {string} contentType
  47. * @returns {string}
  48. */
  49. function getSuffixPattern(contentType) {
  50. var suffix = contentType.replace(/^[a-z]+\//, '');
  51. var suffixPattern = '\\w+/(?:[\\w.-]+\\+)+' + suffix + '(?![+\\w.-])';
  52. return '(?:' + contentType + '|' + suffixPattern + ')';
  53. }
  54. // Insert each content type parser that has its associated language
  55. // currently loaded.
  56. var options;
  57. for (var contentType in httpLanguages) {
  58. if (httpLanguages[contentType]) {
  59. options = options || {};
  60. var pattern = suffixTypes[contentType] ? getSuffixPattern(contentType) : contentType;
  61. options[contentType.replace(/\//g, '-')] = {
  62. pattern: RegExp('(content-type:\\s*' + pattern + '[\\s\\S]*?)(?:\\r?\\n|\\r){2}[\\s\\S]*', 'i'),
  63. lookbehind: true,
  64. inside: httpLanguages[contentType]
  65. };
  66. }
  67. }
  68. if (options) {
  69. Prism.languages.insertBefore('http', 'header-name', options);
  70. }
  71. }(Prism));