main
  1package templates
  2
  3import (
  4	"embed"
  5	_ "embed"
  6	. "html/template"
  7	"path/filepath"
  8	"time"
  9
 10	"mokhan.ca/xlgmokha/gitmal/internal/git"
 11)
 12
 13var funcs = FuncMap{
 14	"BaseName": filepath.Base,
 15	"FormatDate": func(date time.Time) HTML {
 16		return HTML(date.Format(`<span class="nowrap">2006-01-02</span> <span class="nowrap">15:04:05</span>`))
 17	},
 18	"ShortHash": func(hash string) string {
 19		return hash[:7]
 20	},
 21	"FileTreeParams": func(node []*FileTree) FileTreeParams {
 22		return FileTreeParams{Nodes: node}
 23	},
 24}
 25
 26//go:embed layout.gohtml header.gohtml file_tree.gohtml svg.gohtml
 27var layoutContent embed.FS
 28var layout = Must(New("layout").Funcs(funcs).ParseFS(layoutContent, "*.gohtml"))
 29
 30//go:embed blob.gohtml
 31var blobContent string
 32var BlobTemplate = Must(Must(layout.Clone()).Parse(blobContent))
 33
 34//go:embed markdown.gohtml
 35var markdownContent string
 36var MarkdownTemplate = Must(Must(layout.Clone()).Parse(markdownContent))
 37
 38//go:embed list.gohtml
 39var listContent string
 40var ListTemplate = Must(Must(layout.Clone()).Parse(listContent))
 41
 42//go:embed branches.gohtml
 43var branchesContent string
 44var BranchesTemplate = Must(Must(layout.Clone()).Parse(branchesContent))
 45
 46//go:embed tags.gohtml
 47var tagsContent string
 48var TagsTemplate = Must(Must(layout.Clone()).Parse(tagsContent))
 49
 50//go:embed commits_list.gohtml
 51var commitsListContent string
 52var CommitsListTemplate = Must(Must(layout.Clone()).Parse(commitsListContent))
 53
 54//go:embed commit.gohtml
 55var commitContent string
 56var CommitTemplate = Must(Must(layout.Clone()).Parse(commitContent))
 57
 58type LayoutParams struct {
 59	Title            string
 60	Name             string
 61	RootHref         string
 62	CurrentRefDir    string
 63	Selected         string
 64	NeedsMarkdownCSS bool
 65	NeedsSyntaxCSS   bool
 66	Year             int
 67	CloneURL         string
 68}
 69
 70type HeaderParams struct {
 71	Ref         git.Ref
 72	Header      string
 73	Breadcrumbs []Breadcrumb
 74}
 75
 76type Breadcrumb struct {
 77	Name  string
 78	Href  string
 79	IsDir bool
 80}
 81
 82type BlobParams struct {
 83	LayoutParams
 84	HeaderParams
 85	Blob     git.Blob
 86	IsImage  bool
 87	IsBinary bool
 88	Content  HTML
 89}
 90
 91type MarkdownParams struct {
 92	LayoutParams
 93	HeaderParams
 94	Blob    git.Blob
 95	Content HTML
 96}
 97
 98type ListParams struct {
 99	LayoutParams
100	HeaderParams
101	Ref        git.Ref
102	ParentHref string
103	Dirs       []ListEntry
104	Files      []ListEntry
105	Readme     HTML
106}
107
108type ListEntry struct {
109	Name  string
110	Href  string
111	IsDir bool
112	Mode  string
113	Size  string
114}
115
116type BranchesParams struct {
117	LayoutParams
118	Branches []BranchEntry
119}
120
121type BranchEntry struct {
122	Name        string
123	Href        string
124	IsDefault   bool
125	CommitsHref string
126}
127
128type TagsParams struct {
129	LayoutParams
130	Tags           []TagEntry
131	UnreleasedHref string
132}
133
134type TagEntry struct {
135	git.Tag
136	CompareHref string
137}
138
139type Pagination struct {
140	Page       int
141	TotalPages int
142	PrevHref   string
143	NextHref   string
144	FirstHref  string
145	LastHref   string
146}
147
148type CommitsListParams struct {
149	LayoutParams
150	HeaderParams
151	Ref     git.Ref
152	Commits []git.Commit
153	Page    Pagination
154}
155
156type CommitParams struct {
157	LayoutParams
158	Commit    git.Commit
159	FileTree  []*FileTree
160	FileViews []FileView
161}
162
163type FileTreeParams struct {
164	Nodes []*FileTree
165}
166
167// FileTree represents a directory or file in a commit's changed files tree.
168// For directories, IsDir is true and Children contains nested nodes.
169// For files, status flags indicate the type of change.
170type FileTree struct {
171	Name     string
172	Path     string
173	IsDir    bool
174	Children []*FileTree
175
176	// File status (applicable when IsDir == false)
177	IsNew    bool
178	IsDelete bool
179	IsRename bool
180	OldName  string
181	NewName  string
182	// Anchor id for this file (empty for directories)
183	Anchor string
184}
185
186// FileView represents a single file section on the commit page with its
187// pre-rendered HTML diff and metadata used by the template.
188type FileView struct {
189	Path       string
190	OldName    string
191	NewName    string
192	IsNew      bool
193	IsDelete   bool
194	IsRename   bool
195	IsBinary   bool
196	HasChanges bool
197	HTML       HTML // pre-rendered HTML for diff of this file
198}
199
200type CompareParams struct {
201	LayoutParams
202	Base      string
203	Head      string
204	Commits   []git.Commit
205	FileTree  []*FileTree
206	FileViews []FileView
207}
208
209//go:embed compare.gohtml
210var compareContent string
211var CompareTemplate = Must(Must(layout.Clone()).Parse(compareContent))