gitea源码

attribute.go 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. // Copyright 2025 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package attribute
  4. import (
  5. "strings"
  6. "code.gitea.io/gitea/modules/optional"
  7. )
  8. type Attribute string
  9. const (
  10. LinguistVendored = "linguist-vendored"
  11. LinguistGenerated = "linguist-generated"
  12. LinguistDocumentation = "linguist-documentation"
  13. LinguistDetectable = "linguist-detectable"
  14. LinguistLanguage = "linguist-language"
  15. GitlabLanguage = "gitlab-language"
  16. Lockable = "lockable"
  17. Filter = "filter"
  18. Diff = "diff"
  19. )
  20. var LinguistAttributes = []string{
  21. LinguistVendored,
  22. LinguistGenerated,
  23. LinguistDocumentation,
  24. LinguistDetectable,
  25. LinguistLanguage,
  26. GitlabLanguage,
  27. }
  28. func (a Attribute) IsUnspecified() bool {
  29. return a == "" || a == "unspecified"
  30. }
  31. func (a Attribute) ToString() optional.Option[string] {
  32. if !a.IsUnspecified() {
  33. return optional.Some(string(a))
  34. }
  35. return optional.None[string]()
  36. }
  37. // ToBool converts the attribute value to optional boolean: true if "set"/"true", false if "unset"/"false", none otherwise
  38. func (a Attribute) ToBool() optional.Option[bool] {
  39. switch a {
  40. case "set", "true":
  41. return optional.Some(true)
  42. case "unset", "false":
  43. return optional.Some(false)
  44. }
  45. return optional.None[bool]()
  46. }
  47. type Attributes struct {
  48. m map[string]Attribute
  49. }
  50. func NewAttributes() *Attributes {
  51. return &Attributes{m: make(map[string]Attribute)}
  52. }
  53. func (attrs *Attributes) Get(name string) Attribute {
  54. if value, has := attrs.m[name]; has {
  55. return value
  56. }
  57. return ""
  58. }
  59. func (attrs *Attributes) GetVendored() optional.Option[bool] {
  60. return attrs.Get(LinguistVendored).ToBool()
  61. }
  62. func (attrs *Attributes) GetGenerated() optional.Option[bool] {
  63. return attrs.Get(LinguistGenerated).ToBool()
  64. }
  65. func (attrs *Attributes) GetDocumentation() optional.Option[bool] {
  66. return attrs.Get(LinguistDocumentation).ToBool()
  67. }
  68. func (attrs *Attributes) GetDetectable() optional.Option[bool] {
  69. return attrs.Get(LinguistDetectable).ToBool()
  70. }
  71. func (attrs *Attributes) GetLinguistLanguage() optional.Option[string] {
  72. return attrs.Get(LinguistLanguage).ToString()
  73. }
  74. func (attrs *Attributes) GetGitlabLanguage() optional.Option[string] {
  75. attrStr := attrs.Get(GitlabLanguage).ToString()
  76. if attrStr.Has() {
  77. raw := attrStr.Value()
  78. // gitlab-language may have additional parameters after the language
  79. // ignore them and just use the main language
  80. // https://docs.gitlab.com/ee/user/project/highlighting.html#override-syntax-highlighting-for-a-file-type
  81. if idx := strings.IndexByte(raw, '?'); idx >= 0 {
  82. return optional.Some(raw[:idx])
  83. }
  84. }
  85. return attrStr
  86. }
  87. func (attrs *Attributes) GetLanguage() optional.Option[string] {
  88. // prefer linguist-language over gitlab-language
  89. // if linguist-language is not set, use gitlab-language
  90. // if both are not set, return none
  91. language := attrs.GetLinguistLanguage()
  92. if language.Value() == "" {
  93. language = attrs.GetGitlabLanguage()
  94. }
  95. return language
  96. }