tartrazine/utils.go

92 lines
2.3 KiB
Go
Raw Normal View History

2017-06-13 11:56:07 +00:00
package enry
2016-07-13 17:05:09 +00:00
import (
"bytes"
"path/filepath"
"strings"
"gopkg.in/src-d/enry.v1/data"
2016-07-13 17:05:09 +00:00
)
2017-05-29 08:05:16 +00:00
var (
auxiliaryLanguages = map[string]bool{
"Other": true, "XML": true, "YAML": true, "TOML": true, "INI": true,
"JSON": true, "TeX": true, "Public Key": true, "AsciiDoc": true,
"AGS Script": true, "VimL": true, "Diff": true, "CMake": true, "fish": true,
"Awk": true, "Graphviz (DOT)": true, "Markdown": true, "desktop": true,
"XSLT": true, "SQL": true, "RMarkdown": true, "IRC log": true,
"reStructuredText": true, "Twig": true, "CSS": true, "Batchfile": true,
"Text": true, "HTML+ERB": true, "HTML": true, "Gettext Catalog": true,
"Smarty": true, "Raw token data": true,
}
configurationLanguages = map[string]bool{
"XML": true, "JSON": true, "TOML": true, "YAML": true, "INI": true, "SQL": true,
}
)
// IsAuxiliaryLanguage returns whether or not lang is an auxiliary language.
2016-07-18 14:20:12 +00:00
func IsAuxiliaryLanguage(lang string) bool {
_, ok := auxiliaryLanguages[lang]
return ok
}
2017-05-29 08:05:16 +00:00
// IsConfiguration returns whether or not path is using a configuration language.
2016-07-13 20:21:18 +00:00
func IsConfiguration(path string) bool {
2017-06-12 11:42:20 +00:00
language, _ := GetLanguageByExtension(path)
_, is := configurationLanguages[language]
2016-07-13 20:21:18 +00:00
return is
}
2017-05-29 08:05:16 +00:00
// IsDotFile returns whether or not path has dot as a prefix.
2016-07-13 20:21:18 +00:00
func IsDotFile(path string) bool {
return strings.HasPrefix(filepath.Base(path), ".")
}
2017-05-29 08:05:16 +00:00
// IsVendor returns whether or not path is a vendor path.
2016-07-13 20:21:18 +00:00
func IsVendor(path string) bool {
return data.VendorMatchers.Match(path)
2016-07-13 20:21:18 +00:00
}
2017-05-29 08:05:16 +00:00
// IsDocumentation returns whether or not path is a documentation path.
2016-07-13 20:21:18 +00:00
func IsDocumentation(path string) bool {
return data.DocumentationMatchers.Match(path)
2016-07-13 20:21:18 +00:00
}
2017-07-11 10:27:48 +00:00
func IsImage(path string) bool {
extension := filepath.Ext(path)
2017-07-11 09:13:49 +00:00
if extension == ".png" || extension == ".jpg" || extension == ".jpeg" || extension == ".gif" {
return true
}
2017-07-11 10:27:48 +00:00
2017-07-11 09:13:49 +00:00
return false
}
2017-07-11 10:27:48 +00:00
func GetMimeType(path string, language string) string {
if mime, ok := data.LanguagesMime[language]; ok {
return mime
}
2017-07-11 10:27:48 +00:00
if IsImage(path) {
return "image/" + filepath.Ext(path)[1:]
}
return "text/plain"
2017-07-10 10:50:52 +00:00
}
2016-07-13 20:21:18 +00:00
const sniffLen = 8000
2017-05-29 08:05:16 +00:00
// IsBinary detects if data is a binary value based on:
// http://git.kernel.org/cgit/git/git.git/tree/xdiff-interface.c?id=HEAD#n198
2016-07-13 20:21:18 +00:00
func IsBinary(data []byte) bool {
if len(data) > sniffLen {
data = data[:sniffLen]
}
if bytes.IndexByte(data, byte(0)) == -1 {
return false
}
return true
}