1 package lexers
2
3 import (
4 . "github.com/alecthomas/chroma/v2"
5 )
6
7
8 var HTML = Register(MustNewLexer(
9 &Config{
10 Name: "HTML",
11 Aliases: []string{"html"},
12 Filenames: []string{"*.html", "*.htm", "*.xhtml", "*.xslt"},
13 MimeTypes: []string{"text/html", "application/xhtml+xml"},
14 NotMultiline: true,
15 DotAll: true,
16 CaseInsensitive: true,
17 },
18 htmlRules,
19 ))
20
21 func htmlRules() Rules {
22 return Rules{
23 "root": {
24 {`[^<&]+`, Text, nil},
25 {`&\S*?;`, NameEntity, nil},
26 {`\<\!\[CDATA\[.*?\]\]\>`, CommentPreproc, nil},
27 {`<!--`, Comment, Push("comment")},
28 {`<\?.*?\?>`, CommentPreproc, nil},
29 {`<![^>]*>`, CommentPreproc, nil},
30 {`(<)(\s*)(script)(\s*)`, ByGroups(Punctuation, Text, NameTag, Text), Push("script-content", "tag")},
31 {`(<)(\s*)(style)(\s*)`, ByGroups(Punctuation, Text, NameTag, Text), Push("style-content", "tag")},
32 {`(<)(\s*)([\w:.-]+)`, ByGroups(Punctuation, Text, NameTag), Push("tag")},
33 {`(<)(\s*)(/)(\s*)([\w:.-]+)(\s*)(>)`, ByGroups(Punctuation, Text, Punctuation, Text, NameTag, Text, Punctuation), nil},
34 },
35 "comment": {
36 {`[^-]+`, Comment, nil},
37 {`-->`, Comment, Pop(1)},
38 {`-`, Comment, nil},
39 },
40 "tag": {
41 {`\s+`, Text, nil},
42 {`([\w:-]+\s*)(=)(\s*)`, ByGroups(NameAttribute, Operator, Text), Push("attr")},
43 {`[\w:-]+`, NameAttribute, nil},
44 {`(/?)(\s*)(>)`, ByGroups(Punctuation, Text, Punctuation), Pop(1)},
45 },
46 "script-content": {
47 {`(<)(\s*)(/)(\s*)(script)(\s*)(>)`, ByGroups(Punctuation, Text, Punctuation, Text, NameTag, Text, Punctuation), Pop(1)},
48 {`.+?(?=<\s*/\s*script\s*>)`, Using("Javascript"), nil},
49 },
50 "style-content": {
51 {`(<)(\s*)(/)(\s*)(style)(\s*)(>)`, ByGroups(Punctuation, Text, Punctuation, Text, NameTag, Text, Punctuation), Pop(1)},
52 {`.+?(?=<\s*/\s*style\s*>)`, Using("CSS"), nil},
53 },
54 "attr": {
55 {`".*?"`, LiteralString, Pop(1)},
56 {`'.*?'`, LiteralString, Pop(1)},
57 {`[^\s>]+`, LiteralString, Pop(1)},
58 },
59 }
60 }
61
View as plain text