diff --git a/lexers/s/svelte.go b/lexers/s/svelte.go new file mode 100644 index 000000000..58ed27407 --- /dev/null +++ b/lexers/s/svelte.go @@ -0,0 +1,39 @@ +package s; + +import ( + . "github.com/alecthomas/chroma" // nolint + "github.com/alecthomas/chroma/lexers/h" + "github.com/alecthomas/chroma/lexers/internal" + "github.com/alecthomas/chroma/lexers/t" +) + +// Svelte lexer. +var Svelte = internal.Register(DelegatingLexer(h.HTML, MustNewLazyLexer( + &Config{ + Name: "Svelte", + Aliases: []string{"svelte"}, + Filenames: []string{"*.svelte"}, + MimeTypes: []string{"application/x-svelte"}, + DotAll: true, + }, + svelteRules, +))) + +func svelteRules() Rules { + return Rules { + "root": { + {`(<\s*script\s*lang\s*=\s*['"](?:ts|typescript)['"]\s*>)(.+?)(<\s*/\s*script\s*>)`, ByGroups(Other, Using(t.TypeScript), Other), nil}, + {`\{`, Punctuation, Push("templates")}, + {`[^{]`, Other, nil}, + }, + "templates": { + {`}`, Punctuation, Pop(1)}, + {`@(debug|html)\b`, Keyword, nil}, + {`(#|/)(await|each|if)\b`, Keyword, nil}, + {`(:else)(\s+)(if)?\b`, ByGroups(Keyword, Text, Keyword), nil}, + {`:(catch|then)\b`, Keyword, nil}, + {`then\b`, Keyword, nil}, + {`[^}]+`, Using(t.TypeScript), nil}, + }, + } +} diff --git a/lexers/testdata/svelte.actual b/lexers/testdata/svelte.actual new file mode 100644 index 000000000..05f85565d --- /dev/null +++ b/lexers/testdata/svelte.actual @@ -0,0 +1,33 @@ + + + + +

Hello {name}!

+Example image + + + + + diff --git a/lexers/testdata/svelte.expected b/lexers/testdata/svelte.expected new file mode 100644 index 000000000..ecb03b118 --- /dev/null +++ b/lexers/testdata/svelte.expected @@ -0,0 +1,211 @@ +[ + {"type":"Punctuation","value":"\u003c"}, + {"type":"NameTag","value":"script"}, + {"type":"Text","value":" "}, + {"type":"NameAttribute","value":"lang"}, + {"type":"Operator","value":"="}, + {"type":"LiteralString","value":"\"typescript\""}, + {"type":"Punctuation","value":"\u003e"}, + {"type":"Text","value":"\n\t"}, + {"type":"CommentSingle","value":"// Nested is defined elsewhere\n"}, + {"type":"Text","value":"\t"}, + {"type":"KeywordReserved","value":"import"}, + {"type":"Text","value":" "}, + {"type":"NameOther","value":"Nested"}, + {"type":"Text","value":" "}, + {"type":"KeywordReserved","value":"from"}, + {"type":"Text","value":" "}, + {"type":"LiteralStringSingle","value":"'./Nested.svelte'"}, + {"type":"Punctuation","value":";"}, + {"type":"Text","value":"\n\n\t"}, + {"type":"KeywordDeclaration","value":"let"}, + {"type":"Text","value":" "}, + {"type":"NameOther","value":"src"}, + {"type":"Text","value":" "}, + {"type":"Operator","value":"="}, + {"type":"Text","value":" "}, + {"type":"LiteralStringSingle","value":"'example.png'"}, + {"type":"Punctuation","value":";"}, + {"type":"Text","value":"\n\t"}, + {"type":"KeywordDeclaration","value":"let"}, + {"type":"Text","value":" "}, + {"type":"NameOther","value":"name"}, + {"type":"Text","value":" "}, + {"type":"Operator","value":"="}, + {"type":"Text","value":" "}, + {"type":"LiteralStringSingle","value":"'world'"}, + {"type":"Punctuation","value":";"}, + {"type":"Text","value":"\n\n\t"}, + {"type":"KeywordDeclaration","value":"let"}, + {"type":"Text","value":" "}, + {"type":"NameOther","value":"names"}, + {"type":"Text","value":" "}, + {"type":"Operator","value":"="}, + {"type":"Text","value":" "}, + {"type":"Punctuation","value":"["}, + {"type":"Text","value":"\n\t\t"}, + {"type":"Punctuation","value":"{"}, + {"type":"Text","value":" "}, + {"type":"NameOther","value":"id"}, + {"type":"Operator","value":":"}, + {"type":"Text","value":" "}, + {"type":"LiteralStringSingle","value":"'id0001'"}, + {"type":"Punctuation","value":","}, + {"type":"Text","value":" "}, + {"type":"NameOther","value":"name"}, + {"type":"Operator","value":":"}, + {"type":"Text","value":" "}, + {"type":"LiteralStringSingle","value":"'Name 1'"}, + {"type":"Text","value":" "}, + {"type":"Punctuation","value":"},"}, + {"type":"Text","value":"\n\t\t"}, + {"type":"Punctuation","value":"{"}, + {"type":"Text","value":" "}, + {"type":"NameOther","value":"id"}, + {"type":"Operator","value":":"}, + {"type":"Text","value":" "}, + {"type":"LiteralStringSingle","value":"'id0002'"}, + {"type":"Punctuation","value":","}, + {"type":"Text","value":" "}, + {"type":"NameOther","value":"name"}, + {"type":"Operator","value":":"}, + {"type":"Text","value":" "}, + {"type":"LiteralStringSingle","value":"'Name 2'"}, + {"type":"Text","value":" "}, + {"type":"Punctuation","value":"},"}, + {"type":"Text","value":"\n\t\t"}, + {"type":"Punctuation","value":"{"}, + {"type":"Text","value":" "}, + {"type":"NameOther","value":"id"}, + {"type":"Operator","value":":"}, + {"type":"Text","value":" "}, + {"type":"LiteralStringSingle","value":"'id0003'"}, + {"type":"Punctuation","value":","}, + {"type":"Text","value":" "}, + {"type":"NameOther","value":"name"}, + {"type":"Operator","value":":"}, + {"type":"Text","value":" "}, + {"type":"LiteralStringSingle","value":"'Name 3'"}, + {"type":"Text","value":" "}, + {"type":"Punctuation","value":"}"}, + {"type":"Text","value":"\n\t"}, + {"type":"Punctuation","value":"];"}, + {"type":"Text","value":"\n"}, + {"type":"Punctuation","value":"\u003c/"}, + {"type":"NameTag","value":"script"}, + {"type":"Punctuation","value":"\u003e"}, + {"type":"Text","value":"\n\n"}, + {"type":"Punctuation","value":"\u003c"}, + {"type":"NameTag","value":"style"}, + {"type":"Punctuation","value":"\u003e"}, + {"type":"Text","value":"\n\t"}, + {"type":"NameTag","value":"h1"}, + {"type":"Text","value":" "}, + {"type":"Punctuation","value":"{"}, + {"type":"Text","value":"\n\t\t"}, + {"type":"NameOther","value":"color"}, + {"type":"Text","value":": "}, + {"type":"KeywordType","value":"red"}, + {"type":"Punctuation","value":";"}, + {"type":"Text","value":"\n\t\t"}, + {"type":"NameOther","value":"font"}, + {"type":"Operator","value":"-"}, + {"type":"NameOther","value":"family"}, + {"type":"Text","value":": "}, + {"type":"KeywordType","value":"Arial"}, + {"type":"Punctuation","value":","}, + {"type":"Text","value":" "}, + {"type":"NameOther","value":"Helvetica"}, + {"type":"Punctuation","value":","}, + {"type":"Text","value":" "}, + {"type":"NameOther","value":"sans"}, + {"type":"Operator","value":"-"}, + {"type":"NameOther","value":"serif"}, + {"type":"Punctuation","value":";"}, + {"type":"Text","value":"\n\t\t"}, + {"type":"NameOther","value":"font"}, + {"type":"Operator","value":"-"}, + {"type":"NameOther","value":"size"}, + {"type":"Text","value":": "}, + {"type":"KeywordType","value":"2em"}, + {"type":"Punctuation","value":";"}, + {"type":"Text","value":"\n\t"}, + {"type":"Punctuation","value":"}"}, + {"type":"Text","value":"\n"}, + {"type":"Punctuation","value":"\u003c/"}, + {"type":"NameTag","value":"style"}, + {"type":"Punctuation","value":"\u003e"}, + {"type":"Text","value":"\n\n"}, + {"type":"Punctuation","value":"\u003c"}, + {"type":"NameTag","value":"h1"}, + {"type":"Punctuation","value":"\u003e"}, + {"type":"Text","value":"Hello "}, + {"type":"Punctuation","value":"{"}, + {"type":"NameOther","value":"name"}, + {"type":"Punctuation","value":"}"}, + {"type":"Text","value":"!"}, + {"type":"Punctuation","value":"\u003c/"}, + {"type":"NameTag","value":"h1"}, + {"type":"Punctuation","value":"\u003e"}, + {"type":"Text","value":"\n"}, + {"type":"Punctuation","value":"\u003c"}, + {"type":"NameTag","value":"img"}, + {"type":"Text","value":" "}, + {"type":"Punctuation","value":"{"}, + {"type":"NameOther","value":"src"}, + {"type":"Punctuation","value":"}"}, + {"type":"Text","value":" "}, + {"type":"NameAttribute","value":"alt"}, + {"type":"Operator","value":"="}, + {"type":"LiteralString","value":"\"Example image\""}, + {"type":"Punctuation","value":"\u003e"}, + {"type":"Text","value":"\n\n"}, + {"type":"Comment","value":"\u003c!-- import external component --\u003e"}, + {"type":"Text","value":"\n"}, + {"type":"Punctuation","value":"\u003c"}, + {"type":"NameTag","value":"Nested"}, + {"type":"Punctuation","value":"/\u003e"}, + {"type":"Text","value":"\n\n"}, + {"type":"Punctuation","value":"\u003c"}, + {"type":"NameTag","value":"ul"}, + {"type":"Punctuation","value":"\u003e"}, + {"type":"Text","value":"\n\t"}, + {"type":"Punctuation","value":"{"}, + {"type":"Keyword","value":"#each"}, + {"type":"Text","value":" "}, + {"type":"NameOther","value":"names"}, + {"type":"Text","value":" "}, + {"type":"KeywordReserved","value":"as"}, + {"type":"Text","value":" "}, + {"type":"Punctuation","value":"{"}, + {"type":"Text","value":" "}, + {"type":"NameOther","value":"id"}, + {"type":"Punctuation","value":","}, + {"type":"Text","value":" "}, + {"type":"NameOther","value":"name"}, + {"type":"Text","value":" "}, + {"type":"Punctuation","value":"}"}, + {"type":"Text","value":"}\n\t\t"}, + {"type":"Punctuation","value":"\u003c"}, + {"type":"NameTag","value":"li"}, + {"type":"Punctuation","value":"\u003e{"}, + {"type":"NameOther","value":"name"}, + {"type":"Punctuation","value":"}"}, + {"type":"Text","value":" ("}, + {"type":"Punctuation","value":"{"}, + {"type":"NameOther","value":"id"}, + {"type":"Punctuation","value":"}"}, + {"type":"Text","value":")"}, + {"type":"Punctuation","value":"\u003c/"}, + {"type":"NameTag","value":"li"}, + {"type":"Punctuation","value":"\u003e"}, + {"type":"Text","value":"\n\t"}, + {"type":"Punctuation","value":"{"}, + {"type":"Keyword","value":"/each"}, + {"type":"Punctuation","value":"}"}, + {"type":"Text","value":"\n"}, + {"type":"Punctuation","value":"\u003c/"}, + {"type":"NameTag","value":"ul"}, + {"type":"Punctuation","value":"\u003e"}, + {"type":"Text","value":"\n"} +]