package templates
import (
"embed"
_ "embed"
. "html/template"
"path/filepath"
"time"
"mokhan.ca/xlgmokha/gitmal/internal/git"
)
var funcs = FuncMap{
"BaseName": filepath.Base,
"FormatDate": func(date time.Time) HTML {
return HTML(date.Format(`2006-01-02 15:04:05`))
},
"ShortHash": func(hash string) string {
return hash[:7]
},
"FileTreeParams": func(node []*FileTree) FileTreeParams {
return FileTreeParams{Nodes: node}
},
}
//go:embed css/layout.css
var LayoutCSS string
//go:embed css/markdown_light.css
var CSSMarkdownLight string
//go:embed css/markdown_dark.css
var CSSMarkdownDark string
//go:embed css/syntax_light.css
var CSSSyntaxLight string
//go:embed css/syntax_dark.css
var CSSSyntaxDark string
//go:embed layout.gohtml header.gohtml file_tree.gohtml svg.gohtml
var layoutContent embed.FS
var layout = Must(New("layout").Funcs(funcs).ParseFS(layoutContent, "*.gohtml"))
//go:embed blob.gohtml
var blobContent string
var BlobTemplate = Must(Must(layout.Clone()).Parse(blobContent))
//go:embed markdown.gohtml
var markdownContent string
var MarkdownTemplate = Must(Must(layout.Clone()).Parse(markdownContent))
//go:embed list.gohtml
var listContent string
var ListTemplate = Must(Must(layout.Clone()).Parse(listContent))
//go:embed branches.gohtml
var branchesContent string
var BranchesTemplate = Must(Must(layout.Clone()).Parse(branchesContent))
//go:embed tags.gohtml
var tagsContent string
var TagsTemplate = Must(Must(layout.Clone()).Parse(tagsContent))
//go:embed commits_list.gohtml
var commitsListContent string
var CommitsListTemplate = Must(Must(layout.Clone()).Parse(commitsListContent))
//go:embed commit.gohtml
var commitContent string
var CommitTemplate = Must(Must(layout.Clone()).Parse(commitContent))
type LayoutParams struct {
Title string
Name string
RootHref string
CurrentRefDir string
Selected string
NeedsMarkdownCSS bool
NeedsSyntaxCSS bool
}
type HeaderParams struct {
Ref git.Ref
Header string
Breadcrumbs []Breadcrumb
}
type Breadcrumb struct {
Name string
Href string
IsDir bool
}
type BlobParams struct {
LayoutParams
HeaderParams
Blob git.Blob
IsImage bool
IsBinary bool
Content HTML
}
type MarkdownParams struct {
LayoutParams
HeaderParams
Blob git.Blob
Content HTML
}
type ListParams struct {
LayoutParams
HeaderParams
Ref git.Ref
ParentHref string
Dirs []ListEntry
Files []ListEntry
Readme HTML
}
type ListEntry struct {
Name string
Href string
IsDir bool
Mode string
Size string
}
type BranchesParams struct {
LayoutParams
Branches []BranchEntry
}
type BranchEntry struct {
Name string
Href string
IsDefault bool
CommitsHref string
}
type TagsParams struct {
LayoutParams
Tags []TagEntry
}
type TagEntry struct {
git.Tag
CompareHref string
}
type Pagination struct {
Page int
TotalPages int
PrevHref string
NextHref string
FirstHref string
LastHref string
}
type CommitsListParams struct {
LayoutParams
HeaderParams
Ref git.Ref
Commits []git.Commit
Page Pagination
}
type CommitParams struct {
LayoutParams
Commit git.Commit
FileTree []*FileTree
FileViews []FileView
}
type FileTreeParams struct {
Nodes []*FileTree
}
// FileTree represents a directory or file in a commit's changed files tree.
// For directories, IsDir is true and Children contains nested nodes.
// For files, status flags indicate the type of change.
type FileTree struct {
Name string
Path string
IsDir bool
Children []*FileTree
// File status (applicable when IsDir == false)
IsNew bool
IsDelete bool
IsRename bool
OldName string
NewName string
// Anchor id for this file (empty for directories)
Anchor string
}
// FileView represents a single file section on the commit page with its
// pre-rendered HTML diff and metadata used by the template.
type FileView struct {
Path string
OldName string
NewName string
IsNew bool
IsDelete bool
IsRename bool
IsBinary bool
HasChanges bool
HTML HTML // pre-rendered HTML for diff of this file
}
type CompareParams struct {
LayoutParams
Base string
Head string
Commits []git.Commit
FileTree []*FileTree
FileViews []FileView
}
//go:embed compare.gohtml
var compareContent string
var CompareTemplate = Must(Must(layout.Clone()).Parse(compareContent))