Update go-enry dependency to v2.6.0 (#13861)

This commit is contained in:
Lauris BH 2020-12-05 20:31:18 +02:00 committed by GitHub
commit 4a510e08e4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
18 changed files with 32345 additions and 25977 deletions

View file

@ -22,6 +22,8 @@ var DefaultStrategies = []Strategy{
GetLanguagesByFilename,
GetLanguagesByShebang,
GetLanguagesByExtension,
GetLanguagesByXML,
GetLanguagesByManpage,
GetLanguagesByContent,
GetLanguagesByClassifier,
}
@ -328,15 +330,23 @@ func getInterpreter(data []byte) (interpreter string) {
return
}
func getFirstLine(content []byte) []byte {
nlpos := bytes.IndexByte(content, '\n')
if nlpos < 0 {
return content
func getFirstLines(content []byte, count int) []byte {
nlpos := -1
for ; count > 0; count-- {
pos := bytes.IndexByte(content[nlpos+1:], '\n')
if pos < 0 {
return content
}
nlpos += pos + 1
}
return content[:nlpos]
}
func getFirstLine(content []byte) []byte {
return getFirstLines(content, 1)
}
func hasShebang(line []byte) bool {
const shebang = `#!`
prefix := []byte(shebang)
@ -383,6 +393,49 @@ func GetLanguagesByExtension(filename string, _ []byte, _ []string) []string {
return nil
}
var (
manpageExtension = regex.MustCompile(`\.(?:[1-9](?:[a-z_]+[a-z_0-9]*)?|0p|n|man|mdoc)(?:\.in)?$`)
)
// GetLanguagesByManpage returns a slice of possible manpage languages for the given filename.
// It complies with the signature to be a Strategy type.
func GetLanguagesByManpage(filename string, _ []byte, _ []string) []string {
filename = strings.ToLower(filename)
// Check if matches Roff man page filenames
if manpageExtension.Match([]byte(filename)) {
return []string{
"Roff Manpage",
"Roff",
}
}
return nil
}
var (
xmlHeader = regex.MustCompile(`<?xml version=`)
)
// GetLanguagesByXML returns a slice of possible XML language for the given filename.
// It complies with the signature to be a Strategy type.
func GetLanguagesByXML(_ string, content []byte, candidates []string) []string {
if len(candidates) > 0 {
return candidates
}
header := getFirstLines(content, 2)
// Check if contains XML header
if xmlHeader.Match(header) {
return []string{
"XML",
}
}
return nil
}
func getDotIndexes(filename string) []int {
dots := make([]int, 0, 2)
for i, letter := range filename {