xml.go

 1package x
 2
 3import (
 4	. "github.com/alecthomas/chroma" // nolint
 5	"github.com/alecthomas/chroma/lexers/internal"
 6)
 7
 8// XML lexer.
 9var XML = internal.Register(MustNewLexer(
10	&Config{
11		Name:      "XML",
12		Aliases:   []string{"xml"},
13		Filenames: []string{"*.xml", "*.xsl", "*.rss", "*.xslt", "*.xsd", "*.wsdl", "*.wsf", "*.svg"},
14		MimeTypes: []string{"text/xml", "application/xml", "image/svg+xml", "application/rss+xml", "application/atom+xml"},
15		DotAll:    true,
16	},
17	Rules{
18		"root": {
19			{`[^<&]+`, Text, nil},
20			{`&\S*?;`, NameEntity, nil},
21			{`\<\!\[CDATA\[.*?\]\]\>`, CommentPreproc, nil},
22			{`<!--`, Comment, Push("comment")},
23			{`<\?.*?\?>`, CommentPreproc, nil},
24			{`<![^>]*>`, CommentPreproc, nil},
25			{`<\s*[\w:.-]+`, NameTag, Push("tag")},
26			{`<\s*/\s*[\w:.-]+\s*>`, NameTag, nil},
27		},
28		"comment": {
29			{`[^-]+`, Comment, nil},
30			{`-->`, Comment, Pop(1)},
31			{`-`, Comment, nil},
32		},
33		"tag": {
34			{`\s+`, Text, nil},
35			{`[\w.:-]+\s*=`, NameAttribute, Push("attr")},
36			{`/?\s*>`, NameTag, Pop(1)},
37		},
38		"attr": {
39			{`\s+`, Text, nil},
40			{`".*?"`, LiteralString, Pop(1)},
41			{`'.*?'`, LiteralString, Pop(1)},
42			{`[^\s>]+`, LiteralString, Pop(1)},
43		},
44	},
45))