forked from forgejo/forgejo
#2103 Ability to map extensions for syntax highlighting in config
This commit is contained in:
parent
33a99d587a
commit
1e7e092992
5 changed files with 50 additions and 25 deletions
|
@ -7,6 +7,8 @@ package template
|
|||
import (
|
||||
"path"
|
||||
"strings"
|
||||
|
||||
"github.com/gogits/gogs/modules/setting"
|
||||
)
|
||||
|
||||
var (
|
||||
|
@ -16,13 +18,13 @@ var (
|
|||
"copying": true,
|
||||
}
|
||||
|
||||
// File names that are representing highlight class.
|
||||
// File names that are representing highlight classes.
|
||||
highlightFileNames = map[string]bool{
|
||||
"dockerfile": true,
|
||||
"makefile": true,
|
||||
}
|
||||
|
||||
// Extensions that are same as highlight class.
|
||||
// Extensions that are same as highlight classes.
|
||||
highlightExts = map[string]bool{
|
||||
".arm": true,
|
||||
".as": true,
|
||||
|
@ -57,8 +59,18 @@ var (
|
|||
".ts": true,
|
||||
".vb": true,
|
||||
}
|
||||
|
||||
// Extensions that are not same as highlight classes.
|
||||
highlightMapping = map[string]string{}
|
||||
)
|
||||
|
||||
func NewContext() {
|
||||
keys := setting.Cfg.Section("highlight.mapping").Keys()
|
||||
for i := range keys {
|
||||
highlightMapping[keys[i].Name()] = keys[i].Value()
|
||||
}
|
||||
}
|
||||
|
||||
// FileNameToHighlightClass returns the best match for highlight class name
|
||||
// based on the rule of highlight.js.
|
||||
func FileNameToHighlightClass(fname string) string {
|
||||
|
@ -76,5 +88,10 @@ func FileNameToHighlightClass(fname string) string {
|
|||
return ext[1:]
|
||||
}
|
||||
|
||||
name, ok := highlightMapping[ext]
|
||||
if ok {
|
||||
return name
|
||||
}
|
||||
|
||||
return ""
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue