You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

django.go 2.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. package d
  2. import (
  3. . "github.com/alecthomas/chroma" // nolint
  4. "github.com/alecthomas/chroma/lexers/internal"
  5. )
  6. // Django/Jinja lexer.
  7. var DjangoJinja = internal.Register(MustNewLexer(
  8. &Config{
  9. Name: "Django/Jinja",
  10. Aliases: []string{"django", "jinja"},
  11. Filenames: []string{},
  12. MimeTypes: []string{"application/x-django-templating", "application/x-jinja"},
  13. DotAll: true,
  14. },
  15. Rules{
  16. "root": {
  17. {`[^{]+`, Other, nil},
  18. {`\{\{`, CommentPreproc, Push("var")},
  19. {`\{[*#].*?[*#]\}`, Comment, nil},
  20. {`(\{%)(-?\s*)(comment)(\s*-?)(%\})(.*?)(\{%)(-?\s*)(endcomment)(\s*-?)(%\})`, ByGroups(CommentPreproc, Text, Keyword, Text, CommentPreproc, Comment, CommentPreproc, Text, Keyword, Text, CommentPreproc), nil},
  21. {`(\{%)(-?\s*)(raw)(\s*-?)(%\})(.*?)(\{%)(-?\s*)(endraw)(\s*-?)(%\})`, ByGroups(CommentPreproc, Text, Keyword, Text, CommentPreproc, Text, CommentPreproc, Text, Keyword, Text, CommentPreproc), nil},
  22. {`(\{%)(-?\s*)(filter)(\s+)([a-zA-Z_]\w*)`, ByGroups(CommentPreproc, Text, Keyword, Text, NameFunction), Push("block")},
  23. {`(\{%)(-?\s*)([a-zA-Z_]\w*)`, ByGroups(CommentPreproc, Text, Keyword), Push("block")},
  24. {`\{`, Other, nil},
  25. },
  26. "varnames": {
  27. {`(\|)(\s*)([a-zA-Z_]\w*)`, ByGroups(Operator, Text, NameFunction), nil},
  28. {`(is)(\s+)(not)?(\s+)?([a-zA-Z_]\w*)`, ByGroups(Keyword, Text, Keyword, Text, NameFunction), nil},
  29. {`(_|true|false|none|True|False|None)\b`, KeywordPseudo, nil},
  30. {`(in|as|reversed|recursive|not|and|or|is|if|else|import|with(?:(?:out)?\s*context)?|scoped|ignore\s+missing)\b`, Keyword, nil},
  31. {`(loop|block|super|forloop)\b`, NameBuiltin, nil},
  32. {`[a-zA-Z_][\w-]*`, NameVariable, nil},
  33. {`\.\w+`, NameVariable, nil},
  34. {`:?"(\\\\|\\"|[^"])*"`, LiteralStringDouble, nil},
  35. {`:?'(\\\\|\\'|[^'])*'`, LiteralStringSingle, nil},
  36. {`([{}()\[\]+\-*/,:~]|[><=]=?)`, Operator, nil},
  37. {`[0-9](\.[0-9]*)?(eE[+-][0-9])?[flFLdD]?|0[xX][0-9a-fA-F]+[Ll]?`, LiteralNumber, nil},
  38. },
  39. "var": {
  40. {`\s+`, Text, nil},
  41. {`(-?)(\}\})`, ByGroups(Text, CommentPreproc), Pop(1)},
  42. Include("varnames"),
  43. },
  44. "block": {
  45. {`\s+`, Text, nil},
  46. {`(-?)(%\})`, ByGroups(Text, CommentPreproc), Pop(1)},
  47. Include("varnames"),
  48. {`.`, Punctuation, nil},
  49. },
  50. },
  51. ))