Commit e19c0c4

Eric Bower  ·  2024-12-18 22:50:49 -0500 EST
parent 8014989
feat: `--root-relative` flag to change the url for href links
5 files changed,  +71, -53
+3, -3
......@@ -10,9 +10,9 @@
1010
1111 {{template "meta" .}}
1212
13- <link rel="stylesheet" href="/vars.css" />
14- <link rel="stylesheet" href="/smol.css" />
15- <link rel="stylesheet" href="/main.css" />
13+ <link rel="stylesheet" href="{{.Repo.RootRelative}}vars.css" />
14+ <link rel="stylesheet" href="{{.Repo.RootRelative}}smol.css" />
15+ <link rel="stylesheet" href="{{.Repo.RootRelative}}main.css" />
1616 </head>
1717 <body>
1818 <header>{{template "header" .}}</header>
+1, -1
......@@ -1,7 +1,7 @@
11 {{template "base" .}}
22 {{define "title"}}{{.Commit.Summary}} - {{.Repo.RepoName}}@{{.CommitID}}{{end}}
33 {{define "meta"}}
4-<link rel="stylesheet" href="/syntax.css" />
4+<link rel="stylesheet" href="{{.Repo.RootRelative}}syntax.css" />
55 {{end}}
66
77 {{define "content"}}
+1, -1
......@@ -1,7 +1,7 @@
11 {{template "base" .}}
22 {{define "title"}}{{.Item.Path}}@{{.RevData.Name}}{{end}}
33 {{define "meta"}}
4-<link rel="stylesheet" href="/syntax.css" />
4+<link rel="stylesheet" href="{{.Repo.RootRelative}}syntax.css" />
55 {{end}}
66
77 {{define "content"}}
+1, -1
......@@ -2,7 +2,7 @@
22
33 {{define "title"}}{{.Repo.RepoName}}{{if .Repo.Desc}}- {{.Repo.Desc}}{{end}}{{end}}
44 {{define "meta"}}
5-<link rel="stylesheet" href="/syntax.css" />
5+<link rel="stylesheet" href="{{.Repo.RootRelative}}syntax.css" />
66 {{end}}
77
88 {{define "content"}}
+65, -47
......@@ -14,6 +14,7 @@ import (
1414 "sort"
1515 "strings"
1616 "sync"
17+ "time"
1718 "unicode/utf8"
1819
1920 "github.com/alecthomas/chroma/v2"
......@@ -52,6 +53,9 @@ type Config struct {
5253 HomeURL template.URL
5354 CloneURL template.URL
5455
56+ // https://developer.mozilla.org/en-US/docs/Web/API/URL_API/Resolving_relative_references#root_relative
57+ RootRelative string
58+
5559 // computed
5660 // cache for skipping commits, trees, etc.
5761 Cache map[string]bool
......@@ -73,8 +77,9 @@ type RevInfo interface {
7377
7478 // revision data
7579 type RevData struct {
76- id string
77- name string
80+ id string
81+ name string
82+ Config *Config
7883 }
7984
8085 func (r *RevData) ID() string {
......@@ -86,11 +91,11 @@ func (r *RevData) Name() string {
8691 }
8792
8893 func (r *RevData) TreeURL() template.URL {
89- return getTreeURL(r)
94+ return r.Config.getTreeURL(r)
9095 }
9196
9297 func (r *RevData) LogURL() template.URL {
93- return getLogsURL(r)
98+ return r.Config.getLogsURL(r)
9499 }
95100
96101 type TagData struct {
......@@ -427,7 +432,7 @@ func (c *Config) writeHTMLTreeFile(pageData *PageData, treeItem *TreeItem) strin
427432 Contents: template.HTML(contents),
428433 Item: treeItem,
429434 },
430- Subdir: getFileURL(pageData.RevData, d),
435+ Subdir: getFileDir(pageData.RevData, d),
431436 })
432437 return readme
433438 }
......@@ -494,8 +499,8 @@ func (c *Config) writeLogDiff(repo *git.Repository, pageData *PageData, commit *
494499 CommitID: getShortID(commitID),
495500 Diff: rnd,
496501 Parent: getShortID(commit.ParentID),
497- CommitURL: getCommitURL(commitID),
498- ParentURL: getCommitURL(commit.ParentID),
502+ CommitURL: c.getCommitURL(commitID),
503+ ParentURL: c.getCommitURL(commit.ParentID),
499504 }
500505
501506 c.writeHtml(&WriteData{
......@@ -506,13 +511,13 @@ func (c *Config) writeLogDiff(repo *git.Repository, pageData *PageData, commit *
506511 })
507512 }
508513
509-func getSummaryURL() template.URL {
510- url := "/index.html"
514+func (c *Config) getSummaryURL() template.URL {
515+ url := c.RootRelative + "index.html"
511516 return template.URL(url)
512517 }
513518
514-func getRefsURL() template.URL {
515- url := "/refs.html"
519+func (c *Config) getRefsURL() template.URL {
520+ url := c.RootRelative + "refs.html"
516521 return template.URL(url)
517522 }
518523
......@@ -537,24 +542,32 @@ func getFileBaseDir(info RevInfo) string {
537542 return filepath.Join(getTreeBaseDir(info), "item")
538543 }
539544
540-func getFileURL(info RevInfo, fname string) string {
545+func getFileDir(info RevInfo, fname string) string {
541546 return filepath.Join(getFileBaseDir(info), fname)
542547 }
543548
544-func getTreeURL(info RevInfo) template.URL {
545- dir := getTreeBaseDir(info)
546- url := filepath.Join(dir, "index.html")
549+func (c *Config) getFileURL(info RevInfo, fname string) template.URL {
550+ return c.compileURL(getFileBaseDir(info), fname)
551+}
552+
553+func (c *Config) compileURL(dir, fname string) template.URL {
554+ purl := c.RootRelative + strings.TrimPrefix(dir, "/")
555+ url := filepath.Join(purl, fname)
547556 return template.URL(url)
548557 }
549558
550-func getLogsURL(info RevInfo) template.URL {
559+func (c *Config) getTreeURL(info RevInfo) template.URL {
560+ dir := getTreeBaseDir(info)
561+ return c.compileURL(dir, "index.html")
562+}
563+
564+func (c *Config) getLogsURL(info RevInfo) template.URL {
551565 dir := getLogBaseDir(info)
552- url := filepath.Join(dir, "index.html")
553- return template.URL(url)
566+ return c.compileURL(dir, "index.html")
554567 }
555568
556-func getCommitURL(commitID string) template.URL {
557- url := fmt.Sprintf("/commits/%s.html", commitID)
569+func (c *Config) getCommitURL(commitID string) template.URL {
570+ url := fmt.Sprintf("%scommits/%s.html", c.RootRelative, commitID)
558571 return template.URL(url)
559572 }
560573
......@@ -562,8 +575,8 @@ func (c *Config) getURLs() *SiteURLs {
562575 return &SiteURLs{
563576 HomeURL: c.HomeURL,
564577 CloneURL: c.CloneURL,
565- RefsURL: getRefsURL(),
566- SummaryURL: getSummaryURL(),
578+ RefsURL: c.getRefsURL(),
579+ SummaryURL: c.getSummaryURL(),
567580 }
568581 }
569582
......@@ -596,8 +609,9 @@ func (c *Config) writeRepo() *BranchOutput {
596609 }
597610
598611 data := &RevData{
599- id: fullRevID,
600- name: revName,
612+ id: fullRevID,
613+ name: revName,
614+ Config: c,
601615 }
602616
603617 if first == nil {
......@@ -673,8 +687,9 @@ func (c *Config) writeRepo() *BranchOutput {
673687 // use the first revision in our list to generate
674688 // the root summary, logs, and tree the user can click
675689 revData := &RevData{
676- id: first.ID(),
677- name: first.Name(),
690+ id: first.ID(),
691+ name: first.Name(),
692+ Config: c,
678693 }
679694
680695 data := &PageData{
......@@ -699,6 +714,7 @@ type TreeWalker struct {
699714 HideTreeLastCommit bool
700715 PageData *PageData
701716 Repo *git.Repository
717+ Config *Config
702718 }
703719
704720 type Breadcrumb struct {
......@@ -712,11 +728,9 @@ func (tw *TreeWalker) calcBreadcrumbs(curpath string) []*Breadcrumb {
712728 return []*Breadcrumb{}
713729 }
714730 parts := strings.Split(curpath, string(os.PathSeparator))
715- rootURL := template.URL(
716- filepath.Join(
717- getTreeBaseDir(tw.PageData.RevData),
718- "index.html",
719- ),
731+ rootURL := tw.Config.compileURL(
732+ getTreeBaseDir(tw.PageData.RevData),
733+ "index.html",
720734 )
721735
722736 crumbs := make([]*Breadcrumb, len(parts)+1)
......@@ -727,9 +741,11 @@ func (tw *TreeWalker) calcBreadcrumbs(curpath string) []*Breadcrumb {
727741
728742 cur := ""
729743 for idx, d := range parts {
744+ crumb := filepath.Join(getFileBaseDir(tw.PageData.RevData), cur, d)
745+ crumbUrl := tw.Config.compileURL(crumb, "index.html")
730746 crumbs[idx+1] = &Breadcrumb{
731747 Text: d,
732- URL: template.URL(filepath.Join(getFileBaseDir(tw.PageData.RevData), cur, d, "index.html")),
748+ URL: crumbUrl,
733749 }
734750 if idx == len(parts)-1 {
735751 crumbs[idx+1].IsLast = true
......@@ -775,7 +791,7 @@ func (tw *TreeWalker) NewTreeItem(entry *git.TreeEntry, curpath string, crumbs [
775791 Name: entry.Name(),
776792 Path: fname,
777793 Entry: entry,
778- URL: template.URL(getFileURL(tw.PageData.RevData, fname)),
794+ URL: tw.Config.getFileURL(tw.PageData.RevData, fname),
779795 Crumbs: crumbs,
780796 }
781797
......@@ -794,29 +810,28 @@ func (tw *TreeWalker) NewTreeItem(entry *git.TreeEntry, curpath string, crumbs [
794810 if len(lastCommits) > 0 {
795811 lc = lastCommits[0]
796812 }
797- item.CommitURL = getCommitURL(lc.ID.String())
813+ item.CommitURL = tw.Config.getCommitURL(lc.ID.String())
798814 item.CommitID = getShortID(lc.ID.String())
799815 item.Summary = lc.Summary()
800- item.When = lc.Author.When.Format("02 Jan 06")
816+ item.When = lc.Author.When.Format(time.DateOnly)
801817 item.Author = lc.Author
802818 }
803819
804- fpath := getFileURL(
805- tw.PageData.RevData,
806- fmt.Sprintf("%s.html", fname),
807- )
820+ fpath := tw.Config.getFileURL(tw.PageData.RevData, fmt.Sprintf("%s.html", fname))
808821 if typ == git.ObjectTree {
809822 item.IsDir = true
810- fpath = filepath.Join(
811- getFileBaseDir(tw.PageData.RevData),
812- curpath,
813- entry.Name(),
823+ fpath = tw.Config.compileURL(
824+ filepath.Join(
825+ getFileBaseDir(tw.PageData.RevData),
826+ curpath,
827+ entry.Name(),
828+ ),
814829 "index.html",
815830 )
816831 } else if typ == git.ObjectBlob {
817832 item.Icon = FilenameToDevIcon(item.Name)
818833 }
819- item.URL = template.URL(fpath)
834+ item.URL = fpath
820835
821836 return item
822837 }
......@@ -926,11 +941,11 @@ func (c *Config) writeRevision(repo *git.Repository, pageData *PageData, refs []
926941 }
927942 logs = append(logs, &CommitData{
928943 ParentID: parentID,
929- URL: getCommitURL(commit.ID.String()),
944+ URL: c.getCommitURL(commit.ID.String()),
930945 ShortID: getShortID(commit.ID.String()),
931946 SummaryStr: commit.Summary(),
932947 AuthorStr: commit.Author.Name,
933- WhenStr: commit.Author.When.Format("02 Jan 06"),
948+ WhenStr: commit.Author.When.Format(time.DateOnly),
934949 Commit: commit,
935950 Refs: tags,
936951 })
......@@ -954,6 +969,7 @@ func (c *Config) writeRevision(repo *git.Repository, pageData *PageData, refs []
954969 entries := make(chan *TreeItem)
955970 subtrees := make(chan *TreeRoot)
956971 tw := &TreeWalker{
972+ Config: c,
957973 PageData: pageData,
958974 Repo: repo,
959975 treeItem: entries,
......@@ -1035,12 +1051,13 @@ func style(theme chroma.Style) string {
10351051 func main() {
10361052 var outdir = flag.String("out", "./public", "output directory")
10371053 var rpath = flag.String("repo", ".", "path to git repo")
1038- var revsFlag = flag.String("revs", "HEAD", "list of revs to generate logs and tree (e.g. main,v1,c69f86f,HEAD")
1054+ var revsFlag = flag.String("revs", "HEAD", "list of revs to generate logs and tree (e.g. main,v1,c69f86f,HEAD)")
10391055 var themeFlag = flag.String("theme", "dracula", "theme to use for site")
10401056 var labelFlag = flag.String("label", "", "pretty name for the subdir where we create the repo, default is last folder in --repo")
10411057 var cloneFlag = flag.String("clone-url", "", "git clone URL")
10421058 var homeFlag = flag.String("home-url", "", "URL for breadcumbs to get to list of repositories")
10431059 var descFlag = flag.String("desc", "", "description for repo")
1060+ var rootRelativeFlag = flag.String("root-relative", "/", "html root relative")
10441061 var maxCommitsFlag = flag.Int("max-commits", 0, "maximum number of commits to generate")
10451062 var hideTreeLastCommitFlag = flag.Bool("hide-tree-last-commit", false, "dont calculate last commit for each file in the tree")
10461063
......@@ -1084,6 +1101,7 @@ func main() {
10841101 Desc: *descFlag,
10851102 MaxCommits: *maxCommitsFlag,
10861103 HideTreeLastCommit: *hideTreeLastCommitFlag,
1104+ RootRelative: *rootRelativeFlag,
10871105 Formatter: formatter,
10881106 }
10891107 config.Logger.Info("config", "config", config)