prism-docker.js 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. (function (Prism) {
  2. // Many of the following regexes will contain negated lookaheads like `[ \t]+(?![ \t])`. This is a trick to ensure
  3. // that quantifiers behave *atomically*. Atomic quantifiers are necessary to prevent exponential backtracking.
  4. var spaceAfterBackSlash = /\\[\r\n](?:\s|\\[\r\n]|#.*(?!.))*(?![\s#]|\\[\r\n])/.source;
  5. // At least one space, comment, or line break
  6. var space = /(?:[ \t]+(?![ \t])(?:<SP_BS>)?|<SP_BS>)/.source
  7. .replace(/<SP_BS>/g, function () { return spaceAfterBackSlash; });
  8. var string = /"(?:[^"\\\r\n]|\\(?:\r\n|[\s\S]))*"|'(?:[^'\\\r\n]|\\(?:\r\n|[\s\S]))*'/.source;
  9. var option = /--[\w-]+=(?:<STR>|(?!["'])(?:[^\s\\]|\\.)+)/.source.replace(/<STR>/g, function () { return string; });
  10. var stringRule = {
  11. pattern: RegExp(string),
  12. greedy: true
  13. };
  14. var commentRule = {
  15. pattern: /(^[ \t]*)#.*/m,
  16. lookbehind: true,
  17. greedy: true
  18. };
  19. /**
  20. * @param {string} source
  21. * @param {string} flags
  22. * @returns {RegExp}
  23. */
  24. function re(source, flags) {
  25. source = source
  26. .replace(/<OPT>/g, function () { return option; })
  27. .replace(/<SP>/g, function () { return space; });
  28. return RegExp(source, flags);
  29. }
  30. Prism.languages.docker = {
  31. 'instruction': {
  32. pattern: /(^[ \t]*)(?:ADD|ARG|CMD|COPY|ENTRYPOINT|ENV|EXPOSE|FROM|HEALTHCHECK|LABEL|MAINTAINER|ONBUILD|RUN|SHELL|STOPSIGNAL|USER|VOLUME|WORKDIR)(?=\s)(?:\\.|[^\r\n\\])*(?:\\$(?:\s|#.*$)*(?![\s#])(?:\\.|[^\r\n\\])*)*/im,
  33. lookbehind: true,
  34. greedy: true,
  35. inside: {
  36. 'options': {
  37. pattern: re(/(^(?:ONBUILD<SP>)?\w+<SP>)<OPT>(?:<SP><OPT>)*/.source, 'i'),
  38. lookbehind: true,
  39. greedy: true,
  40. inside: {
  41. 'property': {
  42. pattern: /(^|\s)--[\w-]+/,
  43. lookbehind: true
  44. },
  45. 'string': [
  46. stringRule,
  47. {
  48. pattern: /(=)(?!["'])(?:[^\s\\]|\\.)+/,
  49. lookbehind: true
  50. }
  51. ],
  52. 'operator': /\\$/m,
  53. 'punctuation': /=/
  54. }
  55. },
  56. 'keyword': [
  57. {
  58. // https://docs.docker.com/engine/reference/builder/#healthcheck
  59. pattern: re(/(^(?:ONBUILD<SP>)?HEALTHCHECK<SP>(?:<OPT><SP>)*)(?:CMD|NONE)\b/.source, 'i'),
  60. lookbehind: true,
  61. greedy: true
  62. },
  63. {
  64. // https://docs.docker.com/engine/reference/builder/#from
  65. pattern: re(/(^(?:ONBUILD<SP>)?FROM<SP>(?:<OPT><SP>)*(?!--)[^ \t\\]+<SP>)AS/.source, 'i'),
  66. lookbehind: true,
  67. greedy: true
  68. },
  69. {
  70. // https://docs.docker.com/engine/reference/builder/#onbuild
  71. pattern: re(/(^ONBUILD<SP>)\w+/.source, 'i'),
  72. lookbehind: true,
  73. greedy: true
  74. },
  75. {
  76. pattern: /^\w+/,
  77. greedy: true
  78. }
  79. ],
  80. 'comment': commentRule,
  81. 'string': stringRule,
  82. 'variable': /\$(?:\w+|\{[^{}"'\\]*\})/,
  83. 'operator': /\\$/m
  84. }
  85. },
  86. 'comment': commentRule
  87. };
  88. Prism.languages.dockerfile = Prism.languages.docker;
  89. }(Prism));