Skip to content

Commit 93dd83c

Browse files
Docker: Improvements (#2720)
1 parent 18c67b4 commit 93dd83c

7 files changed

+562
-47
lines changed

components/prism-docker.js

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

14-
Prism.languages.dockerfile = Prism.languages.docker;
98+
}(Prism));

components/prism-docker.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
RUN apt-get \
2+
update && apt-get \
3+
#comment
4+
#
5+
\
6+
\
7+
8+
9+
install git -y #not-a-comment \
10+
something
11+
12+
RUN echo hello \
13+
# comment
14+
world
15+
16+
# this is a comment-line
17+
RUN echo hello
18+
RUN echo world
19+
20+
RUN echo "\
21+
hello\
22+
world"
23+
24+
LABEL multi.label1="value1" \
25+
multi.label2="value2" \
26+
other="value3"
27+
28+
EXPOSE 80/udp
29+
30+
ENV MY_NAME="John Doe"
31+
ENV MY_NAME="John Doe" MY_DOG=Rex\ The\ Dog \
32+
MY_CAT=fluffy
33+
34+
ADD hom?.txt /mydir/
35+
36+
ENTRYPOINT ["executable", "param1", "param2"]
37+
38+
FROM debian:stable
39+
RUN apt-get update && apt-get install -y --force-yes apache2
40+
EXPOSE 80 443
41+
VOLUME ["/var/www", "/var/log/apache2", "/etc/apache2"]
42+
ENTRYPOINT ["/usr/sbin/apache2ctl", "-D", "FOREGROUND"]
43+
44+
ENTRYPOINT [ "/path/myprocess", \
45+
"arg1", \
46+
"arg2" \
47+
]
48+
49+
----------------------------------------------------
50+
51+
[
52+
["instruction", [
53+
["keyword", "RUN"], " apt-get ", ["operator", "\\"],
54+
"\r\nupdate && apt-get ", ["operator", "\\"],
55+
["comment", "#comment"],
56+
["comment", "#"],
57+
["operator", "\\"],
58+
["operator", "\\"],
59+
60+
"\r\n\r\n\r\ninstall git -y #not-a-comment ", ["operator", "\\"],
61+
"\r\nsomething"
62+
]],
63+
64+
["instruction", [
65+
["keyword", "RUN"], " echo hello ", ["operator", "\\"],
66+
["comment", "# comment"],
67+
"\r\nworld"
68+
]],
69+
70+
["comment", "# this is a comment-line"],
71+
["instruction", [
72+
["keyword", "RUN"],
73+
" echo hello"
74+
]],
75+
["instruction", [
76+
["keyword", "RUN"],
77+
" echo world"
78+
]],
79+
80+
["instruction", [
81+
["keyword", "RUN"],
82+
" echo ",
83+
["string", "\"\\\r\n hello\\\r\n world\""]
84+
]],
85+
86+
["instruction", [
87+
["keyword", "LABEL"],
88+
" multi.label1=",
89+
["string", "\"value1\""],
90+
["operator", "\\"],
91+
92+
"\r\n multi.label2=",
93+
["string", "\"value2\""],
94+
["operator", "\\"],
95+
96+
"\r\n other=",
97+
["string", "\"value3\""]
98+
]],
99+
100+
["instruction", [
101+
["keyword", "EXPOSE"],
102+
" 80/udp"
103+
]],
104+
105+
["instruction", [
106+
["keyword", "ENV"],
107+
" MY_NAME=",
108+
["string", "\"John Doe\""]
109+
]],
110+
111+
["instruction", [
112+
["keyword", "ENV"],
113+
" MY_NAME=",
114+
["string", "\"John Doe\""],
115+
" MY_DOG=Rex\\ The\\ Dog ",
116+
["operator", "\\"],
117+
118+
"\r\n MY_CAT=fluffy"
119+
]],
120+
121+
["instruction", [
122+
["keyword", "ADD"],
123+
" hom?.txt /mydir/"
124+
]],
125+
126+
["instruction", [
127+
["keyword", "ENTRYPOINT"],
128+
" [",
129+
["string", "\"executable\""],
130+
", ",
131+
["string", "\"param1\""],
132+
", ",
133+
["string", "\"param2\""],
134+
"]"
135+
]],
136+
137+
["instruction", [
138+
["keyword", "FROM"],
139+
" debian:stable"
140+
]],
141+
["instruction", [
142+
["keyword", "RUN"],
143+
" apt-get update && apt-get install -y --force-yes apache2"
144+
]],
145+
["instruction", [
146+
["keyword", "EXPOSE"],
147+
" 80 443"
148+
]],
149+
["instruction", [
150+
["keyword", "VOLUME"],
151+
" [",
152+
["string", "\"/var/www\""],
153+
", ",
154+
["string", "\"/var/log/apache2\""],
155+
", ",
156+
["string", "\"/etc/apache2\""],
157+
"]"
158+
]],
159+
["instruction", [
160+
["keyword", "ENTRYPOINT"],
161+
" [",
162+
["string", "\"/usr/sbin/apache2ctl\""],
163+
", ",
164+
["string", "\"-D\""],
165+
", ",
166+
["string", "\"FOREGROUND\""],
167+
"]"
168+
]],
169+
170+
["instruction", [
171+
["keyword", "ENTRYPOINT"],
172+
" [ ",
173+
["string", "\"/path/myprocess\""],
174+
", ",
175+
["operator", "\\"],
176+
177+
["string", "\"arg1\""],
178+
", ",
179+
["operator", "\\"],
180+
181+
["string", "\"arg2\""],
182+
["operator", "\\"],
183+
184+
"\r\n]"
185+
]]
186+
]

0 commit comments

Comments
 (0)