summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMaxime Vidori <maxime.vidori@gmail.com>2017-03-28 11:21:29 +0200
committerMaxime Vidori <maxime.vidori@gmail.com>2017-03-28 11:21:29 +0200
commit8d6dced7b7bf0854089c185679420861888abaf2 (patch)
tree7bbd9a7879c4c7532bb776ad566a13a7dbcee1e0
parent0db8e281af377923115b894703b2b8beb8f1e9d5 (diff)
downloadpygments-8d6dced7b7bf0854089c185679420861888abaf2.tar.gz
Update Dockerfile lexer
Add Dockerfiles new keyword in the lexer Improve lexer to parse json arrays on specific commands Handle line breaks as a bash syntax
-rw-r--r--pygments/lexers/configs.py22
-rw-r--r--tests/examplefiles/docker.docker33
2 files changed, 45 insertions, 10 deletions
diff --git a/pygments/lexers/configs.py b/pygments/lexers/configs.py
index c39b1a52..4af2adb6 100644
--- a/pygments/lexers/configs.py
+++ b/pygments/lexers/configs.py
@@ -15,6 +15,7 @@ from pygments.lexer import RegexLexer, default, words, bygroups, include, using
from pygments.token import Text, Comment, Operator, Keyword, Name, String, \
Number, Punctuation, Whitespace, Literal
from pygments.lexers.shell import BashLexer
+from pygments.lexers.data import JsonLexer
__all__ = ['IniLexer', 'RegeditLexer', 'PropertiesLexer', 'KconfigLexer',
'Cfengine3Lexer', 'ApacheConfLexer', 'SquidConfLexer',
@@ -539,20 +540,25 @@ class DockerLexer(RegexLexer):
filenames = ['Dockerfile', '*.docker']
mimetypes = ['text/x-dockerfile-config']
- _keywords = (r'(?:FROM|MAINTAINER|CMD|EXPOSE|ENV|ADD|ENTRYPOINT|'
- r'VOLUME|WORKDIR)')
-
+ _keywords = (r'(?:FROM|MAINTAINER|EXPOSE|WORKDIR|USER|STOPSIGNAL)')
+ _bash_keywords = (r'(?:RUN|CMD|ENTRYPOINT|ENV|ARG|LABEL|ADD|COPY)')
+ _lb = r'(?:\s*\\?\s*)' # dockerfile line break regex
flags = re.IGNORECASE | re.MULTILINE
tokens = {
'root': [
- (r'^(ONBUILD)(\s+)(%s)\b' % (_keywords,),
- bygroups(Name.Keyword, Whitespace, Keyword)),
- (r'^(%s)\b(.*)' % (_keywords,), bygroups(Keyword, String)),
(r'#.*', Comment),
- (r'RUN', Keyword), # Rest of line falls through
+ (r'(ONBUILD)(%s)' % (_lb,), bygroups(Keyword, using(BashLexer))),
+ (r'(HEALTHCHECK)((%s--\w+=\w+%s)*)' % (_lb, _lb),
+ bygroups(Keyword, using(BashLexer))),
+ (r'(VOLUME|ENTRYPOINT|CMD|SHELL)(%s)(\[.*?\])' % (_lb,),
+ bygroups(Keyword, using(BashLexer), using(JsonLexer))),
+ (r'(LABEL|ENV|ARG)((%s\w+=\w+%s)*)' % (_lb, _lb),
+ bygroups(Keyword, using(BashLexer))),
+ (r'(%s|VOLUME)\b(.*)' % (_keywords), bygroups(Keyword, String)),
+ (r'(%s)' % (_bash_keywords,), Keyword),
(r'(.*\\\n)*.+', using(BashLexer)),
- ],
+ ]
}
diff --git a/tests/examplefiles/docker.docker b/tests/examplefiles/docker.docker
index d65385b6..1ae3c3a1 100644
--- a/tests/examplefiles/docker.docker
+++ b/tests/examplefiles/docker.docker
@@ -1,5 +1,34 @@
-maintainer First O'Last
+FROM alpine:3.5
+MAINTAINER First O'Last
+# comment
run echo \
123 $bar
-# comment
+RUN apk --update add rsync dumb-init
+
+# Test env with both syntax
+ENV FOO = "BAR"
+ENV FOO \
+ "BAR"
+
+COPY foo "bar"
+COPY foo \
+ "bar"
+
+HEALTHCHECK \
+ --interval=5m --timeout=3s \
+ CMD curl -f http://localhost/ || exit 1
+
+# ONBUILD keyword, then with linebreak
+ONBUILD ADD . /app/src
+ONBUILD \
+ RUN echo 123 $bar
+
+# Potential JSON array parsing, mixed with linebreaks
+VOLUME \
+ /foo
+VOLUME \
+ ["/bar"]
+VOLUME ["/bar"]
+VOLUME /foo
+CMD ["foo", "bar"]