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.

code_langauge.go 979B

123456789101112131415161718192021222324252627282930313233343536
  1. // Copyright 2020 The Gitea 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 analyze
  5. import (
  6. "path/filepath"
  7. "github.com/src-d/enry/v2"
  8. )
  9. // GetCodeLanguageWithCallback detects code language based on file name and content using callback
  10. func GetCodeLanguageWithCallback(filename string, contentFunc func() ([]byte, error)) string {
  11. if language, ok := enry.GetLanguageByExtension(filename); ok {
  12. return language
  13. }
  14. if language, ok := enry.GetLanguageByFilename(filename); ok {
  15. return language
  16. }
  17. content, err := contentFunc()
  18. if err != nil {
  19. return enry.OtherLanguage
  20. }
  21. return enry.GetLanguage(filepath.Base(filename), content)
  22. }
  23. // GetCodeLanguage detects code language based on file name and content
  24. func GetCodeLanguage(filename string, content []byte) string {
  25. return GetCodeLanguageWithCallback(filename, func() ([]byte, error) {
  26. return content, nil
  27. })
  28. }