From 2f57ef977b4b6de941c5d49a04f4db3300ebb2ef Mon Sep 17 00:00:00 2001 From: RunDevelopment Date: Thu, 14 Jan 2021 17:02:55 +0100 Subject: [PATCH 1/7] WIP --- components/prism-docker.js | 61 ++++++++++++++++---- tests/languages/docker/command_feature.test | 52 +++++++++++++++++ tests/languages/docker/variable_feature.test | 12 ++++ 3 files changed, 113 insertions(+), 12 deletions(-) create mode 100644 tests/languages/docker/command_feature.test create mode 100644 tests/languages/docker/variable_feature.test diff --git a/components/prism-docker.js b/components/prism-docker.js index 270e66c554..1adbc1fc9d 100644 --- a/components/prism-docker.js +++ b/components/prism-docker.js @@ -1,14 +1,51 @@ -Prism.languages.docker = { - 'keyword': { - pattern: /(^\s*)(?:ADD|ARG|CMD|COPY|ENTRYPOINT|ENV|EXPOSE|FROM|HEALTHCHECK|LABEL|MAINTAINER|ONBUILD|RUN|SHELL|STOPSIGNAL|USER|VOLUME|WORKDIR)(?=\s)/mi, - lookbehind: true - }, - 'string': /("|')(?:(?!\1)[^\\\r\n]|\\(?:\r\n|[\s\S]))*\1/, - 'comment': { - pattern: /#.*/, +(function (Prism) { + + var string = /"(?:[^"\\\r\n]|\\(?:\r\n|[\s\S]))*"|'(?:[^'\\\r\n]|\\(?:\r\n|[\s\S]))*'/; + var commentRule = { + pattern: /(^[ \t]*)#.*/m, + lookbehind: true, greedy: true - }, - 'punctuation': /---|\.\.\.|[:[\]{}\-,|>?]/ -}; + }; + + Prism.languages.docker = { + 'command': { + 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\\])*)*/mi, + lookbehind: true, + inside: { + 'instruction': { + pattern: /^\w+|(\s)AS(?=\s)/, + lookbehind: true, + greedy: true, + alias: 'keyword' + }, + 'option': { + pattern: RegExp(/(^\s+)--[\w-]+=(?:|(?!["'])(?:[^\s\\]|\\.)+)/.source.replace(//g, function () { return string.source })), + lookbehind: true, + inside: { + 'property': /^--[\w-]+/, + 'attr-value': { + pattern: /^(=)[\s\S]+/, + lookbehind: true + }, + 'punctuation': /=/ + } + }, + 'comment': commentRule, + 'string': { + pattern: string, + greedy: true + }, + 'variable': /\$(?:\w+|\{[^{}"'\\]*\})/, + 'operator': /\\$/m, + // punctuation for JSON arrays + 'punctuation': { + pattern: /("\s*)/ + } + } + }, + 'comment': commentRule + }; + + Prism.languages.dockerfile = Prism.languages.docker; -Prism.languages.dockerfile = Prism.languages.docker; +}(Prism)); diff --git a/tests/languages/docker/command_feature.test b/tests/languages/docker/command_feature.test new file mode 100644 index 0000000000..bc0cc38c77 --- /dev/null +++ b/tests/languages/docker/command_feature.test @@ -0,0 +1,52 @@ +RUN apt-get \ +update && apt-get \ +#asd +# +\ +\ + + +install git -y #asd \ +asdasdasd + +RUN echo hello \ +# comment +world + + # this is a comment-line + RUN echo hello +RUN echo world + +RUN echo "\ + hello\ + world" + +LABEL multi.label1="value1" \ + multi.label2="value2" \ + other="value3" + +EXPOSE 80/udp + +ENV MY_NAME="John Doe" +ENV MY_NAME="John Doe" MY_DOG=Rex\ The\ Dog \ + MY_CAT=fluffy + +ADD hom?.txt /mydir/ +ADD --chown=1 files* /somedir/ +COPY --chown=1 files* /somedir/ + +ENTRYPOINT ["executable", "param1", "param2"] + +FROM debian:stable +RUN apt-get update && apt-get install -y --force-yes apache2 +EXPOSE 80 443 +VOLUME ["/var/www", "/var/log/apache2", "/etc/apache2"] +ENTRYPOINT ["/usr/sbin/apache2ctl", "-D", "FOREGROUND"] + +HEALTHCHECK --interval=5m --timeout=3s \ + CMD curl -f http://localhost/ || exit 1 + +ENTRYPOINT [ "/path/myprocess", \ + "arg1", \ + "arg2" \ +] diff --git a/tests/languages/docker/variable_feature.test b/tests/languages/docker/variable_feature.test new file mode 100644 index 0000000000..5a70970eb6 --- /dev/null +++ b/tests/languages/docker/variable_feature.test @@ -0,0 +1,12 @@ +FROM busybox +USER ${user:-some_user} +ARG user +USER $user +RUN echo $CONT_IMG_VER + +ARG CODE_VERSION=latest +FROM base:${CODE_VERSION} +CMD /code/run-app + +FROM extras:${CODE_VERSION} +CMD /code/run-extras From 74af81fc4cf649b8a826166df2f3735e9a2d3ce8 Mon Sep 17 00:00:00 2001 From: RunDevelopment Date: Sun, 17 Jan 2021 13:32:25 +0100 Subject: [PATCH 2/7] WIP --- components/prism-docker.js | 1 + 1 file changed, 1 insertion(+) diff --git a/components/prism-docker.js b/components/prism-docker.js index 1adbc1fc9d..1d55f05eb4 100644 --- a/components/prism-docker.js +++ b/components/prism-docker.js @@ -11,6 +11,7 @@ 'command': { 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\\])*)*/mi, lookbehind: true, + greedy: true, inside: { 'instruction': { pattern: /^\w+|(\s)AS(?=\s)/, From 3bb1263d9fce3d921ab9e3096bb255963c80fe74 Mon Sep 17 00:00:00 2001 From: RunDevelopment Date: Sun, 17 Jan 2021 14:06:21 +0100 Subject: [PATCH 3/7] Final --- components/prism-docker.js | 47 +++--- components/prism-docker.min.js | 2 +- tests/languages/docker/command_feature.test | 150 ++++++++++++++++++- tests/languages/docker/keyword_feature.test | 96 +++++++++--- tests/languages/docker/options_feature.test | 67 +++++++++ tests/languages/docker/string_feature.test | 52 +++++-- tests/languages/docker/variable_feature.test | 50 +++++++ 7 files changed, 401 insertions(+), 63 deletions(-) create mode 100644 tests/languages/docker/options_feature.test diff --git a/components/prism-docker.js b/components/prism-docker.js index 1d55f05eb4..655bce01b8 100644 --- a/components/prism-docker.js +++ b/components/prism-docker.js @@ -1,6 +1,12 @@ (function (Prism) { - var string = /"(?:[^"\\\r\n]|\\(?:\r\n|[\s\S]))*"|'(?:[^'\\\r\n]|\\(?:\r\n|[\s\S]))*'/; + var string = /"(?:[^"\\\r\n]|\\(?:\r\n|[\s\S]))*"|'(?:[^'\\\r\n]|\\(?:\r\n|[\s\S]))*'/.source; + var option = /--[\w-]+=(?:|(?!["'])(?:[^\s\\]|\\.)+)/.source.replace(//g, function () { return string; }); + + var stringRule = { + pattern: RegExp(string), + greedy: true + }; var commentRule = { pattern: /(^[ \t]*)#.*/m, lookbehind: true, @@ -13,35 +19,36 @@ lookbehind: true, greedy: true, inside: { - 'instruction': { - pattern: /^\w+|(\s)AS(?=\s)/, + 'options': { + pattern: RegExp(/(^\w+(?:\\$|\s)+)(?:(?:\\$|\s)+)*/.source.replace(//g, function () { return option; }), 'm'), lookbehind: true, greedy: true, - alias: 'keyword' - }, - 'option': { - pattern: RegExp(/(^\s+)--[\w-]+=(?:|(?!["'])(?:[^\s\\]|\\.)+)/.source.replace(//g, function () { return string.source })), - lookbehind: true, inside: { - 'property': /^--[\w-]+/, - 'attr-value': { - pattern: /^(=)[\s\S]+/, + 'property': { + pattern: /(^|\s)--[\w-]+/, lookbehind: true }, + 'string': [ + stringRule, + { + pattern: /(=)(?!["'])(?:[^\s\\]|\\.)+/, + lookbehind: true + } + ], + 'operator': /\\$/m, 'punctuation': /=/ } }, - 'comment': commentRule, - 'string': { - pattern: string, - greedy: true + 'instruction': { + pattern: /^\w+|(\s)AS(?=\s)/, + lookbehind: true, + greedy: true, + alias: 'keyword' }, + 'comment': commentRule, + 'string': stringRule, 'variable': /\$(?:\w+|\{[^{}"'\\]*\})/, - 'operator': /\\$/m, - // punctuation for JSON arrays - 'punctuation': { - pattern: /("\s*)/ - } + 'operator': /\\$/m } }, 'comment': commentRule diff --git a/components/prism-docker.min.js b/components/prism-docker.min.js index 9c1b73ff6d..a8cb5809b3 100644 --- a/components/prism-docker.min.js +++ b/components/prism-docker.min.js @@ -1 +1 @@ -Prism.languages.docker={keyword:{pattern:/(^\s*)(?:ADD|ARG|CMD|COPY|ENTRYPOINT|ENV|EXPOSE|FROM|HEALTHCHECK|LABEL|MAINTAINER|ONBUILD|RUN|SHELL|STOPSIGNAL|USER|VOLUME|WORKDIR)(?=\s)/im,lookbehind:!0},string:/("|')(?:(?!\1)[^\\\r\n]|\\(?:\r\n|[\s\S]))*\1/,comment:{pattern:/#.*/,greedy:!0},punctuation:/---|\.\.\.|[:[\]{}\-,|>?]/},Prism.languages.dockerfile=Prism.languages.docker; \ No newline at end of file +!function(e){var n="\"(?:[^\"\\\\\r\n]|\\\\(?:\r\n|[^]))*\"|'(?:[^'\\\\\r\n]|\\\\(?:\r\n|[^]))*'",r="--[\\w-]+=(?:|(?![\"'])(?:[^\\s\\\\]|\\\\.)+)".replace(//g,function(){return n}),t={pattern:RegExp(n),greedy:!0},o={pattern:/(^[ \t]*)#.*/m,lookbehind:!0,greedy:!0};e.languages.docker={command:{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,lookbehind:!0,greedy:!0,inside:{options:{pattern:RegExp("(^\\w+(?:\\\\$|\\s)+)(?:(?:\\\\$|\\s)+)*".replace(//g,function(){return r}),"m"),lookbehind:!0,greedy:!0,inside:{property:{pattern:/(^|\s)--[\w-]+/,lookbehind:!0},string:[t,{pattern:/(=)(?!["'])(?:[^\s\\]|\\.)+/,lookbehind:!0}],operator:/\\$/m,punctuation:/=/}},instruction:{pattern:/^\w+|(\s)AS(?=\s)/,lookbehind:!0,greedy:!0,alias:"keyword"},comment:o,string:t,variable:/\$(?:\w+|\{[^{}"'\\]*\})/,operator:/\\$/m}},comment:o},e.languages.dockerfile=e.languages.docker}(Prism); \ No newline at end of file diff --git a/tests/languages/docker/command_feature.test b/tests/languages/docker/command_feature.test index bc0cc38c77..36a07d1f99 100644 --- a/tests/languages/docker/command_feature.test +++ b/tests/languages/docker/command_feature.test @@ -1,13 +1,13 @@ RUN apt-get \ update && apt-get \ -#asd +#comment # \ \ -install git -y #asd \ -asdasdasd +install git -y #not-a-comment \ +something RUN echo hello \ # comment @@ -32,8 +32,6 @@ ENV MY_NAME="John Doe" MY_DOG=Rex\ The\ Dog \ MY_CAT=fluffy ADD hom?.txt /mydir/ -ADD --chown=1 files* /somedir/ -COPY --chown=1 files* /somedir/ ENTRYPOINT ["executable", "param1", "param2"] @@ -43,10 +41,146 @@ EXPOSE 80 443 VOLUME ["/var/www", "/var/log/apache2", "/etc/apache2"] ENTRYPOINT ["/usr/sbin/apache2ctl", "-D", "FOREGROUND"] -HEALTHCHECK --interval=5m --timeout=3s \ - CMD curl -f http://localhost/ || exit 1 - ENTRYPOINT [ "/path/myprocess", \ "arg1", \ "arg2" \ ] + +---------------------------------------------------- + +[ + ["command", [ + ["instruction", "RUN"], " apt-get ", ["operator", "\\"], + "\r\nupdate && apt-get ", ["operator", "\\"], + ["comment", "#comment"], + ["comment", "#"], + ["operator", "\\"], + ["operator", "\\"], + + "\r\n\r\n\r\ninstall git -y #not-a-comment ", ["operator", "\\"], + "\r\nsomething" + ]], + + ["command", [ + ["instruction", "RUN"], " echo hello ", ["operator", "\\"], + ["comment", "# comment"], + "\r\nworld" + ]], + + ["comment", "# this is a comment-line"], + ["command", [ + ["instruction", "RUN"], + " echo hello" + ]], + ["command", [ + ["instruction", "RUN"], + " echo world" + ]], + + ["command", [ + ["instruction", "RUN"], + " echo ", + ["string", "\"\\\r\n hello\\\r\n world\""] + ]], + + ["command", [ + ["instruction", "LABEL"], + " multi.label1=", + ["string", "\"value1\""], + ["operator", "\\"], + + "\r\n multi.label2=", + ["string", "\"value2\""], + ["operator", "\\"], + + "\r\n other=", + ["string", "\"value3\""] + ]], + + ["command", [ + ["instruction", "EXPOSE"], + " 80/udp" + ]], + + ["command", [ + ["instruction", "ENV"], + " MY_NAME=", + ["string", "\"John Doe\""] + ]], + + ["command", [ + ["instruction", "ENV"], + " MY_NAME=", + ["string", "\"John Doe\""], + " MY_DOG=Rex\\ The\\ Dog ", + ["operator", "\\"], + + "\r\n MY_CAT=fluffy" + ]], + + ["command", [ + ["instruction", "ADD"], + " hom?.txt /mydir/" + ]], + + ["command", [ + ["instruction", "ENTRYPOINT"], + " [", + ["string", "\"executable\""], + ", ", + ["string", "\"param1\""], + ", ", + ["string", "\"param2\""], + "]" + ]], + + ["command", [ + ["instruction", "FROM"], + " debian:stable" + ]], + ["command", [ + ["instruction", "RUN"], + " apt-get update && apt-get install -y --force-yes apache2" + ]], + ["command", [ + ["instruction", "EXPOSE"], + " 80 443" + ]], + ["command", [ + ["instruction", "VOLUME"], + " [", + ["string", "\"/var/www\""], + ", ", + ["string", "\"/var/log/apache2\""], + ", ", + ["string", "\"/etc/apache2\""], + "]" + ]], + ["command", [ + ["instruction", "ENTRYPOINT"], + " [", + ["string", "\"/usr/sbin/apache2ctl\""], + ", ", + ["string", "\"-D\""], + ", ", + ["string", "\"FOREGROUND\""], + "]" + ]], + + ["command", [ + ["instruction", "ENTRYPOINT"], + " [ ", + ["string", "\"/path/myprocess\""], + ", ", + ["operator", "\\"], + + ["string", "\"arg1\""], + ", ", + ["operator", "\\"], + + ["string", "\"arg2\""], + ["operator", "\\"], + + "\r\n]" + ]] +] diff --git a/tests/languages/docker/keyword_feature.test b/tests/languages/docker/keyword_feature.test index a36a7e00b6..d33fb88d03 100644 --- a/tests/languages/docker/keyword_feature.test +++ b/tests/languages/docker/keyword_feature.test @@ -18,28 +18,82 @@ STOPSIGNAL signal ---------------------------------------------------- [ - ["keyword", "ONBUILD"], ["keyword", "ADD"], " . /app/src\r\n", - ["keyword", "FROM"], " ubuntu\r\n", - ["keyword", "MAINTAINER"], " SvenDowideit@home.org.au\r\n", - ["keyword", "RUN"], " cd /tmp\r\n", - ["keyword", "EXPOSE"], " 5900\r\n", - ["keyword", "ENV"], " myName John Doe\r\n", - ["keyword", "COPY"], " hom* /mydir/\r\n", - ["keyword", "VOLUME"], " /myvol\r\n", - ["keyword", "USER"], " daemon\r\n", - ["keyword", "WORKDIR"], " /a\r\n", - ["keyword", "HEALTHCHECK"], ["keyword", "CMD"], " echo ", ["string", "\"foo\""], - ["keyword", "LABEL"], " version=", ["string", "\"1.0\""], - ["keyword", "ENTRYPOINT"], - ["punctuation", "["], ["string", "\"top\""], ["punctuation", ","], - ["string", "\"-b\""], ["punctuation", "]"], - ["keyword", "ARG"], " user1\r\n", - ["keyword", "SHELL"], - ["punctuation", "["], ["string", "\"powershell\""], ["punctuation", ","], - ["string", "\"-command\""], ["punctuation", "]"], - ["keyword", "STOPSIGNAL"], " signal" + ["command", [ + ["instruction", "ONBUILD"], + " ADD . /app/src" + ]], + ["command", [ + ["instruction", "FROM"], + " ubuntu" + ]], + ["command", [ + ["instruction", "MAINTAINER"], + " SvenDowideit@home.org.au" + ]], + ["command", [ + ["instruction", "RUN"], + " cd /tmp" + ]], + ["command", [ + ["instruction", "EXPOSE"], + " 5900" + ]], + ["command", [ + ["instruction", "ENV"], + " myName John Doe" + ]], + ["command", [ + ["instruction", "COPY"], + " hom* /mydir/" + ]], + ["command", [ + ["instruction", "VOLUME"], + " /myvol" + ]], + ["command", [ + ["instruction", "USER"], + " daemon" + ]], + ["command", [ + ["instruction", "WORKDIR"], + " /a" + ]], + ["command", [ + ["instruction", "HEALTHCHECK"], + " CMD echo ", + ["string", "\"foo\""] + ]], + ["command", [ + ["instruction", "LABEL"], + " version=", + ["string", "\"1.0\""] + ]], + ["command", [ + ["instruction", "ENTRYPOINT"], + " [", + ["string", "\"top\""], + ", ", + ["string", "\"-b\""], + "]" + ]], + ["command", [ + ["instruction", "ARG"], + " user1" + ]], + ["command", [ + ["instruction", "SHELL"], + " [", + ["string", "\"powershell\""], + ", ", + ["string", "\"-command\""], + "]" + ]], + ["command", [ + ["instruction", "STOPSIGNAL"], + " signal" + ]] ] ---------------------------------------------------- -Checks for keywords. \ No newline at end of file +Checks for keywords. diff --git a/tests/languages/docker/options_feature.test b/tests/languages/docker/options_feature.test new file mode 100644 index 0000000000..7a8af4c9ab --- /dev/null +++ b/tests/languages/docker/options_feature.test @@ -0,0 +1,67 @@ +ADD --chown=1 files* /somedir/ +COPY --chown=1 files* /somedir/ + +HEALTHCHECK --interval=5m --timeout=3s \ + CMD foo + +HEALTHCHECK \ + --interval=5m \ + --timeout=3s \ + CMD foo + +---------------------------------------------------- + +[ + ["command", [ + ["instruction", "ADD"], + ["options", [ + ["property", "--chown"], + ["punctuation", "="], + ["string", "1"] + ]], + " files* /somedir/" + ]], + ["command", [ + ["instruction", "COPY"], + ["options", [ + ["property", "--chown"], + ["punctuation", "="], + ["string", "1"] + ]], + " files* /somedir/" + ]], + + ["command", [ + ["instruction", "HEALTHCHECK"], + ["options", [ + ["property", "--interval"], + ["punctuation", "="], + ["string", "5m"], + ["property", "--timeout"], + ["punctuation", "="], + ["string", "3s"] + ]], + ["operator", "\\"], + + "\r\n CMD foo" + ]], + + ["command", [ + ["instruction", "HEALTHCHECK"], + ["operator", "\\"], + + ["options", [ + ["property", "--interval"], + ["punctuation", "="], + ["string", "5m"], + ["operator", "\\"], + + ["property", "--timeout"], + ["punctuation", "="], + ["string", "3s"] + ]], + ["operator", "\\"], + + "\r\n CMD foo" + ]] +] \ No newline at end of file diff --git a/tests/languages/docker/string_feature.test b/tests/languages/docker/string_feature.test index e131f02136..7fd50e6d2c 100644 --- a/tests/languages/docker/string_feature.test +++ b/tests/languages/docker/string_feature.test @@ -1,23 +1,49 @@ -"" -"fo\"obar" -"foo\ +RUN echo "" +RUN echo "fo\"obar" +RUN echo "foo\ bar" -'' -'fo\'obar' -'foo\ + +RUN echo '' +RUN echo 'fo\'obar' +RUN echo 'foo\ bar' ---------------------------------------------------- [ - ["string", "\"\""], - ["string", "\"fo\\\"obar\""], - ["string", "\"foo\\\r\nbar\""], - ["string", "''"], - ["string", "'fo\\'obar'"], - ["string", "'foo\\\r\nbar'"] + ["command", [ + ["instruction", "RUN"], + " echo ", + ["string", "\"\""] + ]], + ["command", [ + ["instruction", "RUN"], + " echo ", + ["string", "\"fo\\\"obar\""] + ]], + ["command", [ + ["instruction", "RUN"], + " echo ", + ["string", "\"foo\\\r\nbar\""] + ]], + + ["command", [ + ["instruction", "RUN"], + " echo ", + ["string", "''"] + ]], + ["command", [ + ["instruction", "RUN"], + " echo ", + ["string", "'fo\\'obar'"] + ]], + ["command", [ + ["instruction", "RUN"], + " echo ", + ["string", "'foo\\\r\nbar'"] + ]] ] ---------------------------------------------------- -Checks for strings. \ No newline at end of file +Checks for strings. diff --git a/tests/languages/docker/variable_feature.test b/tests/languages/docker/variable_feature.test index 5a70970eb6..71b790b1cb 100644 --- a/tests/languages/docker/variable_feature.test +++ b/tests/languages/docker/variable_feature.test @@ -10,3 +10,53 @@ CMD /code/run-app FROM extras:${CODE_VERSION} CMD /code/run-extras + +---------------------------------------------------- + +[ + ["command", [ + ["instruction", "FROM"], + " busybox" + ]], + ["command", [ + ["instruction", "USER"], + ["variable", "${user:-some_user}"] + ]], + ["command", [ + ["instruction", "ARG"], + " user" + ]], + ["command", [ + ["instruction", "USER"], + ["variable", "$user"] + ]], + ["command", [ + ["instruction", "RUN"], + " echo ", + ["variable", "$CONT_IMG_VER"] + ]], + + ["command", [ + ["instruction", "ARG"], + " CODE_VERSION=latest" + ]], + ["command", [ + ["instruction", "FROM"], + " base:", + ["variable", "${CODE_VERSION}"] + ]], + ["command", [ + ["instruction", "CMD"], + " /code/run-app" + ]], + + ["command", [ + ["instruction", "FROM"], + " extras:", + ["variable", "${CODE_VERSION}"] + ]], + ["command", [ + ["instruction", "CMD"], + " /code/run-extras" + ]] +] \ No newline at end of file From 79b202f13516e7e36b27318054bb0139b3e2bd27 Mon Sep 17 00:00:00 2001 From: RunDevelopment Date: Sun, 17 Jan 2021 14:24:12 +0100 Subject: [PATCH 4/7] Added "AS" test case --- tests/languages/docker/keyword_feature.test | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/tests/languages/docker/keyword_feature.test b/tests/languages/docker/keyword_feature.test index d33fb88d03..600c853cdd 100644 --- a/tests/languages/docker/keyword_feature.test +++ b/tests/languages/docker/keyword_feature.test @@ -1,5 +1,6 @@ ONBUILD ADD . /app/src FROM ubuntu +FROM ubuntu AS build MAINTAINER SvenDowideit@home.org.au RUN cd /tmp EXPOSE 5900 @@ -26,6 +27,12 @@ STOPSIGNAL signal ["instruction", "FROM"], " ubuntu" ]], + ["command", [ + ["instruction", "FROM"], + " ubuntu ", + ["instruction", "AS"], + " build" + ]], ["command", [ ["instruction", "MAINTAINER"], " SvenDowideit@home.org.au" From 22cfc3bcc6d9ddb555581fa8bb14c8e2b04977be Mon Sep 17 00:00:00 2001 From: RunDevelopment Date: Fri, 22 Jan 2021 13:11:19 +0100 Subject: [PATCH 5/7] Instruction vs keyword This changes the usage of "instruction" and "keyword" as discussed. It also adds support for some missing keywords. --- components/prism-docker.js | 55 ++++++++++--- components/prism-docker.min.js | 2 +- ..._feature.test => instruction_feature.test} | 70 ++++++++-------- tests/languages/docker/keyword_feature.test | 81 ++++++++++--------- tests/languages/docker/options_feature.test | 42 +++++++--- tests/languages/docker/string_feature.test | 24 +++--- tests/languages/docker/variable_feature.test | 40 ++++----- 7 files changed, 190 insertions(+), 124 deletions(-) rename tests/languages/docker/{command_feature.test => instruction_feature.test} (71%) diff --git a/components/prism-docker.js b/components/prism-docker.js index 655bce01b8..d63d626934 100644 --- a/components/prism-docker.js +++ b/components/prism-docker.js @@ -1,7 +1,12 @@ (function (Prism) { + var spaceAfterBaskSlash = /\\[\r\n](?:\s|\\[\r\n]|#.*(?!.))*(?![\s#]|\\[\r\n])/.source; + // At least one space, comment, or line break + var space = /(?:[ \t]+(?![ \t])(?:)?|)/.source + .replace(//g, function () { return spaceAfterBaskSlash; }); + var string = /"(?:[^"\\\r\n]|\\(?:\r\n|[\s\S]))*"|'(?:[^'\\\r\n]|\\(?:\r\n|[\s\S]))*'/.source; - var option = /--[\w-]+=(?:|(?!["'])(?:[^\s\\]|\\.)+)/.source.replace(//g, function () { return string; }); + var option = /--[\w-]+=(?:|(?!["'])(?:[^\s\\]|\\.)+)/.source.replace(//g, function () { return string; }); var stringRule = { pattern: RegExp(string), @@ -13,14 +18,28 @@ greedy: true }; + /** + * @param {string} source + * @param {string} flags + * @returns {RegExp} + */ + function re(source, flags) { + source = source + .replace(//g, function () { return option; }) + .replace(//g, function () { return space; }); + + return RegExp(source, flags); + } + Prism.languages.docker = { - 'command': { + 'instruction': { 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\\])*)*/mi, lookbehind: true, greedy: true, + alias: 'command', inside: { 'options': { - pattern: RegExp(/(^\w+(?:\\$|\s)+)(?:(?:\\$|\s)+)*/.source.replace(//g, function () { return option; }), 'm'), + pattern: re(/(^(?:ONBUILD)?\w+)(?:)*/.source, 'i'), lookbehind: true, greedy: true, inside: { @@ -39,12 +58,30 @@ 'punctuation': /=/ } }, - 'instruction': { - pattern: /^\w+|(\s)AS(?=\s)/, - lookbehind: true, - greedy: true, - alias: 'keyword' - }, + 'keyword': [ + { + // https://docs.docker.com/engine/reference/builder/#healthcheck + pattern: re(/(^(?:ONBUILD)?HEALTHCHECK(?:)*)(?:CMD|NONE)\b/.source, 'i'), + lookbehind: true, + greedy: true + }, + { + // https://docs.docker.com/engine/reference/builder/#from + pattern: re(/(^(?:ONBUILD)?FROM(?:)*(?!--)[^ \t\\]+)AS/.source, 'i'), + lookbehind: true, + greedy: true + }, + { + // https://docs.docker.com/engine/reference/builder/#onbuild + pattern: re(/(^ONBUILD)\w+/.source, 'i'), + lookbehind: true, + greedy: true + }, + { + pattern: /^\w+/, + greedy: true + } + ], 'comment': commentRule, 'string': stringRule, 'variable': /\$(?:\w+|\{[^{}"'\\]*\})/, diff --git a/components/prism-docker.min.js b/components/prism-docker.min.js index a8cb5809b3..8ee6f04149 100644 --- a/components/prism-docker.min.js +++ b/components/prism-docker.min.js @@ -1 +1 @@ -!function(e){var n="\"(?:[^\"\\\\\r\n]|\\\\(?:\r\n|[^]))*\"|'(?:[^'\\\\\r\n]|\\\\(?:\r\n|[^]))*'",r="--[\\w-]+=(?:|(?![\"'])(?:[^\\s\\\\]|\\\\.)+)".replace(//g,function(){return n}),t={pattern:RegExp(n),greedy:!0},o={pattern:/(^[ \t]*)#.*/m,lookbehind:!0,greedy:!0};e.languages.docker={command:{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,lookbehind:!0,greedy:!0,inside:{options:{pattern:RegExp("(^\\w+(?:\\\\$|\\s)+)(?:(?:\\\\$|\\s)+)*".replace(//g,function(){return r}),"m"),lookbehind:!0,greedy:!0,inside:{property:{pattern:/(^|\s)--[\w-]+/,lookbehind:!0},string:[t,{pattern:/(=)(?!["'])(?:[^\s\\]|\\.)+/,lookbehind:!0}],operator:/\\$/m,punctuation:/=/}},instruction:{pattern:/^\w+|(\s)AS(?=\s)/,lookbehind:!0,greedy:!0,alias:"keyword"},comment:o,string:t,variable:/\$(?:\w+|\{[^{}"'\\]*\})/,operator:/\\$/m}},comment:o},e.languages.dockerfile=e.languages.docker}(Prism); \ No newline at end of file +!function(e){var r="(?:[ \t]+(?![ \t])(?:)?|)".replace(//g,function(){return"\\\\[\r\n](?:\\s|\\\\[\r\n]|#.*(?!.))*(?![\\s#]|\\\\[\r\n])"}),n="\"(?:[^\"\\\\\r\n]|\\\\(?:\r\n|[^]))*\"|'(?:[^'\\\\\r\n]|\\\\(?:\r\n|[^]))*'",t="--[\\w-]+=(?:|(?![\"'])(?:[^\\s\\\\]|\\\\.)+)".replace(//g,function(){return n}),o={pattern:RegExp(n),greedy:!0},i={pattern:/(^[ \t]*)#.*/m,lookbehind:!0,greedy:!0};function a(e,n){return e=e.replace(//g,function(){return t}).replace(//g,function(){return r}),RegExp(e,n)}e.languages.docker={instruction:{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,lookbehind:!0,greedy:!0,alias:"command",inside:{options:{pattern:a("(^(?:ONBUILD)?\\w+)(?:)*","i"),lookbehind:!0,greedy:!0,inside:{property:{pattern:/(^|\s)--[\w-]+/,lookbehind:!0},string:[o,{pattern:/(=)(?!["'])(?:[^\s\\]|\\.)+/,lookbehind:!0}],operator:/\\$/m,punctuation:/=/}},keyword:[{pattern:a("(^(?:ONBUILD)?HEALTHCHECK(?:)*)(?:CMD|NONE)\\b","i"),lookbehind:!0,greedy:!0},{pattern:a("(^(?:ONBUILD)?FROM(?:)*(?!--)[^ \t\\\\]+)AS","i"),lookbehind:!0,greedy:!0},{pattern:a("(^ONBUILD)\\w+","i"),lookbehind:!0,greedy:!0},{pattern:/^\w+/,greedy:!0}],comment:i,string:o,variable:/\$(?:\w+|\{[^{}"'\\]*\})/,operator:/\\$/m}},comment:i},e.languages.dockerfile=e.languages.docker}(Prism); \ No newline at end of file diff --git a/tests/languages/docker/command_feature.test b/tests/languages/docker/instruction_feature.test similarity index 71% rename from tests/languages/docker/command_feature.test rename to tests/languages/docker/instruction_feature.test index 36a07d1f99..49179c194b 100644 --- a/tests/languages/docker/command_feature.test +++ b/tests/languages/docker/instruction_feature.test @@ -49,8 +49,8 @@ ENTRYPOINT [ "/path/myprocess", \ ---------------------------------------------------- [ - ["command", [ - ["instruction", "RUN"], " apt-get ", ["operator", "\\"], + ["instruction", [ + ["keyword", "RUN"], " apt-get ", ["operator", "\\"], "\r\nupdate && apt-get ", ["operator", "\\"], ["comment", "#comment"], ["comment", "#"], @@ -61,30 +61,30 @@ ENTRYPOINT [ "/path/myprocess", \ "\r\nsomething" ]], - ["command", [ - ["instruction", "RUN"], " echo hello ", ["operator", "\\"], + ["instruction", [ + ["keyword", "RUN"], " echo hello ", ["operator", "\\"], ["comment", "# comment"], "\r\nworld" ]], ["comment", "# this is a comment-line"], - ["command", [ - ["instruction", "RUN"], + ["instruction", [ + ["keyword", "RUN"], " echo hello" ]], - ["command", [ - ["instruction", "RUN"], + ["instruction", [ + ["keyword", "RUN"], " echo world" ]], - ["command", [ - ["instruction", "RUN"], + ["instruction", [ + ["keyword", "RUN"], " echo ", ["string", "\"\\\r\n hello\\\r\n world\""] ]], - ["command", [ - ["instruction", "LABEL"], + ["instruction", [ + ["keyword", "LABEL"], " multi.label1=", ["string", "\"value1\""], ["operator", "\\"], @@ -97,19 +97,19 @@ ENTRYPOINT [ "/path/myprocess", \ ["string", "\"value3\""] ]], - ["command", [ - ["instruction", "EXPOSE"], + ["instruction", [ + ["keyword", "EXPOSE"], " 80/udp" ]], - ["command", [ - ["instruction", "ENV"], + ["instruction", [ + ["keyword", "ENV"], " MY_NAME=", ["string", "\"John Doe\""] ]], - ["command", [ - ["instruction", "ENV"], + ["instruction", [ + ["keyword", "ENV"], " MY_NAME=", ["string", "\"John Doe\""], " MY_DOG=Rex\\ The\\ Dog ", @@ -118,13 +118,13 @@ ENTRYPOINT [ "/path/myprocess", \ "\r\n MY_CAT=fluffy" ]], - ["command", [ - ["instruction", "ADD"], + ["instruction", [ + ["keyword", "ADD"], " hom?.txt /mydir/" ]], - ["command", [ - ["instruction", "ENTRYPOINT"], + ["instruction", [ + ["keyword", "ENTRYPOINT"], " [", ["string", "\"executable\""], ", ", @@ -134,20 +134,20 @@ ENTRYPOINT [ "/path/myprocess", \ "]" ]], - ["command", [ - ["instruction", "FROM"], + ["instruction", [ + ["keyword", "FROM"], " debian:stable" ]], - ["command", [ - ["instruction", "RUN"], + ["instruction", [ + ["keyword", "RUN"], " apt-get update && apt-get install -y --force-yes apache2" ]], - ["command", [ - ["instruction", "EXPOSE"], + ["instruction", [ + ["keyword", "EXPOSE"], " 80 443" ]], - ["command", [ - ["instruction", "VOLUME"], + ["instruction", [ + ["keyword", "VOLUME"], " [", ["string", "\"/var/www\""], ", ", @@ -156,8 +156,8 @@ ENTRYPOINT [ "/path/myprocess", \ ["string", "\"/etc/apache2\""], "]" ]], - ["command", [ - ["instruction", "ENTRYPOINT"], + ["instruction", [ + ["keyword", "ENTRYPOINT"], " [", ["string", "\"/usr/sbin/apache2ctl\""], ", ", @@ -167,8 +167,8 @@ ENTRYPOINT [ "/path/myprocess", \ "]" ]], - ["command", [ - ["instruction", "ENTRYPOINT"], + ["instruction", [ + ["keyword", "ENTRYPOINT"], " [ ", ["string", "\"/path/myprocess\""], ", ", @@ -183,4 +183,4 @@ ENTRYPOINT [ "/path/myprocess", \ "\r\n]" ]] -] +] \ No newline at end of file diff --git a/tests/languages/docker/keyword_feature.test b/tests/languages/docker/keyword_feature.test index 600c853cdd..5330a6221e 100644 --- a/tests/languages/docker/keyword_feature.test +++ b/tests/languages/docker/keyword_feature.test @@ -10,6 +10,7 @@ VOLUME /myvol USER daemon WORKDIR /a HEALTHCHECK CMD echo "foo" +HEALTHCHECK NONE LABEL version="1.0" ENTRYPOINT ["top", "-b"] ARG user1 @@ -19,84 +20,90 @@ STOPSIGNAL signal ---------------------------------------------------- [ - ["command", [ - ["instruction", "ONBUILD"], - " ADD . /app/src" + ["instruction", [ + ["keyword", "ONBUILD"], + ["keyword", "ADD"], + " . /app/src" ]], - ["command", [ - ["instruction", "FROM"], + ["instruction", [ + ["keyword", "FROM"], " ubuntu" ]], - ["command", [ - ["instruction", "FROM"], + ["instruction", [ + ["keyword", "FROM"], " ubuntu ", - ["instruction", "AS"], + ["keyword", "AS"], " build" ]], - ["command", [ - ["instruction", "MAINTAINER"], + ["instruction", [ + ["keyword", "MAINTAINER"], " SvenDowideit@home.org.au" ]], - ["command", [ - ["instruction", "RUN"], + ["instruction", [ + ["keyword", "RUN"], " cd /tmp" ]], - ["command", [ - ["instruction", "EXPOSE"], + ["instruction", [ + ["keyword", "EXPOSE"], " 5900" ]], - ["command", [ - ["instruction", "ENV"], + ["instruction", [ + ["keyword", "ENV"], " myName John Doe" ]], - ["command", [ - ["instruction", "COPY"], + ["instruction", [ + ["keyword", "COPY"], " hom* /mydir/" ]], - ["command", [ - ["instruction", "VOLUME"], + ["instruction", [ + ["keyword", "VOLUME"], " /myvol" ]], - ["command", [ - ["instruction", "USER"], + ["instruction", [ + ["keyword", "USER"], " daemon" ]], - ["command", [ - ["instruction", "WORKDIR"], + ["instruction", [ + ["keyword", "WORKDIR"], " /a" ]], - ["command", [ - ["instruction", "HEALTHCHECK"], - " CMD echo ", + ["instruction", [ + ["keyword", "HEALTHCHECK"], + ["keyword", "CMD"], + " echo ", ["string", "\"foo\""] ]], - ["command", [ - ["instruction", "LABEL"], + ["instruction", [ + ["keyword", "HEALTHCHECK"], + ["keyword", "NONE"] + ]], + ["instruction", [ + ["keyword", "LABEL"], " version=", ["string", "\"1.0\""] ]], - ["command", [ - ["instruction", "ENTRYPOINT"], + ["instruction", [ + ["keyword", "ENTRYPOINT"], " [", ["string", "\"top\""], ", ", ["string", "\"-b\""], "]" ]], - ["command", [ - ["instruction", "ARG"], + ["instruction", [ + ["keyword", "ARG"], " user1" ]], - ["command", [ - ["instruction", "SHELL"], + ["instruction", [ + ["keyword", "SHELL"], " [", ["string", "\"powershell\""], ", ", ["string", "\"-command\""], "]" ]], - ["command", [ - ["instruction", "STOPSIGNAL"], + ["instruction", [ + ["keyword", "STOPSIGNAL"], " signal" ]] ] diff --git a/tests/languages/docker/options_feature.test b/tests/languages/docker/options_feature.test index 7a8af4c9ab..03f4841ce5 100644 --- a/tests/languages/docker/options_feature.test +++ b/tests/languages/docker/options_feature.test @@ -4,6 +4,9 @@ COPY --chown=1 files* /somedir/ HEALTHCHECK --interval=5m --timeout=3s \ CMD foo +ONBUILD HEALTHCHECK --interval=5m --timeout=3s \ + CMD foo + HEALTHCHECK \ --interval=5m \ --timeout=3s \ @@ -12,8 +15,8 @@ HEALTHCHECK \ ---------------------------------------------------- [ - ["command", [ - ["instruction", "ADD"], + ["instruction", [ + ["keyword", "ADD"], ["options", [ ["property", "--chown"], ["punctuation", "="], @@ -21,8 +24,8 @@ HEALTHCHECK \ ]], " files* /somedir/" ]], - ["command", [ - ["instruction", "COPY"], + ["instruction", [ + ["keyword", "COPY"], ["options", [ ["property", "--chown"], ["punctuation", "="], @@ -31,8 +34,25 @@ HEALTHCHECK \ " files* /somedir/" ]], - ["command", [ - ["instruction", "HEALTHCHECK"], + ["instruction", [ + ["keyword", "HEALTHCHECK"], + ["options", [ + ["property", "--interval"], + ["punctuation", "="], + ["string", "5m"], + ["property", "--timeout"], + ["punctuation", "="], + ["string", "3s"] + ]], + ["operator", "\\"], + + ["keyword", "CMD"], + " foo" + ]], + + ["instruction", [ + ["keyword", "ONBUILD"], + ["keyword", "HEALTHCHECK"], ["options", [ ["property", "--interval"], ["punctuation", "="], @@ -43,11 +63,12 @@ HEALTHCHECK \ ]], ["operator", "\\"], - "\r\n CMD foo" + ["keyword", "CMD"], + " foo" ]], - ["command", [ - ["instruction", "HEALTHCHECK"], + ["instruction", [ + ["keyword", "HEALTHCHECK"], ["operator", "\\"], ["options", [ @@ -62,6 +83,7 @@ HEALTHCHECK \ ]], ["operator", "\\"], - "\r\n CMD foo" + ["keyword", "CMD"], + " foo" ]] ] \ No newline at end of file diff --git a/tests/languages/docker/string_feature.test b/tests/languages/docker/string_feature.test index 7fd50e6d2c..9f7b812f4c 100644 --- a/tests/languages/docker/string_feature.test +++ b/tests/languages/docker/string_feature.test @@ -11,34 +11,34 @@ bar' ---------------------------------------------------- [ - ["command", [ - ["instruction", "RUN"], + ["instruction", [ + ["keyword", "RUN"], " echo ", ["string", "\"\""] ]], - ["command", [ - ["instruction", "RUN"], + ["instruction", [ + ["keyword", "RUN"], " echo ", ["string", "\"fo\\\"obar\""] ]], - ["command", [ - ["instruction", "RUN"], + ["instruction", [ + ["keyword", "RUN"], " echo ", ["string", "\"foo\\\r\nbar\""] ]], - ["command", [ - ["instruction", "RUN"], + ["instruction", [ + ["keyword", "RUN"], " echo ", ["string", "''"] ]], - ["command", [ - ["instruction", "RUN"], + ["instruction", [ + ["keyword", "RUN"], " echo ", ["string", "'fo\\'obar'"] ]], - ["command", [ - ["instruction", "RUN"], + ["instruction", [ + ["keyword", "RUN"], " echo ", ["string", "'foo\\\r\nbar'"] ]] diff --git a/tests/languages/docker/variable_feature.test b/tests/languages/docker/variable_feature.test index 71b790b1cb..53067767e9 100644 --- a/tests/languages/docker/variable_feature.test +++ b/tests/languages/docker/variable_feature.test @@ -14,49 +14,49 @@ CMD /code/run-extras ---------------------------------------------------- [ - ["command", [ - ["instruction", "FROM"], + ["instruction", [ + ["keyword", "FROM"], " busybox" ]], - ["command", [ - ["instruction", "USER"], + ["instruction", [ + ["keyword", "USER"], ["variable", "${user:-some_user}"] ]], - ["command", [ - ["instruction", "ARG"], + ["instruction", [ + ["keyword", "ARG"], " user" ]], - ["command", [ - ["instruction", "USER"], + ["instruction", [ + ["keyword", "USER"], ["variable", "$user"] ]], - ["command", [ - ["instruction", "RUN"], + ["instruction", [ + ["keyword", "RUN"], " echo ", ["variable", "$CONT_IMG_VER"] ]], - ["command", [ - ["instruction", "ARG"], + ["instruction", [ + ["keyword", "ARG"], " CODE_VERSION=latest" ]], - ["command", [ - ["instruction", "FROM"], + ["instruction", [ + ["keyword", "FROM"], " base:", ["variable", "${CODE_VERSION}"] ]], - ["command", [ - ["instruction", "CMD"], + ["instruction", [ + ["keyword", "CMD"], " /code/run-app" ]], - ["command", [ - ["instruction", "FROM"], + ["instruction", [ + ["keyword", "FROM"], " extras:", ["variable", "${CODE_VERSION}"] ]], - ["command", [ - ["instruction", "CMD"], + ["instruction", [ + ["keyword", "CMD"], " /code/run-extras" ]] ] \ No newline at end of file From 159ad50942dd17d871fc76a388e2e677be95d47c Mon Sep 17 00:00:00 2001 From: RunDevelopment Date: Sat, 23 Jan 2021 15:06:17 +0100 Subject: [PATCH 6/7] Removed `command` alias --- components/prism-docker.js | 1 - components/prism-docker.min.js | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/components/prism-docker.js b/components/prism-docker.js index d63d626934..6f82406a53 100644 --- a/components/prism-docker.js +++ b/components/prism-docker.js @@ -36,7 +36,6 @@ 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\\])*)*/mi, lookbehind: true, greedy: true, - alias: 'command', inside: { 'options': { pattern: re(/(^(?:ONBUILD)?\w+)(?:)*/.source, 'i'), diff --git a/components/prism-docker.min.js b/components/prism-docker.min.js index 8ee6f04149..3d23e8a719 100644 --- a/components/prism-docker.min.js +++ b/components/prism-docker.min.js @@ -1 +1 @@ -!function(e){var r="(?:[ \t]+(?![ \t])(?:)?|)".replace(//g,function(){return"\\\\[\r\n](?:\\s|\\\\[\r\n]|#.*(?!.))*(?![\\s#]|\\\\[\r\n])"}),n="\"(?:[^\"\\\\\r\n]|\\\\(?:\r\n|[^]))*\"|'(?:[^'\\\\\r\n]|\\\\(?:\r\n|[^]))*'",t="--[\\w-]+=(?:|(?![\"'])(?:[^\\s\\\\]|\\\\.)+)".replace(//g,function(){return n}),o={pattern:RegExp(n),greedy:!0},i={pattern:/(^[ \t]*)#.*/m,lookbehind:!0,greedy:!0};function a(e,n){return e=e.replace(//g,function(){return t}).replace(//g,function(){return r}),RegExp(e,n)}e.languages.docker={instruction:{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,lookbehind:!0,greedy:!0,alias:"command",inside:{options:{pattern:a("(^(?:ONBUILD)?\\w+)(?:)*","i"),lookbehind:!0,greedy:!0,inside:{property:{pattern:/(^|\s)--[\w-]+/,lookbehind:!0},string:[o,{pattern:/(=)(?!["'])(?:[^\s\\]|\\.)+/,lookbehind:!0}],operator:/\\$/m,punctuation:/=/}},keyword:[{pattern:a("(^(?:ONBUILD)?HEALTHCHECK(?:)*)(?:CMD|NONE)\\b","i"),lookbehind:!0,greedy:!0},{pattern:a("(^(?:ONBUILD)?FROM(?:)*(?!--)[^ \t\\\\]+)AS","i"),lookbehind:!0,greedy:!0},{pattern:a("(^ONBUILD)\\w+","i"),lookbehind:!0,greedy:!0},{pattern:/^\w+/,greedy:!0}],comment:i,string:o,variable:/\$(?:\w+|\{[^{}"'\\]*\})/,operator:/\\$/m}},comment:i},e.languages.dockerfile=e.languages.docker}(Prism); \ No newline at end of file +!function(e){var r="(?:[ \t]+(?![ \t])(?:)?|)".replace(//g,function(){return"\\\\[\r\n](?:\\s|\\\\[\r\n]|#.*(?!.))*(?![\\s#]|\\\\[\r\n])"}),n="\"(?:[^\"\\\\\r\n]|\\\\(?:\r\n|[^]))*\"|'(?:[^'\\\\\r\n]|\\\\(?:\r\n|[^]))*'",t="--[\\w-]+=(?:|(?![\"'])(?:[^\\s\\\\]|\\\\.)+)".replace(//g,function(){return n}),o={pattern:RegExp(n),greedy:!0},i={pattern:/(^[ \t]*)#.*/m,lookbehind:!0,greedy:!0};function a(e,n){return e=e.replace(//g,function(){return t}).replace(//g,function(){return r}),RegExp(e,n)}e.languages.docker={instruction:{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,lookbehind:!0,greedy:!0,inside:{options:{pattern:a("(^(?:ONBUILD)?\\w+)(?:)*","i"),lookbehind:!0,greedy:!0,inside:{property:{pattern:/(^|\s)--[\w-]+/,lookbehind:!0},string:[o,{pattern:/(=)(?!["'])(?:[^\s\\]|\\.)+/,lookbehind:!0}],operator:/\\$/m,punctuation:/=/}},keyword:[{pattern:a("(^(?:ONBUILD)?HEALTHCHECK(?:)*)(?:CMD|NONE)\\b","i"),lookbehind:!0,greedy:!0},{pattern:a("(^(?:ONBUILD)?FROM(?:)*(?!--)[^ \t\\\\]+)AS","i"),lookbehind:!0,greedy:!0},{pattern:a("(^ONBUILD)\\w+","i"),lookbehind:!0,greedy:!0},{pattern:/^\w+/,greedy:!0}],comment:i,string:o,variable:/\$(?:\w+|\{[^{}"'\\]*\})/,operator:/\\$/m}},comment:i},e.languages.dockerfile=e.languages.docker}(Prism); \ No newline at end of file From 5af096ed93651ee087b73ffc07ae4d4a7392e5a7 Mon Sep 17 00:00:00 2001 From: RunDevelopment Date: Fri, 29 Jan 2021 14:44:41 +0100 Subject: [PATCH 7/7] Added comment and fixed typo --- components/prism-docker.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/components/prism-docker.js b/components/prism-docker.js index 6f82406a53..2c8c7740aa 100644 --- a/components/prism-docker.js +++ b/components/prism-docker.js @@ -1,9 +1,12 @@ (function (Prism) { - var spaceAfterBaskSlash = /\\[\r\n](?:\s|\\[\r\n]|#.*(?!.))*(?![\s#]|\\[\r\n])/.source; + // Many of the following regexes will contain negated lookaheads like `[ \t]+(?![ \t])`. This is a trick to ensure + // that quantifiers behave *atomically*. Atomic quantifiers are necessary to prevent exponential backtracking. + + var spaceAfterBackSlash = /\\[\r\n](?:\s|\\[\r\n]|#.*(?!.))*(?![\s#]|\\[\r\n])/.source; // At least one space, comment, or line break var space = /(?:[ \t]+(?![ \t])(?:)?|)/.source - .replace(//g, function () { return spaceAfterBaskSlash; }); + .replace(//g, function () { return spaceAfterBackSlash; }); var string = /"(?:[^"\\\r\n]|\\(?:\r\n|[\s\S]))*"|'(?:[^'\\\r\n]|\\(?:\r\n|[\s\S]))*'/.source; var option = /--[\w-]+=(?:|(?!["'])(?:[^\s\\]|\\.)+)/.source.replace(//g, function () { return string; });