repos / pgit

static site generator for git
git clone https://github.com/picosh/pgit.git

commit
4cb9cc5
parent
029d56d
author
Eric Bower
date
2023-08-09 13:13:06 +0000 UTC
theme
1 files changed,  +29, -17
M main.go
+29, -17
  1@@ -14,6 +14,7 @@ import (
  2 	"strings"
  3 	"unicode/utf8"
  4 
  5+	"github.com/alecthomas/chroma"
  6 	formatterHtml "github.com/alecthomas/chroma/formatters/html"
  7 	"github.com/alecthomas/chroma/lexers"
  8 	"github.com/alecthomas/chroma/styles"
  9@@ -47,6 +48,8 @@ type Config struct {
 10 	// We offer a way to disable showing the latest commit in the output
 11 	// for those who want a faster build time
 12 	HideTreeLastCommit bool
 13+	// chroma style
 14+	Theme *chroma.Style
 15 
 16 	// user-defined urls
 17 	HomeUrl  template.URL
 18@@ -116,6 +119,13 @@ type BranchOutput struct {
 19 	LastCommit *git.Commit
 20 }
 21 
 22+type SiteURLs struct {
 23+	RootURL    template.URL
 24+	CloneURL   template.URL
 25+	SummaryURL template.URL
 26+	RefsURL    template.URL
 27+}
 28+
 29 type PageData struct {
 30 	Repo     *Config
 31 	SiteURLs *SiteURLs
 32@@ -166,6 +176,12 @@ type WriteData struct {
 33 	Data     interface{}
 34 }
 35 
 36+func bail(err error) {
 37+	if err != nil {
 38+		panic(err)
 39+	}
 40+}
 41+
 42 func diffFileType(_type git.DiffFileType) string {
 43 	if _type == git.DiffFileAdd {
 44 		return "A"
 45@@ -180,13 +196,8 @@ func diffFileType(_type git.DiffFileType) string {
 46 	return ""
 47 }
 48 
 49-func bail(err error) {
 50-	if err != nil {
 51-		panic(err)
 52-	}
 53-}
 54-
 55-func parseText(filename string, text string) (string, error) {
 56+// converts contents of files in git tree to pretty formatted code
 57+func parseText(filename string, text string, style *chroma.Style) (string, error) {
 58 	formatter := formatterHtml.New(
 59 		formatterHtml.WithLineNumbers(true),
 60 		formatterHtml.LinkableLineNumbers(true, ""),
 61@@ -204,7 +215,7 @@ func parseText(filename string, text string) (string, error) {
 62 		return text, err
 63 	}
 64 	var buf bytes.Buffer
 65-	err = formatter.Format(&buf, styles.Dracula, iterator)
 66+	err = formatter.Format(&buf, style, iterator)
 67 	if err != nil {
 68 		return text, err
 69 	}
 70@@ -363,7 +374,7 @@ func (c *Config) writeHTMLTreeFiles(pageData *PageData, tree []*TreeItem) string
 71 		contents := "binary file, cannot display"
 72 		if file.IsTextFile {
 73 			file.NumLines = len(strings.Split(str, "\n"))
 74-			contents, err = parseText(file.Entry.Name(), string(b))
 75+			contents, err = parseText(file.Entry.Name(), string(b), c.Theme)
 76 			bail(err)
 77 		}
 78 
 79@@ -444,7 +455,7 @@ func (c *Config) writeLogDiffs(repo *git.Repository, pageData *PageData, logs []
 80 				}
 81 			}
 82 			// set filename to something our `ParseText` recognizes (e.g. `.diff`)
 83-			finContent, err := parseText("commit.diff", content)
 84+			finContent, err := parseText("commit.diff", content, c.Theme)
 85 			bail(err)
 86 
 87 			fl.Content = template.HTML(finContent)
 88@@ -471,13 +482,6 @@ func (c *Config) writeLogDiffs(repo *git.Repository, pageData *PageData, logs []
 89 	}
 90 }
 91 
 92-type SiteURLs struct {
 93-	RootURL    template.URL
 94-	CloneURL   template.URL
 95-	SummaryURL template.URL
 96-	RefsURL    template.URL
 97-}
 98-
 99 func (c *Config) getCloneURL() template.URL {
100 	url := fmt.Sprintf("https://%s/%s.git", c.CloneURL, c.RepoName)
101 	return template.URL(url)
102@@ -594,6 +598,7 @@ func (c *Config) writeRepo() *BranchOutput {
103 	}
104 
105 	// loop through ALL refs that don't have URLs
106+	// and add them to the map
107 	for _, ref := range refs {
108 		if refInfoMap[ref.ID] != nil {
109 			continue
110@@ -604,6 +609,7 @@ func (c *Config) writeRepo() *BranchOutput {
111 		}
112 	}
113 
114+	// gather lists of refs to display on refs.html page
115 	refInfoList := []*RefInfo{}
116 	for _, val := range refInfoMap {
117 		refInfoList = append(refInfoList, val)
118@@ -619,6 +625,8 @@ func (c *Config) writeRepo() *BranchOutput {
119 		return urlI > urlJ
120 	})
121 
122+	// use the first revision in our list to generate
123+	// the root summary, logs, and tree the user can click
124 	revData := &RevData{
125 		TreeURL: c.getTreeUrl(first.RevName),
126 		LogURL:  c.getLogsUrl(first.RevName),
127@@ -716,6 +724,7 @@ func main() {
128 	var rpath = flag.String("repo", ".", "path to git repo")
129 	var refsFlag = flag.String("refs", "", "list of refs to generate logs and tree (e.g. main,v1)")
130 	var revsFlag = flag.String("revs", "HEAD", "list of revs to generate logs and tree (e.g. c69f86f,7415be1")
131+	var themeFlag = flag.String("theme", "dracula", "theme to use for site")
132 
133 	flag.Parse()
134 
135@@ -734,6 +743,8 @@ func main() {
136 		revs = []string{}
137 	}
138 
139+	theme := styles.Get(*themeFlag)
140+
141 	config := &Config{
142 		Outdir:   out,
143 		RepoPath: repoPath,
144@@ -741,6 +752,7 @@ func main() {
145 		Cache:    make(map[string]bool),
146 		Refs:     refs,
147 		Revs:     revs,
148+		Theme:    theme,
149 	}
150 
151 	config.writeRepo()