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