diff options
Diffstat (limited to 'pkg')
| -rw-r--r-- | pkg/handler/git/handler.go | 40 | ||||
| -rw-r--r-- | pkg/handler/router.go | 2 | ||||
| -rw-r--r-- | pkg/service/git.go | 36 | 
3 files changed, 62 insertions, 16 deletions
| diff --git a/pkg/handler/git/handler.go b/pkg/handler/git/handler.go index 28cc99e..7bdf372 100644 --- a/pkg/handler/git/handler.go +++ b/pkg/handler/git/handler.go @@ -2,8 +2,10 @@ package git  import (  	"bytes" +	"io"  	"log/slog"  	"net/http" +	"os"  	"path/filepath"  	"git.gabrielgio.me/cerrado/pkg/ext" @@ -15,11 +17,15 @@ import (  	"github.com/alecthomas/chroma/v2/styles"  	"github.com/go-git/go-git/v5/plumbing"  	"github.com/go-git/go-git/v5/plumbing/object" +	"github.com/gomarkdown/markdown" +	markdownhtml "github.com/gomarkdown/markdown/html" +	"github.com/gomarkdown/markdown/parser"  )  type (  	GitHandler struct {  		gitService gitService +		readmePath string  	}  	gitService interface { @@ -31,11 +37,16 @@ type (  		ListTags(name string) ([]*object.Tag, error)  		ListBranches(name string) ([]*plumbing.Reference, error)  	} + +	configurationRepository interface { +		GetRootReadme() string +	}  ) -func NewGitHandler(gitService gitService) *GitHandler { +func NewGitHandler(gitService gitService, confRepo configurationRepository) *GitHandler {  	return &GitHandler{  		gitService: gitService, +		readmePath: confRepo.GetRootReadme(),  	}  } @@ -46,7 +57,32 @@ func (g *GitHandler) List(w http.ResponseWriter, _ *http.Request) {  		return  	} -	gitList := &templates.GitListPage{repos} +	f, err := os.Open(g.readmePath) +	if err != nil { +		slog.Error("Error loading readme file", "error", err) +		return +	} + +	bs, err := io.ReadAll(f) +	if err != nil { +		slog.Error("Error reading readme file bytes", "error", err) +		return +	} + +	extensions := parser.CommonExtensions | parser.AutoHeadingIDs | parser.NoEmptyLineBeforeBlock +	p := parser.NewWithExtensions(extensions) +	doc := p.Parse(bs) + +	htmlFlag := markdownhtml.CommonFlags | markdownhtml.HrefTargetBlank +	opts := markdownhtml.RendererOptions{Flags: htmlFlag} +	renderer := markdownhtml.NewRenderer(opts) + +	bs = markdown.Render(doc, renderer) + +	gitList := &templates.GitListPage{ +		Respositories: repos, +		About:         bs, +	}  	templates.WritePageTemplate(w, gitList)  } diff --git a/pkg/handler/router.go b/pkg/handler/router.go index de5117c..bf13ad5 100644 --- a/pkg/handler/router.go +++ b/pkg/handler/router.go @@ -20,7 +20,7 @@ func MountHandler(  	configRepo *serverconfig.ConfigurationRepository,  ) (http.Handler, error) {  	var ( -		gitHandler   = git.NewGitHandler(gitService) +		gitHandler   = git.NewGitHandler(gitService, configRepo)  		aboutHandler = about.NewAboutHandler(configRepo)  		configHander = config.ConfigFile(configRepo)  	) diff --git a/pkg/service/git.go b/pkg/service/git.go index f886785..31a1cbb 100644 --- a/pkg/service/git.go +++ b/pkg/service/git.go @@ -1,21 +1,24 @@  package service  import ( +	"log/slog" +	"os"  	"path"  	"git.gabrielgio.me/cerrado/pkg/config"  	"git.gabrielgio.me/cerrado/pkg/git" +	"git.gabrielgio.me/cerrado/pkg/u"  	"github.com/go-git/go-git/v5/plumbing"  	"github.com/go-git/go-git/v5/plumbing/object"  )  type (  	Repository struct { -		Name              string -		Title             string -		LastCommitMessage string -		LastCommitDate    string -		Ref               string +		Name           string +		Title          string +		Description    string +		LastCommitDate string +		Ref            string  	}  	GitService struct { @@ -46,9 +49,6 @@ func (g *GitService) ListRepositories() ([]*Repository, error) {  		if err != nil {  			return nil, err  		} -		if err != nil { -			return nil, err -		}  		obj, err := repo.LastCommit()  		if err != nil { @@ -60,13 +60,23 @@ func (g *GitService) ListRepositories() ([]*Repository, error) {  			return nil, err  		} +		d := path.Join(r.Path, "description") +		description := "" +		if u.FileExist(d) { +			if b, err := os.ReadFile(d); err == nil { +				description = string(b) +			} else { +				slog.Error("Error loading description file", "err", err) +			} +		} +  		baseName := path.Base(r.Path)  		repos[i] = &Repository{ -			Name:              baseName, -			Title:             baseName, -			LastCommitMessage: obj.Message, -			LastCommitDate:    obj.Author.When.Format(timeFormat), -			Ref:               head.Name().Short(), +			Name:           baseName, +			Title:          baseName, +			Description:    description, +			LastCommitDate: obj.Author.When.Format(timeFormat), +			Ref:            head.Name().Short(),  		}  	} | 
