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.

genshi.go 3.2KB

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