genshi.go

  1package lexers
  2
  3import (
  4	. "github.com/alecthomas/chroma/v2" // nolint
  5)
  6
  7// Genshi Text lexer.
  8var GenshiText = Register(MustNewLexer(
  9	&Config{
 10		Name:      "Genshi Text",
 11		Aliases:   []string{"genshitext"},
 12		Filenames: []string{},
 13		MimeTypes: []string{"application/x-genshi-text", "text/x-genshi"},
 14	},
 15	genshiTextRules,
 16))
 17
 18func genshiTextRules() Rules {
 19	return Rules{
 20		"root": {
 21			{`[^#$\s]+`, Other, nil},
 22			{`^(\s*)(##.*)$`, ByGroups(Text, Comment), nil},
 23			{`^(\s*)(#)`, ByGroups(Text, CommentPreproc), Push("directive")},
 24			Include("variable"),
 25			{`[#$\s]`, Other, nil},
 26		},
 27		"directive": {
 28			{`\n`, Text, Pop(1)},
 29			{`(?:def|for|if)\s+.*`, Using("Python"), Pop(1)},
 30			{`(choose|when|with)([^\S\n]+)(.*)`, ByGroups(Keyword, Text, Using("Python")), Pop(1)},
 31			{`(choose|otherwise)\b`, Keyword, Pop(1)},
 32			{`(end\w*)([^\S\n]*)(.*)`, ByGroups(Keyword, Text, Comment), Pop(1)},
 33		},
 34		"variable": {
 35			{`(?<!\$)(\$\{)(.+?)(\})`, ByGroups(CommentPreproc, Using("Python"), CommentPreproc), nil},
 36			{`(?<!\$)(\$)([a-zA-Z_][\w.]*)`, NameVariable, nil},
 37		},
 38	}
 39}
 40
 41// Html+Genshi lexer.
 42var GenshiHTMLTemplate = Register(MustNewLexer(
 43	&Config{
 44		Name:         "Genshi HTML",
 45		Aliases:      []string{"html+genshi", "html+kid"},
 46		Filenames:    []string{},
 47		MimeTypes:    []string{"text/html+genshi"},
 48		NotMultiline: true,
 49		DotAll:       true,
 50	},
 51	genshiMarkupRules,
 52))
 53
 54// Genshi lexer.
 55var Genshi = Register(MustNewLexer(
 56	&Config{
 57		Name:         "Genshi",
 58		Aliases:      []string{"genshi", "kid", "xml+genshi", "xml+kid"},
 59		Filenames:    []string{"*.kid"},
 60		MimeTypes:    []string{"application/x-genshi", "application/x-kid"},
 61		NotMultiline: true,
 62		DotAll:       true,
 63	},
 64	genshiMarkupRules,
 65))
 66
 67func genshiMarkupRules() Rules {
 68	return Rules{
 69		"root": {
 70			{`[^<$]+`, Other, nil},
 71			{`(<\?python)(.*?)(\?>)`, ByGroups(CommentPreproc, Using("Python"), CommentPreproc), nil},
 72			{`<\s*(script|style)\s*.*?>.*?<\s*/\1\s*>`, Other, nil},
 73			{`<\s*py:[a-zA-Z0-9]+`, NameTag, Push("pytag")},
 74			{`<\s*[a-zA-Z0-9:.]+`, NameTag, Push("tag")},
 75			Include("variable"),
 76			{`[<$]`, Other, nil},
 77		},
 78		"pytag": {
 79			{`\s+`, Text, nil},
 80			{`[\w:-]+\s*=`, NameAttribute, Push("pyattr")},
 81			{`/?\s*>`, NameTag, Pop(1)},
 82		},
 83		"pyattr": {
 84			{`(")(.*?)(")`, ByGroups(LiteralString, Using("Python"), LiteralString), Pop(1)},
 85			{`(')(.*?)(')`, ByGroups(LiteralString, Using("Python"), LiteralString), Pop(1)},
 86			{`[^\s>]+`, LiteralString, Pop(1)},
 87		},
 88		"tag": {
 89			{`\s+`, Text, nil},
 90			{`py:[\w-]+\s*=`, NameAttribute, Push("pyattr")},
 91			{`[\w:-]+\s*=`, NameAttribute, Push("attr")},
 92			{`/?\s*>`, NameTag, Pop(1)},
 93		},
 94		"attr": {
 95			{`"`, LiteralString, Push("attr-dstring")},
 96			{`'`, LiteralString, Push("attr-sstring")},
 97			{`[^\s>]*`, LiteralString, Pop(1)},
 98		},
 99		"attr-dstring": {
100			{`"`, LiteralString, Pop(1)},
101			Include("strings"),
102			{`'`, LiteralString, nil},
103		},
104		"attr-sstring": {
105			{`'`, LiteralString, Pop(1)},
106			Include("strings"),
107			{`'`, LiteralString, nil},
108		},
109		"strings": {
110			{`[^"'$]+`, LiteralString, nil},
111			Include("variable"),
112		},
113		"variable": {
114			{`(?<!\$)(\$\{)(.+?)(\})`, ByGroups(CommentPreproc, Using("Python"), CommentPreproc), nil},
115			{`(?<!\$)(\$)([a-zA-Z_][\w\.]*)`, NameVariable, nil},
116		},
117	}
118}