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.

151 lines
3.4 KiB

  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 highlight
  5. import (
  6. "path"
  7. "strings"
  8. "code.gitea.io/gitea/modules/setting"
  9. )
  10. var (
  11. // File name should ignore highlight.
  12. ignoreFileNames = map[string]bool{
  13. "license": true,
  14. "copying": true,
  15. }
  16. // File names that are representing highlight classes.
  17. highlightFileNames = map[string]string{
  18. "dockerfile": "dockerfile",
  19. "makefile": "makefile",
  20. "gnumakefile": "makefile",
  21. "cmakelists.txt": "cmake",
  22. }
  23. // Extensions that are same as highlight classes.
  24. // See hljs.listLanguages() for list of language names.
  25. highlightExts = map[string]struct{}{
  26. ".applescript": {},
  27. ".arm": {},
  28. ".as": {},
  29. ".bash": {},
  30. ".bat": {},
  31. ".c": {},
  32. ".cmake": {},
  33. ".cpp": {},
  34. ".cs": {},
  35. ".css": {},
  36. ".dart": {},
  37. ".diff": {},
  38. ".django": {},
  39. ".go": {},
  40. ".gradle": {},
  41. ".groovy": {},
  42. ".haml": {},
  43. ".handlebars": {},
  44. ".html": {},
  45. ".ini": {},
  46. ".java": {},
  47. ".json": {},
  48. ".less": {},
  49. ".lua": {},
  50. ".php": {},
  51. ".scala": {},
  52. ".scss": {},
  53. ".sql": {},
  54. ".swift": {},
  55. ".ts": {},
  56. ".xml": {},
  57. ".yaml": {},
  58. }
  59. // Extensions that are not same as highlight classes.
  60. highlightMapping = map[string]string{
  61. ".ahk": "autohotkey",
  62. ".crmsh": "crmsh",
  63. ".dash": "shell",
  64. ".erl": "erlang",
  65. ".escript": "erlang",
  66. ".ex": "elixir",
  67. ".exs": "elixir",
  68. ".f": "fortran",
  69. ".f77": "fortran",
  70. ".f90": "fortran",
  71. ".f95": "fortran",
  72. ".feature": "gherkin",
  73. ".fish": "shell",
  74. ".for": "fortran",
  75. ".hbs": "handlebars",
  76. ".hs": "haskell",
  77. ".hx": "haxe",
  78. ".js": "javascript",
  79. ".jsx": "javascript",
  80. ".ksh": "shell",
  81. ".kt": "kotlin",
  82. ".l": "ocaml",
  83. ".ls": "livescript",
  84. ".md": "markdown",
  85. ".mjs": "javascript",
  86. ".mli": "ocaml",
  87. ".mll": "ocaml",
  88. ".mly": "ocaml",
  89. ".patch": "diff",
  90. ".pl": "perl",
  91. ".pm": "perl",
  92. ".ps1": "powershell",
  93. ".psd1": "powershell",
  94. ".psm1": "powershell",
  95. ".py": "python",
  96. ".pyw": "python",
  97. ".rb": "ruby",
  98. ".rs": "rust",
  99. ".scpt": "applescript",
  100. ".scptd": "applescript",
  101. ".sh": "bash",
  102. ".tcsh": "shell",
  103. ".ts": "typescript",
  104. ".tsx": "typescript",
  105. ".txt": "plaintext",
  106. ".vb": "vbnet",
  107. ".vbs": "vbscript",
  108. ".yml": "yaml",
  109. ".zsh": "shell",
  110. }
  111. )
  112. // NewContext loads highlight map
  113. func NewContext() {
  114. keys := setting.Cfg.Section("highlight.mapping").Keys()
  115. for i := range keys {
  116. highlightMapping[keys[i].Name()] = keys[i].Value()
  117. }
  118. }
  119. // FileNameToHighlightClass returns the best match for highlight class name
  120. // based on the rule of highlight.js.
  121. func FileNameToHighlightClass(fname string) string {
  122. fname = strings.ToLower(fname)
  123. if ignoreFileNames[fname] {
  124. return "nohighlight"
  125. }
  126. if name, ok := highlightFileNames[fname]; ok {
  127. return name
  128. }
  129. ext := path.Ext(fname)
  130. if _, ok := highlightExts[ext]; ok {
  131. return ext[1:]
  132. }
  133. name, ok := highlightMapping[ext]
  134. if ok {
  135. return name
  136. }
  137. return ""
  138. }