File: svelte.go

package info (click to toggle)
golang-github-alecthomas-chroma-v2 2.12.0-1~bpo12%2B1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-backports
  • size: 9,172 kB
  • sloc: xml: 38,564; python: 422; javascript: 357; sh: 37; makefile: 36
file content (70 lines) | stat: -rw-r--r-- 1,998 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package lexers

import (
	. "github.com/alecthomas/chroma/v2" // nolint
)

// Svelte lexer.
var Svelte = Register(DelegatingLexer(HTML, MustNewLexer(
	&Config{
		Name:      "Svelte",
		Aliases:   []string{"svelte"},
		Filenames: []string{"*.svelte"},
		MimeTypes: []string{"application/x-svelte"},
		DotAll:    true,
	},
	svelteRules,
)))

func svelteRules() Rules {
	return Rules{
		"root": {
			// Let HTML handle the comments, including comments containing script and style tags
			{`<!--`, Other, Push("comment")},
			{
				// Highlight script and style tags based on lang attribute
				// and allow attributes besides lang
				`(<\s*(?:script|style).*?lang\s*=\s*['"])` +
					`(.+?)(['"].*?>)` +
					`(.+?)` +
					`(<\s*/\s*(?:script|style)\s*>)`,
				UsingByGroup(2, 4, Other, Other, Other, Other, Other),
				nil,
			},
			{
				// Make sure `{` is not inside script or style tags
				`(?<!<\s*(?:script|style)(?:(?!(?:script|style)\s*>).)*?)` +
					`{` +
					`(?!(?:(?!<\s*(?:script|style)).)*?(?:script|style)\s*>)`,
				Punctuation,
				Push("templates"),
			},
			// on:submit|preventDefault
			{`(?<=\s+on:\w+(?:\|\w+)*)\|(?=\w+)`, Operator, nil},
			{`.+?`, Other, nil},
		},
		"comment": {
			{`-->`, Other, Pop(1)},
			{`.+?`, Other, nil},
		},
		"templates": {
			{`}`, Punctuation, Pop(1)},
			// Let TypeScript handle strings and the curly braces inside them
			{`(?<!(?<!\\)\\)(['"` + "`])" + `.*?(?<!(?<!\\)\\)\1`, Using("TypeScript"), nil},
			// If there is another opening curly brace push to templates again
			{"{", Punctuation, Push("templates")},
			{`@(debug|html)\b`, Keyword, nil},
			{
				`(#await)(\s+)(\w+)(\s+)(then|catch)(\s+)(\w+)`,
				ByGroups(Keyword, Text, Using("TypeScript"), Text,
					Keyword, Text, Using("TypeScript"),
				),
				nil,
			},
			{`(#|/)(await|each|if|key)\b`, Keyword, nil},
			{`(:else)(\s+)(if)?\b`, ByGroups(Keyword, Text, Keyword), nil},
			{`:(catch|then)\b`, Keyword, nil},
			{`[^{}]+`, Using("TypeScript"), nil},
		},
	}
}