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