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