Commit 4cb9cc5

Eric Bower  ·  2023-08-09 09:13:06 -0400 EDT
parent 029d56d
theme
1 files changed,  +29, -17
+29, -17
......@@ -14,6 +14,7 @@ import (
1414 "strings"
1515 "unicode/utf8"
1616
17+ "github.com/alecthomas/chroma"
1718 formatterHtml "github.com/alecthomas/chroma/formatters/html"
1819 "github.com/alecthomas/chroma/lexers"
1920 "github.com/alecthomas/chroma/styles"
......@@ -47,6 +48,8 @@ type Config struct {
4748 // We offer a way to disable showing the latest commit in the output
4849 // for those who want a faster build time
4950 HideTreeLastCommit bool
51+ // chroma style
52+ Theme *chroma.Style
5053
5154 // user-defined urls
5255 HomeUrl template.URL
......@@ -116,6 +119,13 @@ type BranchOutput struct {
116119 LastCommit *git.Commit
117120 }
118121
122+type SiteURLs struct {
123+ RootURL template.URL
124+ CloneURL template.URL
125+ SummaryURL template.URL
126+ RefsURL template.URL
127+}
128+
119129 type PageData struct {
120130 Repo *Config
121131 SiteURLs *SiteURLs
......@@ -166,6 +176,12 @@ type WriteData struct {
166176 Data interface{}
167177 }
168178
179+func bail(err error) {
180+ if err != nil {
181+ panic(err)
182+ }
183+}
184+
169185 func diffFileType(_type git.DiffFileType) string {
170186 if _type == git.DiffFileAdd {
171187 return "A"
......@@ -180,13 +196,8 @@ func diffFileType(_type git.DiffFileType) string {
180196 return ""
181197 }
182198
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) {
190201 formatter := formatterHtml.New(
191202 formatterHtml.WithLineNumbers(true),
192203 formatterHtml.LinkableLineNumbers(true, ""),
......@@ -204,7 +215,7 @@ func parseText(filename string, text string) (string, error) {
204215 return text, err
205216 }
206217 var buf bytes.Buffer
207- err = formatter.Format(&buf, styles.Dracula, iterator)
218+ err = formatter.Format(&buf, style, iterator)
208219 if err != nil {
209220 return text, err
210221 }
......@@ -363,7 +374,7 @@ func (c *Config) writeHTMLTreeFiles(pageData *PageData, tree []*TreeItem) string
363374 contents := "binary file, cannot display"
364375 if file.IsTextFile {
365376 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)
367378 bail(err)
368379 }
369380
......@@ -444,7 +455,7 @@ func (c *Config) writeLogDiffs(repo *git.Repository, pageData *PageData, logs []
444455 }
445456 }
446457 // 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)
448459 bail(err)
449460
450461 fl.Content = template.HTML(finContent)
......@@ -471,13 +482,6 @@ func (c *Config) writeLogDiffs(repo *git.Repository, pageData *PageData, logs []
471482 }
472483 }
473484
474-type SiteURLs struct {
475- RootURL template.URL
476- CloneURL template.URL
477- SummaryURL template.URL
478- RefsURL template.URL
479-}
480-
481485 func (c *Config) getCloneURL() template.URL {
482486 url := fmt.Sprintf("https://%s/%s.git", c.CloneURL, c.RepoName)
483487 return template.URL(url)
......@@ -594,6 +598,7 @@ func (c *Config) writeRepo() *BranchOutput {
594598 }
595599
596600 // loop through ALL refs that don't have URLs
601+ // and add them to the map
597602 for _, ref := range refs {
598603 if refInfoMap[ref.ID] != nil {
599604 continue
......@@ -604,6 +609,7 @@ func (c *Config) writeRepo() *BranchOutput {
604609 }
605610 }
606611
612+ // gather lists of refs to display on refs.html page
607613 refInfoList := []*RefInfo{}
608614 for _, val := range refInfoMap {
609615 refInfoList = append(refInfoList, val)
......@@ -619,6 +625,8 @@ func (c *Config) writeRepo() *BranchOutput {
619625 return urlI > urlJ
620626 })
621627
628+ // use the first revision in our list to generate
629+ // the root summary, logs, and tree the user can click
622630 revData := &RevData{
623631 TreeURL: c.getTreeUrl(first.RevName),
624632 LogURL: c.getLogsUrl(first.RevName),
......@@ -716,6 +724,7 @@ func main() {
716724 var rpath = flag.String("repo", ".", "path to git repo")
717725 var refsFlag = flag.String("refs", "", "list of refs to generate logs and tree (e.g. main,v1)")
718726 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")
719728
720729 flag.Parse()
721730
......@@ -734,6 +743,8 @@ func main() {
734743 revs = []string{}
735744 }
736745
746+ theme := styles.Get(*themeFlag)
747+
737748 config := &Config{
738749 Outdir: out,
739750 RepoPath: repoPath,
......@@ -741,6 +752,7 @@ func main() {
741752 Cache: make(map[string]bool),
742753 Refs: refs,
743754 Revs: revs,
755+ Theme: theme,
744756 }
745757
746758 config.writeRepo()