highlight.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. // Copyright 2015 - Present, The Gogs Authors. All rights reserved.
  2. // Copyright 2018 - Present, Gitote. All rights reserved.
  3. //
  4. // This source code is licensed under the MIT license found in the
  5. // LICENSE file in the root directory of this source tree.
  6. package highlight
  7. import (
  8. "gitote/gitote/pkg/setting"
  9. "path"
  10. "strings"
  11. )
  12. var (
  13. // File name should ignore highlight.
  14. ignoreFileNames = map[string]bool{
  15. "license": true,
  16. "copying": true,
  17. }
  18. // File names that are representing highlight classes.
  19. highlightFileNames = map[string]bool{
  20. "cmakelists.txt": true,
  21. "dockerfile": true,
  22. "makefile": true,
  23. }
  24. // Extensions that are same as highlight classes.
  25. highlightExts = map[string]bool{
  26. ".arm": true,
  27. ".as": true,
  28. ".sh": true,
  29. ".cs": true,
  30. ".cpp": true,
  31. ".c": true,
  32. ".css": true,
  33. ".cmake": true,
  34. ".bat": true,
  35. ".dart": true,
  36. ".patch": true,
  37. ".elixir": true,
  38. ".erlang": true,
  39. ".go": true,
  40. ".html": true,
  41. ".xml": true,
  42. ".hs": true,
  43. ".ini": true,
  44. ".json": true,
  45. ".java": true,
  46. ".js": true,
  47. ".less": true,
  48. ".lua": true,
  49. ".php": true,
  50. ".py": true,
  51. ".rb": true,
  52. ".scss": true,
  53. ".sql": true,
  54. ".scala": true,
  55. ".swift": true,
  56. ".ts": true,
  57. ".vb": true,
  58. }
  59. // Extensions that are not same as highlight classes.
  60. highlightMapping = map[string]string{
  61. ".txt": "nohighlight",
  62. }
  63. )
  64. // NewContext for highlight.mapping
  65. func NewContext() {
  66. keys := setting.Cfg.Section("highlight.mapping").Keys()
  67. for i := range keys {
  68. highlightMapping[keys[i].Name()] = keys[i].Value()
  69. }
  70. }
  71. // FileNameToHighlightClass returns the best match for highlight class name
  72. // based on the rule of highlight.js.
  73. func FileNameToHighlightClass(fname string) string {
  74. fname = strings.ToLower(fname)
  75. if ignoreFileNames[fname] {
  76. return "nohighlight"
  77. }
  78. if highlightFileNames[fname] {
  79. return fname
  80. }
  81. ext := path.Ext(fname)
  82. if highlightExts[ext] {
  83. return ext[1:]
  84. }
  85. name, ok := highlightMapping[ext]
  86. if ok {
  87. return name
  88. }
  89. return ""
  90. }