refactor: extract Log and CommitInfo components
3 files changed, 47 insertions(+), 69 deletions(-)
M templates/commit.go → templates/commit.go
@@ -4,6 +4,7 @@ import ( "fmt" "github.com/bluekeyes/go-gitdiff/gitdiff" + "github.com/go-git/go-git/v5/plumbing/object" "go.alanpearce.eu/elgit/git" g "go.alanpearce.eu/gomponents" . "go.alanpearce.eu/gomponents/html"@@ -17,13 +18,7 @@ RenderNav(data), Main( Section(Class("commit"), Pre(g.Text(diff.Commit.Message)), - Div(Class("commit-info"), - g.Text(diff.Commit.Author.Name+" "), - A(Href(fmt.Sprintf("mailto:%s", diff.Commit.Author.Email)), - Class("commit-email"), - g.Text(diff.Commit.Author.Email)), - Div(g.Text(diff.Commit.Author.When.Format("Mon, 02 Jan 2006 15:04:05 -0700"))), - ), + CommitInfo(diff.Commit.Author), Div( Strong(g.Text("commit")), P(A(Href(fmt.Sprintf("/%s/commit/%s", data.Name, diff.Commit.This)),@@ -110,6 +105,18 @@ })..., ), ), }) +} + +func CommitInfo(author object.Signature) g.Node { + return Div(Class("commit-info"), + g.Text(author.Name+" "), + A( + Href(fmt.Sprintf("mailto:%s", author.Email)), + Class("commit-email"), + g.Text(author.Email), + ), + Div(g.Text(author.When.Format("Mon, 02 Jan 2006 15:04:05 -0700"))), + ) } func pluralise(n int) string {
M templates/log.go → templates/log.go
@@ -15,42 +15,38 @@ return Page(data, []g.Node{ RepoHeader(data), RenderNav(data), Main( - Div(Class("log"), - g.Map(commits, func(commit *git.CommitReference) g.Node { - return g.Group{ - Div( - Div( - A( - Href(fmt.Sprintf("/%s/commit/%s", data.Name, commit.Hash.String())), - Class("commit-hash"), - g.Text(commit.Hash.String()[:8]), - ), - g.Text(" "), - g.If( - commit.HasReference(), - g.Map(commit.References(), func(ref *plumbing.Reference) g.Node { - return A( - Href(fmt.Sprintf("/%s/tree/%s", data.Name, ref.Name().Short())), - Class("commit-hash"), - g.Text(ref.Name().Short()), - ) - }), - ), - ), - Pre(g.Text(commit.Message)), - ), - Div(Class("commit-info"), - g.Text(commit.Author.Name+" "), - A( - Href(fmt.Sprintf("mailto:%s", commit.Author.Email)), - Class("commit-email"), - g.Text(commit.Author.Email), - ), - Div(g.Text(commit.Author.When.Format("Mon, 02 Jan 2006 15:04:05 -0700"))), - ), - } - }), - ), + Log(data, commits), ), }) } + +func Log(data PageData, commits []*git.CommitReference) g.Node { + return Div(Class("log"), + g.Map(commits, func(commit *git.CommitReference) g.Node { + return g.Group{ + Div( + Div( + A( + Href(fmt.Sprintf("/%s/commit/%s", data.Name, commit.Hash.String())), + Class("commit-hash"), + g.Text(commit.Hash.String()[:8]), + ), + g.Text(" "), + g.If( + commit.HasReference(), + g.Map(commit.References(), func(ref *plumbing.Reference) g.Node { + return A( + Href(fmt.Sprintf("/%s/tree/%s", data.Name, ref.Name().Short())), + Class("commit-hash"), + g.Text(ref.Name().Short()), + ) + }), + ), + ), + Pre(g.Text(commit.Message)), + ), + CommitInfo(commit.Author), + } + }), + ) +}
M templates/repo.go → templates/repo.go
@@ -14,32 +14,7 @@ return Page(data, []g.Node{ RepoHeader(data), RenderNav(data), Main( - Div(Class("log"), - g.Map(commits, func(commit *git.CommitReference) g.Node { - return g.Group{ - Div( - Div( - A( - Href(fmt.Sprintf("/%s/commit/%s", data.Name, commit.Hash.String())), - Class("commit-hash"), - g.Text(commit.Hash.String()[:8]), - ), - ), - Pre(g.Text(commit.Message)), - ), - Div(Class("commit-info"), - g.Text(commit.Author.Name), - g.Text(" "), - A( - Href(fmt.Sprintf("mailto:%s", commit.Author.Email)), - Class("commit-email"), - g.Text(commit.Author.Email), - ), - Div(g.Text(commit.Author.When.Format("Mon, 02 Jan 2006 15:04:05 -0700"))), - ), - } - }), - ), + Log(data, commits), g.If(readme != "", Article(Class("readme"), g.Raw(readme)), ),