Skip to content

Commit a03c30c

Browse files
committed
Improve docker lexer
* Allow comments between parts of multiline run instructions * And systax highlighting for label keys and values
1 parent 251423d commit a03c30c

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

lib/rouge/lexers/docker.rb

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class Docker < RegexLexer
1212
mimetypes 'text/x-dockerfile-config'
1313

1414
KEYWORDS = %w(
15-
FROM MAINTAINER CMD LABEL EXPOSE ENV ADD COPY ENTRYPOINT VOLUME USER WORKDIR ARG STOPSIGNAL HEALTHCHECK SHELL
15+
FROM MAINTAINER CMD EXPOSE ENV ADD COPY ENTRYPOINT VOLUME USER WORKDIR ARG STOPSIGNAL HEALTHCHECK SHELL
1616
).join('|')
1717

1818
start { @shell = Shell.new(@options) }
@@ -40,16 +40,37 @@ class Docker < RegexLexer
4040
@shell.reset!
4141
end
4242

43+
rule %r/^LABEL(\s+)/i do
44+
token Keyword
45+
push :label
46+
end
47+
4348
rule %r/\w+/, Text
4449
rule %r/[^\w]+/, Text
4550
rule %r/./, Text
4651
end
4752

4853
state :run do
4954
rule %r/\n/, Text, :pop!
55+
rule %r/^\s*#.*\n/, Comment
5056
rule %r/\\./m, Str::Escape
5157
rule(/(\\.|[^\n\\])+/) { delegate @shell }
5258
end
59+
60+
state :label do
61+
rule %r/\n/, Text, :pop!
62+
rule %r/^\s*#.*\n/, Comment
63+
rule %r/\s*\\./m, Str::Escape
64+
rule %r/(\s*(?:\S+|"[^"]+"))(=)/ do
65+
groups Name::Property, Punctuation
66+
push :value
67+
end
68+
end
69+
70+
state :value do
71+
rule %r/\n/, Text, :pop!
72+
rule %r/(".*?")|('.*?')/m, Str, :pop!
73+
end
5374
end
5475
end
5576
end

0 commit comments

Comments
 (0)