summaryrefslogtreecommitdiff
path: root/internal/templates/templates.go
blob: 734a15046d306dd48553edcfbaefcb414ffe9d7f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
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(`<span class="nowrap">2006-01-02</span> <span class="nowrap">15:04:05</span>`))
	},
	"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))