highlight.go 1.8 KB

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