highlight.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. // Copyright 2015 The Gogs Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package template
  5. import (
  6. "path"
  7. "strings"
  8. )
  9. var (
  10. // File name should ignore highlight.
  11. ignoreFileNames = map[string]bool{
  12. "license": true,
  13. "copying": true,
  14. }
  15. // File names that are representing highlight class.
  16. highlightFileNames = map[string]bool{
  17. "dockerfile": true,
  18. "makefile": true,
  19. }
  20. // Extensions that are same as highlight class.
  21. highlightExts = map[string]bool{
  22. ".arm": true,
  23. ".as": true,
  24. ".sh": true,
  25. ".cs": true,
  26. ".cpp": true,
  27. ".c": true,
  28. ".css": true,
  29. ".cmake": true,
  30. ".bat": true,
  31. ".dart": true,
  32. ".patch": true,
  33. ".elixir": true,
  34. ".erlang": true,
  35. ".go": true,
  36. ".html": true,
  37. ".xml": true,
  38. ".hs": true,
  39. ".ini": true,
  40. ".json": true,
  41. ".java": true,
  42. ".js": true,
  43. ".less": true,
  44. ".lua": true,
  45. ".php": true,
  46. ".py": true,
  47. ".rb": true,
  48. ".scss": true,
  49. ".sql": true,
  50. ".scala": true,
  51. ".swift": true,
  52. ".ts": true,
  53. ".vb": true,
  54. }
  55. )
  56. // FileNameToHighlightClass returns the best match for highlight class name
  57. // based on the rule of highlight.js.
  58. func FileNameToHighlightClass(fname string) string {
  59. fname = strings.ToLower(fname)
  60. if ignoreFileNames[fname] {
  61. return "nohighlight"
  62. }
  63. if highlightFileNames[fname] {
  64. return fname
  65. }
  66. ext := path.Ext(fname)
  67. if highlightExts[ext] {
  68. return ext[1:]
  69. }
  70. return ""
  71. }