Bumps the go-modules-updates group with 11 updates in the /backend directory: | Package | From | To | | --- | --- | --- | | [github.com/PuerkitoBio/goquery](https://github.com/PuerkitoBio/goquery) | `1.10.2` | `1.11.0` | | [github.com/alecthomas/chroma/v2](https://github.com/alecthomas/chroma) | `2.15.0` | `2.20.0` | | [github.com/go-chi/chi/v5](https://github.com/go-chi/chi) | `5.2.1` | `5.2.3` | | [github.com/go-chi/cors](https://github.com/go-chi/cors) | `1.2.1` | `1.2.2` | | [github.com/go-pkgz/jrpc](https://github.com/go-pkgz/jrpc) | `0.3.1` | `0.4.0` | | [github.com/go-pkgz/lgr](https://github.com/go-pkgz/lgr) | `0.12.0` | `0.12.1` | | [github.com/go-pkgz/rest](https://github.com/go-pkgz/rest) | `1.20.3` | `1.20.4` | | [github.com/golang-jwt/jwt/v5](https://github.com/golang-jwt/jwt) | `5.2.2` | `5.3.0` | | [github.com/stretchr/testify](https://github.com/stretchr/testify) | `1.10.0` | `1.11.1` | | [go.etcd.io/bbolt](https://github.com/etcd-io/bbolt) | `1.4.0` | `1.4.3` | | [golang.org/x/image](https://github.com/golang/image) | `0.26.0` | `0.33.0` | Updates `github.com/PuerkitoBio/goquery` from 1.10.2 to 1.11.0 - [Release notes](https://github.com/PuerkitoBio/goquery/releases) - [Commits](https://github.com/PuerkitoBio/goquery/compare/v1.10.2...v1.11.0) Updates `github.com/alecthomas/chroma/v2` from 2.15.0 to 2.20.0 - [Release notes](https://github.com/alecthomas/chroma/releases) - [Commits](https://github.com/alecthomas/chroma/compare/v2.15.0...v2.20.0) Updates `github.com/go-chi/chi/v5` from 5.2.1 to 5.2.3 - [Release notes](https://github.com/go-chi/chi/releases) - [Changelog](https://github.com/go-chi/chi/blob/master/CHANGELOG.md) - [Commits](https://github.com/go-chi/chi/compare/v5.2.1...v5.2.3) Updates `github.com/go-chi/cors` from 1.2.1 to 1.2.2 - [Release notes](https://github.com/go-chi/cors/releases) - [Commits](https://github.com/go-chi/cors/compare/v1.2.1...v1.2.2) Updates `github.com/go-pkgz/jrpc` from 0.3.1 to 0.4.0 - [Release notes](https://github.com/go-pkgz/jrpc/releases) - [Commits](https://github.com/go-pkgz/jrpc/compare/v0.3.1...v0.4.0) Updates `github.com/go-pkgz/lgr` from 0.12.0 to 0.12.1 - [Release notes](https://github.com/go-pkgz/lgr/releases) - [Commits](https://github.com/go-pkgz/lgr/compare/v0.12.0...v0.12.1) Updates `github.com/go-pkgz/rest` from 1.20.3 to 1.20.4 - [Release notes](https://github.com/go-pkgz/rest/releases) - [Commits](https://github.com/go-pkgz/rest/compare/v1.20.3...v1.20.4) Updates `github.com/golang-jwt/jwt/v5` from 5.2.2 to 5.3.0 - [Release notes](https://github.com/golang-jwt/jwt/releases) - [Commits](https://github.com/golang-jwt/jwt/compare/v5.2.2...v5.3.0) Updates `github.com/stretchr/testify` from 1.10.0 to 1.11.1 - [Release notes](https://github.com/stretchr/testify/releases) - [Commits](https://github.com/stretchr/testify/compare/v1.10.0...v1.11.1) Updates `go.etcd.io/bbolt` from 1.4.0 to 1.4.3 - [Release notes](https://github.com/etcd-io/bbolt/releases) - [Commits](https://github.com/etcd-io/bbolt/compare/v1.4.0...v1.4.3) Updates `golang.org/x/image` from 0.26.0 to 0.33.0 - [Commits](https://github.com/golang/image/compare/v0.26.0...v0.33.0) --- updated-dependencies: - dependency-name: github.com/PuerkitoBio/goquery dependency-version: 1.11.0 dependency-type: direct:production update-type: version-update:semver-minor dependency-group: go-modules-updates - dependency-name: github.com/alecthomas/chroma/v2 dependency-version: 2.20.0 dependency-type: direct:production update-type: version-update:semver-minor dependency-group: go-modules-updates - dependency-name: github.com/go-chi/chi/v5 dependency-version: 5.2.3 dependency-type: direct:production update-type: version-update:semver-patch dependency-group: go-modules-updates - dependency-name: github.com/go-chi/cors dependency-version: 1.2.2 dependency-type: direct:production update-type: version-update:semver-patch dependency-group: go-modules-updates - dependency-name: github.com/go-pkgz/jrpc dependency-version: 0.4.0 dependency-type: direct:production update-type: version-update:semver-minor dependency-group: go-modules-updates - dependency-name: github.com/go-pkgz/lgr dependency-version: 0.12.1 dependency-type: direct:production update-type: version-update:semver-patch dependency-group: go-modules-updates - dependency-name: github.com/go-pkgz/rest dependency-version: 1.20.4 dependency-type: direct:production update-type: version-update:semver-patch dependency-group: go-modules-updates - dependency-name: github.com/golang-jwt/jwt/v5 dependency-version: 5.3.0 dependency-type: direct:production update-type: version-update:semver-minor dependency-group: go-modules-updates - dependency-name: github.com/stretchr/testify dependency-version: 1.11.1 dependency-type: direct:production update-type: version-update:semver-minor dependency-group: go-modules-updates - dependency-name: go.etcd.io/bbolt dependency-version: 1.4.3 dependency-type: direct:production update-type: version-update:semver-patch dependency-group: go-modules-updates - dependency-name: golang.org/x/image dependency-version: 0.33.0 dependency-type: direct:production update-type: version-update:semver-minor dependency-group: go-modules-updates ... Signed-off-by: dependabot[bot] <support@github.com>
229 lines
5.1 KiB
Go
229 lines
5.1 KiB
Go
package chroma
|
|
|
|
import (
|
|
"path/filepath"
|
|
"sort"
|
|
"strings"
|
|
)
|
|
|
|
var (
|
|
ignoredSuffixes = [...]string{
|
|
// Editor backups
|
|
"~", ".bak", ".old", ".orig",
|
|
// Debian and derivatives apt/dpkg/ucf backups
|
|
".dpkg-dist", ".dpkg-old", ".ucf-dist", ".ucf-new", ".ucf-old",
|
|
// Red Hat and derivatives rpm backups
|
|
".rpmnew", ".rpmorig", ".rpmsave",
|
|
// Build system input/template files
|
|
".in",
|
|
}
|
|
)
|
|
|
|
// LexerRegistry is a registry of Lexers.
|
|
type LexerRegistry struct {
|
|
Lexers Lexers
|
|
byName map[string]Lexer
|
|
byAlias map[string]Lexer
|
|
}
|
|
|
|
// NewLexerRegistry creates a new LexerRegistry of Lexers.
|
|
func NewLexerRegistry() *LexerRegistry {
|
|
return &LexerRegistry{
|
|
byName: map[string]Lexer{},
|
|
byAlias: map[string]Lexer{},
|
|
}
|
|
}
|
|
|
|
// Names of all lexers, optionally including aliases.
|
|
func (l *LexerRegistry) Names(withAliases bool) []string {
|
|
out := []string{}
|
|
for _, lexer := range l.Lexers {
|
|
config := lexer.Config()
|
|
out = append(out, config.Name)
|
|
if withAliases {
|
|
out = append(out, config.Aliases...)
|
|
}
|
|
}
|
|
sort.Strings(out)
|
|
return out
|
|
}
|
|
|
|
// Aliases of all the lexers, and skip those lexers who do not have any aliases,
|
|
// or show their name instead
|
|
func (l *LexerRegistry) Aliases(skipWithoutAliases bool) []string {
|
|
out := []string{}
|
|
for _, lexer := range l.Lexers {
|
|
config := lexer.Config()
|
|
if len(config.Aliases) == 0 {
|
|
if skipWithoutAliases {
|
|
continue
|
|
}
|
|
out = append(out, config.Name)
|
|
}
|
|
out = append(out, config.Aliases...)
|
|
}
|
|
sort.Strings(out)
|
|
return out
|
|
}
|
|
|
|
// Get a Lexer by name, alias or file extension.
|
|
func (l *LexerRegistry) Get(name string) Lexer {
|
|
if lexer := l.byName[name]; lexer != nil {
|
|
return lexer
|
|
}
|
|
if lexer := l.byAlias[name]; lexer != nil {
|
|
return lexer
|
|
}
|
|
if lexer := l.byName[strings.ToLower(name)]; lexer != nil {
|
|
return lexer
|
|
}
|
|
if lexer := l.byAlias[strings.ToLower(name)]; lexer != nil {
|
|
return lexer
|
|
}
|
|
|
|
candidates := PrioritisedLexers{}
|
|
// Try file extension.
|
|
if lexer := l.Match("filename." + name); lexer != nil {
|
|
candidates = append(candidates, lexer)
|
|
}
|
|
// Try exact filename.
|
|
if lexer := l.Match(name); lexer != nil {
|
|
candidates = append(candidates, lexer)
|
|
}
|
|
if len(candidates) == 0 {
|
|
return nil
|
|
}
|
|
sort.Sort(candidates)
|
|
return candidates[0]
|
|
}
|
|
|
|
// MatchMimeType attempts to find a lexer for the given MIME type.
|
|
func (l *LexerRegistry) MatchMimeType(mimeType string) Lexer {
|
|
matched := PrioritisedLexers{}
|
|
for _, l := range l.Lexers {
|
|
for _, lmt := range l.Config().MimeTypes {
|
|
if mimeType == lmt {
|
|
matched = append(matched, l)
|
|
}
|
|
}
|
|
}
|
|
if len(matched) != 0 {
|
|
sort.Sort(matched)
|
|
return matched[0]
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// Match returns the first lexer matching filename.
|
|
//
|
|
// Note that this iterates over all file patterns in all lexers, so is not fast.
|
|
func (l *LexerRegistry) Match(filename string) Lexer {
|
|
filename = filepath.Base(filename)
|
|
matched := PrioritisedLexers{}
|
|
// First, try primary filename matches.
|
|
for _, lexer := range l.Lexers {
|
|
config := lexer.Config()
|
|
for _, glob := range config.Filenames {
|
|
ok, err := filepath.Match(glob, filename)
|
|
if err != nil { // nolint
|
|
panic(err)
|
|
} else if ok {
|
|
matched = append(matched, lexer)
|
|
} else {
|
|
for _, suf := range &ignoredSuffixes {
|
|
ok, err := filepath.Match(glob+suf, filename)
|
|
if err != nil {
|
|
panic(err)
|
|
} else if ok {
|
|
matched = append(matched, lexer)
|
|
break
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
if len(matched) > 0 {
|
|
sort.Sort(matched)
|
|
return matched[0]
|
|
}
|
|
matched = nil
|
|
// Next, try filename aliases.
|
|
for _, lexer := range l.Lexers {
|
|
config := lexer.Config()
|
|
for _, glob := range config.AliasFilenames {
|
|
ok, err := filepath.Match(glob, filename)
|
|
if err != nil { // nolint
|
|
panic(err)
|
|
} else if ok {
|
|
matched = append(matched, lexer)
|
|
} else {
|
|
for _, suf := range &ignoredSuffixes {
|
|
ok, err := filepath.Match(glob+suf, filename)
|
|
if err != nil {
|
|
panic(err)
|
|
} else if ok {
|
|
matched = append(matched, lexer)
|
|
break
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
if len(matched) > 0 {
|
|
sort.Sort(matched)
|
|
return matched[0]
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// Analyse text content and return the "best" lexer..
|
|
func (l *LexerRegistry) Analyse(text string) Lexer {
|
|
var picked Lexer
|
|
highest := float32(0.0)
|
|
for _, lexer := range l.Lexers {
|
|
if analyser, ok := lexer.(Analyser); ok {
|
|
weight := analyser.AnalyseText(text)
|
|
if weight > highest {
|
|
picked = lexer
|
|
highest = weight
|
|
}
|
|
}
|
|
}
|
|
return picked
|
|
}
|
|
|
|
// Register a Lexer with the LexerRegistry. If the lexer is already registered
|
|
// it will be replaced.
|
|
func (l *LexerRegistry) Register(lexer Lexer) Lexer {
|
|
lexer.SetRegistry(l)
|
|
config := lexer.Config()
|
|
|
|
l.byName[config.Name] = lexer
|
|
l.byName[strings.ToLower(config.Name)] = lexer
|
|
|
|
for _, alias := range config.Aliases {
|
|
l.byAlias[alias] = lexer
|
|
l.byAlias[strings.ToLower(alias)] = lexer
|
|
}
|
|
|
|
l.Lexers = add(l.Lexers, lexer)
|
|
|
|
return lexer
|
|
}
|
|
|
|
// add adds a lexer to a slice of lexers if it doesn't already exist, or if found will replace it.
|
|
func add(lexers Lexers, lexer Lexer) Lexers {
|
|
for i, val := range lexers {
|
|
if val == nil {
|
|
continue
|
|
}
|
|
|
|
if val.Config().Name == lexer.Config().Name {
|
|
lexers[i] = lexer
|
|
return lexers
|
|
}
|
|
}
|
|
|
|
return append(lexers, lexer)
|
|
}
|