github.com/zkry/enry@v1.6.3/utils.go (about)

     1  package enry
     2  
     3  import (
     4  	"bytes"
     5  	"path/filepath"
     6  	"strings"
     7  
     8  	"gopkg.in/src-d/enry.v1/data"
     9  )
    10  
    11  var (
    12  	auxiliaryLanguages = map[string]bool{
    13  		"Other": true, "XML": true, "YAML": true, "TOML": true, "INI": true,
    14  		"JSON": true, "TeX": true, "Public Key": true, "AsciiDoc": true,
    15  		"AGS Script": true, "VimL": true, "Diff": true, "CMake": true, "fish": true,
    16  		"Awk": true, "Graphviz (DOT)": true, "Markdown": true, "desktop": true,
    17  		"XSLT": true, "SQL": true, "RMarkdown": true, "IRC log": true,
    18  		"reStructuredText": true, "Twig": true, "CSS": true, "Batchfile": true,
    19  		"Text": true, "HTML+ERB": true, "HTML": true, "Gettext Catalog": true,
    20  		"Smarty": true, "Raw token data": true,
    21  	}
    22  
    23  	configurationLanguages = map[string]bool{
    24  		"XML": true, "JSON": true, "TOML": true, "YAML": true, "INI": true, "SQL": true,
    25  	}
    26  )
    27  
    28  // IsAuxiliaryLanguage returns whether or not lang is an auxiliary language.
    29  func IsAuxiliaryLanguage(lang string) bool {
    30  	_, ok := auxiliaryLanguages[lang]
    31  	return ok
    32  }
    33  
    34  // IsConfiguration returns whether or not path is using a configuration language.
    35  func IsConfiguration(path string) bool {
    36  	language, _ := GetLanguageByExtension(path)
    37  	_, is := configurationLanguages[language]
    38  	return is
    39  }
    40  
    41  // IsDotFile returns whether or not path has dot as a prefix.
    42  func IsDotFile(path string) bool {
    43  	path = filepath.Clean(path)
    44  	base := filepath.Base(path)
    45  	return strings.HasPrefix(base, ".") && base != "." && base != ".."
    46  }
    47  
    48  // IsVendor returns whether or not path is a vendor path.
    49  func IsVendor(path string) bool {
    50  	return data.VendorMatchers.Match(path)
    51  }
    52  
    53  // IsDocumentation returns whether or not path is a documentation path.
    54  func IsDocumentation(path string) bool {
    55  	return data.DocumentationMatchers.Match(path)
    56  }
    57  
    58  func IsImage(path string) bool {
    59  	extension := filepath.Ext(path)
    60  	if extension == ".png" || extension == ".jpg" || extension == ".jpeg" || extension == ".gif" {
    61  		return true
    62  	}
    63  
    64  	return false
    65  }
    66  
    67  func GetMimeType(path string, language string) string {
    68  	if mime, ok := data.LanguagesMime[language]; ok {
    69  		return mime
    70  	}
    71  
    72  	if IsImage(path) {
    73  		return "image/" + filepath.Ext(path)[1:]
    74  	}
    75  
    76  	return "text/plain"
    77  }
    78  
    79  const sniffLen = 8000
    80  
    81  // IsBinary detects if data is a binary value based on:
    82  // http://git.kernel.org/cgit/git/git.git/tree/xdiff-interface.c?id=HEAD#n198
    83  func IsBinary(data []byte) bool {
    84  	if len(data) > sniffLen {
    85  		data = data[:sniffLen]
    86  	}
    87  
    88  	if bytes.IndexByte(data, byte(0)) == -1 {
    89  		return false
    90  	}
    91  
    92  	return true
    93  }
    94  
    95  // FileCount type stores language name and count of files belonging to the
    96  // language.
    97  type FileCount struct {
    98  	Name  string
    99  	Count int
   100  }
   101  
   102  // FileCountList type is a list of FileCounts.
   103  type FileCountList []FileCount
   104  
   105  func (fcl FileCountList) Len() int           { return len(fcl) }
   106  func (fcl FileCountList) Less(i, j int) bool { return fcl[i].Count < fcl[j].Count }
   107  func (fcl FileCountList) Swap(i, j int)      { fcl[i], fcl[j] = fcl[j], fcl[i] }