feat/multi-repo
  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}
 91
 92type Breadcrumb struct {
 93	Name  string
 94	Href  string
 95	IsDir bool
 96}
 97
 98type BlobParams struct {
 99	LayoutParams
100	HeaderParams
101	CSS      CSS
102	Blob     git.Blob
103	IsImage  bool
104	IsBinary bool
105	Content  HTML
106}
107
108type MarkdownParams struct {
109	LayoutParams
110	HeaderParams
111	Blob    git.Blob
112	Content HTML
113}
114
115type ListParams struct {
116	LayoutParams
117	HeaderParams
118	Ref        git.Ref
119	ParentHref string
120	Dirs       []ListEntry
121	Files      []ListEntry
122	Readme     HTML
123}
124
125type ListEntry struct {
126	Name  string
127	Href  string
128	IsDir bool
129	Mode  string
130	Size  string
131}
132
133type BranchesParams struct {
134	LayoutParams
135	Branches []BranchEntry
136}
137
138type BranchEntry struct {
139	Name        string
140	Href        string
141	IsDefault   bool
142	CommitsHref string
143}
144
145type TagsParams struct {
146	LayoutParams
147	Tags []git.Tag
148}
149
150type Pagination struct {
151	Page       int
152	TotalPages int
153	PrevHref   string
154	NextHref   string
155	FirstHref  string
156	LastHref   string
157}
158
159type CommitsListParams struct {
160	LayoutParams
161	HeaderParams
162	Ref     git.Ref
163	Commits []git.Commit
164	Page    Pagination
165}
166
167type CommitParams struct {
168	LayoutParams
169	Commit    git.Commit
170	DiffCSS   CSS
171	FileTree  []*FileTree
172	FileViews []FileView
173}
174
175type FileTreeParams struct {
176	Nodes []*FileTree
177}
178
179// FileTree represents a directory or file in a commit's changed files tree.
180// For directories, IsDir is true and Children contains nested nodes.
181// For files, status flags indicate the type of change.
182type FileTree struct {
183	Name     string
184	Path     string
185	IsDir    bool
186	Children []*FileTree
187
188	// File status (applicable when IsDir == false)
189	IsNew    bool
190	IsDelete bool
191	IsRename bool
192	OldName  string
193	NewName  string
194	// Anchor id for this file (empty for directories)
195	Anchor string
196}
197
198// FileView represents a single file section on the commit page with its
199// pre-rendered HTML diff and metadata used by the template.
200type FileView struct {
201	Path       string
202	OldName    string
203	NewName    string
204	IsNew      bool
205	IsDelete   bool
206	IsRename   bool
207	IsBinary   bool
208	HasChanges bool
209	HTML       HTML // pre-rendered HTML for diff of this file
210}
211
212type PreviewCard struct {
213	Name string
214	Tone string
215	HTML HTML
216}
217
218type PreviewParams struct {
219	Count  int
220	Themes []PreviewCard
221}
222
223type RepoIndexEntry struct {
224	Name        string
225	Slug        string
226	Href        string
227	Description string
228	LastUpdated time.Time
229}
230
231type MultiRepoIndexParams struct {
232	LayoutParams
233	Title string
234	Repos []RepoIndexEntry
235}