...

Source file src/github.com/alecthomas/chroma/lexers/s/svelte.go

Documentation: github.com/alecthomas/chroma/lexers/s

     1  package s
     2  
     3  import (
     4  	. "github.com/alecthomas/chroma" // nolint
     5  	"github.com/alecthomas/chroma/lexers/h"
     6  	"github.com/alecthomas/chroma/lexers/internal"
     7  	"github.com/alecthomas/chroma/lexers/t"
     8  )
     9  
    10  // Svelte lexer.
    11  var Svelte = internal.Register(DelegatingLexer(h.HTML, MustNewLazyLexer(
    12  	&Config{
    13  		Name:      "Svelte",
    14  		Aliases:   []string{"svelte"},
    15  		Filenames: []string{"*.svelte"},
    16  		MimeTypes: []string{"application/x-svelte"},
    17  		DotAll:    true,
    18  	},
    19  	svelteRules,
    20  )))
    21  
    22  func svelteRules() Rules {
    23  	return Rules{
    24  		"root": {
    25  			// Let HTML handle the comments, including comments containing script and style tags
    26  			{`<!--`, Other, Push("comment")},
    27  			{
    28  				// Highlight script and style tags based on lang attribute
    29  				// and allow attributes besides lang
    30  				`(<\s*(?:script|style).*?lang\s*=\s*['"])` +
    31  					`(.+?)(['"].*?>)` +
    32  					`(.+?)` +
    33  					`(<\s*/\s*(?:script|style)\s*>)`,
    34  				UsingByGroup(internal.Get, 2, 4, Other, Other, Other, Other, Other),
    35  				nil,
    36  			},
    37  			{
    38  				// Make sure `{` is not inside script or style tags
    39  				`(?<!<\s*(?:script|style)(?:(?!(?:script|style)\s*>).)*?)` +
    40  					`{` +
    41  					`(?!(?:(?!<\s*(?:script|style)).)*?(?:script|style)\s*>)`,
    42  				Punctuation,
    43  				Push("templates"),
    44  			},
    45  			// on:submit|preventDefault
    46  			{`(?<=\s+on:\w+(?:\|\w+)*)\|(?=\w+)`, Operator, nil},
    47  			{`.+?`, Other, nil},
    48  		},
    49  		"comment": {
    50  			{`-->`, Other, Pop(1)},
    51  			{`.+?`, Other, nil},
    52  		},
    53  		"templates": {
    54  			{`}`, Punctuation, Pop(1)},
    55  			// Let TypeScript handle strings and the curly braces inside them
    56  			{`(?<!(?<!\\)\\)(['"` + "`])" + `.*?(?<!(?<!\\)\\)\1`, Using(t.TypeScript), nil},
    57  			// If there is another opening curly brace push to templates again
    58  			{"{", Punctuation, Push("templates")},
    59  			{`@(debug|html)\b`, Keyword, nil},
    60  			{
    61  				`(#await)(\s+)(\w+)(\s+)(then|catch)(\s+)(\w+)`,
    62  				ByGroups(Keyword, Text, Using(t.TypeScript), Text,
    63  					Keyword, Text, Using(t.TypeScript),
    64  				),
    65  				nil,
    66  			},
    67  			{`(#|/)(await|each|if|key)\b`, Keyword, nil},
    68  			{`(:else)(\s+)(if)?\b`, ByGroups(Keyword, Text, Keyword), nil},
    69  			{`:(catch|then)\b`, Keyword, nil},
    70  			{`[^{}]+`, Using(t.TypeScript), nil},
    71  		},
    72  	}
    73  }
    74  

View as plain text