diff options
Diffstat (limited to 'modules')
-rw-r--r-- | modules/context/repo.go | 24 | ||||
-rw-r--r-- | modules/template/template.go | 10 |
2 files changed, 34 insertions, 0 deletions
diff --git a/modules/context/repo.go b/modules/context/repo.go index af188814..2f6a5de7 100644 --- a/modules/context/repo.go +++ b/modules/context/repo.go @@ -6,10 +6,12 @@ package context import ( "fmt" + "io/ioutil" "path" "strings" "github.com/Unknwon/com" + "gopkg.in/editorconfig/editorconfig-core-go.v1" "gopkg.in/macaron.v1" "github.com/gogits/git-module" @@ -69,6 +71,28 @@ func (r *Repository) HasAccess() bool { return r.AccessMode >= models.ACCESS_MODE_READ } +// GetEditorconfig returns the .editorconfig definition if found in the +// HEAD of the default repo branch. +func (r *Repository) GetEditorconfig() (*editorconfig.Editorconfig, error) { + commit, err := r.GitRepo.GetBranchCommit(r.Repository.DefaultBranch) + if err != nil { + return nil, err + } + treeEntry, err := commit.GetTreeEntryByPath(".editorconfig") + if err != nil { + return nil, err + } + reader, err := treeEntry.Blob().Data() + if err != nil { + return nil, err + } + data, err := ioutil.ReadAll(reader) + if err != nil { + return nil, err + } + return editorconfig.ParseBytes(data) +} + func RetrieveBaseRepo(ctx *Context, repo *models.Repository) { // Non-fork repository will not return error in this method. if err := repo.GetBaseRepo(); err != nil { diff --git a/modules/template/template.go b/modules/template/template.go index 6e9b15b9..1b5e0a11 100644 --- a/modules/template/template.go +++ b/modules/template/template.go @@ -17,6 +17,7 @@ import ( "golang.org/x/net/html/charset" "golang.org/x/text/transform" + "gopkg.in/editorconfig/editorconfig-core-go.v1" "github.com/gogits/gogs/models" "github.com/gogits/gogs/modules/base" @@ -109,6 +110,15 @@ func NewFuncMap() []template.FuncMap { mimeType := mime.TypeByExtension(filepath.Ext(filename)) return strings.HasPrefix(mimeType, "image/") }, + "TabSizeClass": func(ec *editorconfig.Editorconfig, filename string) string { + if ec != nil { + def := ec.GetDefinitionForFilename(filename) + if def.TabWidth > 0 { + return fmt.Sprintf("tab-size-%d", def.TabWidth) + } + } + return "tab-size-8" + }, }} } |