main
1package templates
2
3import (
4 "embed"
5 _ "embed"
6 . "html/template"
7 "path/filepath"
8 "time"
9
10 "github.com/antonmedv/gitmal/pkg/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
58//go:embed preview.gohtml
59var previewContent string
60var PreviewTemplate = Must(New("preview").Parse(previewContent))
61
62type LayoutParams struct {
63 Title string
64 Name string
65 RootHref string
66 CurrentRefDir string
67 Selected string
68 AssetNames map[string]string
69}
70
71type HeaderParams struct {
72 Ref git.Ref
73 Header string
74 Breadcrumbs []Breadcrumb
75 RawHref string
76}
77
78type Breadcrumb struct {
79 Name string
80 Href string
81 IsDir bool
82}
83
84type BlobParams struct {
85 LayoutParams
86 HeaderParams
87 Blob git.Blob
88 IsImage bool
89 IsBinary bool
90 Content HTML
91}
92
93type MarkdownParams struct {
94 LayoutParams
95 HeaderParams
96 Blob git.Blob
97 Content HTML
98}
99
100type ListParams struct {
101 LayoutParams
102 HeaderParams
103 Ref git.Ref
104 ParentHref string
105 Dirs []ListEntry
106 Files []ListEntry
107 Readme HTML
108}
109
110type ListEntry struct {
111 Name string
112 Href string
113 IsDir bool
114 Mode string
115 Size string
116}
117
118type BranchesParams struct {
119 LayoutParams
120 Branches []BranchEntry
121}
122
123type BranchEntry struct {
124 Name string
125 Href string
126 IsDefault bool
127 CommitsHref string
128}
129
130type TagsParams struct {
131 LayoutParams
132 Tags []git.Tag
133}
134
135type Pagination struct {
136 Page int
137 TotalPages int
138 PrevHref string
139 NextHref string
140 FirstHref string
141 LastHref string
142}
143
144type CommitsListParams struct {
145 LayoutParams
146 HeaderParams
147 Ref git.Ref
148 Commits []git.Commit
149 Page Pagination
150}
151
152type CommitParams struct {
153 LayoutParams
154 Commit git.Commit
155 FileTree []*FileTree
156 FileViews []FileView
157}
158
159type FileTreeParams struct {
160 Nodes []*FileTree
161}
162
163// FileTree represents a directory or file in a commit's changed files tree.
164// For directories, IsDir is true and Children contains nested nodes.
165// For files, status flags indicate the type of change.
166type FileTree struct {
167 Name string
168 Path string
169 IsDir bool
170 Children []*FileTree
171
172 // File status (applicable when IsDir == false)
173 IsNew bool
174 IsDelete bool
175 IsRename bool
176 OldName string
177 NewName string
178 // Anchor id for this file (empty for directories)
179 Anchor string
180}
181
182// FileView represents a single file section on the commit page with its
183// pre-rendered HTML diff and metadata used by the template.
184type FileView struct {
185 Path string
186 OldName string
187 NewName string
188 IsNew bool
189 IsDelete bool
190 IsRename bool
191 IsBinary bool
192 HasChanges bool
193 HTML HTML // pre-rendered HTML for diff of this file
194}
195
196type PreviewCard struct {
197 Name string
198 Tone string
199 HTML HTML
200}
201
202type PreviewParams struct {
203 Count int
204 Themes []PreviewCard
205}