Commit 4cb9cc5
Eric Bower
·
2023-08-09 09:13:06 -0400 EDT
parent 029d56d
theme
1 files changed,
+29,
-17
M
main.go
M
main.go
+29,
-17
| ... | ... | @@ -14,6 +14,7 @@ import ( | |
| 14 | 14 | "strings" | |
| 15 | 15 | "unicode/utf8" | |
| 16 | 16 | ||
| 17 | + | "github.com/alecthomas/chroma" | |
| 17 | 18 | formatterHtml "github.com/alecthomas/chroma/formatters/html" | |
| 18 | 19 | "github.com/alecthomas/chroma/lexers" | |
| 19 | 20 | "github.com/alecthomas/chroma/styles" |
| ... | ... | @@ -47,6 +48,8 @@ type Config struct { | |
| 47 | 48 | // We offer a way to disable showing the latest commit in the output | |
| 48 | 49 | // for those who want a faster build time | |
| 49 | 50 | HideTreeLastCommit bool | |
| 51 | + | // chroma style | |
| 52 | + | Theme *chroma.Style | |
| 50 | 53 | ||
| 51 | 54 | // user-defined urls | |
| 52 | 55 | HomeUrl template.URL |
| ... | ... | @@ -116,6 +119,13 @@ type BranchOutput struct { | |
| 116 | 119 | LastCommit *git.Commit | |
| 117 | 120 | } | |
| 118 | 121 | ||
| 122 | + | type SiteURLs struct { | |
| 123 | + | RootURL template.URL | |
| 124 | + | CloneURL template.URL | |
| 125 | + | SummaryURL template.URL | |
| 126 | + | RefsURL template.URL | |
| 127 | + | } | |
| 128 | + | ||
| 119 | 129 | type PageData struct { | |
| 120 | 130 | Repo *Config | |
| 121 | 131 | SiteURLs *SiteURLs |
| ... | ... | @@ -166,6 +176,12 @@ type WriteData struct { | |
| 166 | 176 | Data interface{} | |
| 167 | 177 | } | |
| 168 | 178 | ||
| 179 | + | func bail(err error) { | |
| 180 | + | if err != nil { | |
| 181 | + | panic(err) | |
| 182 | + | } | |
| 183 | + | } | |
| 184 | + | ||
| 169 | 185 | func diffFileType(_type git.DiffFileType) string { | |
| 170 | 186 | if _type == git.DiffFileAdd { | |
| 171 | 187 | return "A" |
| ... | ... | @@ -180,13 +196,8 @@ func diffFileType(_type git.DiffFileType) string { | |
| 180 | 196 | return "" | |
| 181 | 197 | } | |
| 182 | 198 | ||
| 183 | - | func bail(err error) { | |
| 184 | - | if err != nil { | |
| 185 | - | panic(err) | |
| 186 | - | } | |
| 187 | - | } | |
| 188 | - | ||
| 189 | - | func parseText(filename string, text string) (string, error) { | |
| 199 | + | // converts contents of files in git tree to pretty formatted code | |
| 200 | + | func parseText(filename string, text string, style *chroma.Style) (string, error) { | |
| 190 | 201 | formatter := formatterHtml.New( | |
| 191 | 202 | formatterHtml.WithLineNumbers(true), | |
| 192 | 203 | formatterHtml.LinkableLineNumbers(true, ""), |
| ... | ... | @@ -204,7 +215,7 @@ func parseText(filename string, text string) (string, error) { | |
| 204 | 215 | return text, err | |
| 205 | 216 | } | |
| 206 | 217 | var buf bytes.Buffer | |
| 207 | - | err = formatter.Format(&buf, styles.Dracula, iterator) | |
| 218 | + | err = formatter.Format(&buf, style, iterator) | |
| 208 | 219 | if err != nil { | |
| 209 | 220 | return text, err | |
| 210 | 221 | } |
| ... | ... | @@ -363,7 +374,7 @@ func (c *Config) writeHTMLTreeFiles(pageData *PageData, tree []*TreeItem) string | |
| 363 | 374 | contents := "binary file, cannot display" | |
| 364 | 375 | if file.IsTextFile { | |
| 365 | 376 | file.NumLines = len(strings.Split(str, "\n")) | |
| 366 | - | contents, err = parseText(file.Entry.Name(), string(b)) | |
| 377 | + | contents, err = parseText(file.Entry.Name(), string(b), c.Theme) | |
| 367 | 378 | bail(err) | |
| 368 | 379 | } | |
| 369 | 380 |
| ... | ... | @@ -444,7 +455,7 @@ func (c *Config) writeLogDiffs(repo *git.Repository, pageData *PageData, logs [] | |
| 444 | 455 | } | |
| 445 | 456 | } | |
| 446 | 457 | // set filename to something our `ParseText` recognizes (e.g. `.diff`) | |
| 447 | - | finContent, err := parseText("commit.diff", content) | |
| 458 | + | finContent, err := parseText("commit.diff", content, c.Theme) | |
| 448 | 459 | bail(err) | |
| 449 | 460 | ||
| 450 | 461 | fl.Content = template.HTML(finContent) |
| ... | ... | @@ -471,13 +482,6 @@ func (c *Config) writeLogDiffs(repo *git.Repository, pageData *PageData, logs [] | |
| 471 | 482 | } | |
| 472 | 483 | } | |
| 473 | 484 | ||
| 474 | - | type SiteURLs struct { | |
| 475 | - | RootURL template.URL | |
| 476 | - | CloneURL template.URL | |
| 477 | - | SummaryURL template.URL | |
| 478 | - | RefsURL template.URL | |
| 479 | - | } | |
| 480 | - | ||
| 481 | 485 | func (c *Config) getCloneURL() template.URL { | |
| 482 | 486 | url := fmt.Sprintf("https://%s/%s.git", c.CloneURL, c.RepoName) | |
| 483 | 487 | return template.URL(url) |
| ... | ... | @@ -594,6 +598,7 @@ func (c *Config) writeRepo() *BranchOutput { | |
| 594 | 598 | } | |
| 595 | 599 | ||
| 596 | 600 | // loop through ALL refs that don't have URLs | |
| 601 | + | // and add them to the map | |
| 597 | 602 | for _, ref := range refs { | |
| 598 | 603 | if refInfoMap[ref.ID] != nil { | |
| 599 | 604 | continue |
| ... | ... | @@ -604,6 +609,7 @@ func (c *Config) writeRepo() *BranchOutput { | |
| 604 | 609 | } | |
| 605 | 610 | } | |
| 606 | 611 | ||
| 612 | + | // gather lists of refs to display on refs.html page | |
| 607 | 613 | refInfoList := []*RefInfo{} | |
| 608 | 614 | for _, val := range refInfoMap { | |
| 609 | 615 | refInfoList = append(refInfoList, val) |
| ... | ... | @@ -619,6 +625,8 @@ func (c *Config) writeRepo() *BranchOutput { | |
| 619 | 625 | return urlI > urlJ | |
| 620 | 626 | }) | |
| 621 | 627 | ||
| 628 | + | // use the first revision in our list to generate | |
| 629 | + | // the root summary, logs, and tree the user can click | |
| 622 | 630 | revData := &RevData{ | |
| 623 | 631 | TreeURL: c.getTreeUrl(first.RevName), | |
| 624 | 632 | LogURL: c.getLogsUrl(first.RevName), |
| ... | ... | @@ -716,6 +724,7 @@ func main() { | |
| 716 | 724 | var rpath = flag.String("repo", ".", "path to git repo") | |
| 717 | 725 | var refsFlag = flag.String("refs", "", "list of refs to generate logs and tree (e.g. main,v1)") | |
| 718 | 726 | var revsFlag = flag.String("revs", "HEAD", "list of revs to generate logs and tree (e.g. c69f86f,7415be1") | |
| 727 | + | var themeFlag = flag.String("theme", "dracula", "theme to use for site") | |
| 719 | 728 | ||
| 720 | 729 | flag.Parse() | |
| 721 | 730 |