Skip to content

Commit

Permalink
Improve docker lexer
Browse files Browse the repository at this point in the history
* Allow comments between parts of multiline run instructions
* And systax highlighting for label keys and values
  • Loading branch information
Bond-009 committed Nov 27, 2024
1 parent 251423d commit a03c30c
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion lib/rouge/lexers/docker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class Docker < RegexLexer
mimetypes 'text/x-dockerfile-config'

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

start { @shell = Shell.new(@options) }
Expand Down Expand Up @@ -40,16 +40,37 @@ class Docker < RegexLexer
@shell.reset!
end

rule %r/^LABEL(\s+)/i do
token Keyword
push :label
end

rule %r/\w+/, Text
rule %r/[^\w]+/, Text
rule %r/./, Text
end

state :run do
rule %r/\n/, Text, :pop!
rule %r/^\s*#.*\n/, Comment
rule %r/\\./m, Str::Escape
rule(/(\\.|[^\n\\])+/) { delegate @shell }
end

state :label do
rule %r/\n/, Text, :pop!
rule %r/^\s*#.*\n/, Comment
rule %r/\s*\\./m, Str::Escape
rule %r/(\s*(?:\S+|"[^"]+"))(=)/ do
groups Name::Property, Punctuation
push :value
end
end

state :value do
rule %r/\n/, Text, :pop!
rule %r/(".*?")|('.*?')/m, Str, :pop!
end
end
end
end

0 comments on commit a03c30c

Please sign in to comment.