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 @@ | |
| 10 | 10 | ||
| 11 | 11 | {{template "meta" .}} | |
| 12 | 12 | ||
| 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" /> | |
| 16 | 16 | </head> | |
| 17 | 17 | <body> | |
| 18 | 18 | <header>{{template "header" .}}</header> |
+1,
-1
| ... | ... | @@ -1,7 +1,7 @@ | |
| 1 | 1 | {{template "base" .}} | |
| 2 | 2 | {{define "title"}}{{.Commit.Summary}} - {{.Repo.RepoName}}@{{.CommitID}}{{end}} | |
| 3 | 3 | {{define "meta"}} | |
| 4 | - | <link rel="stylesheet" href="/syntax.css" /> | |
| 4 | + | <link rel="stylesheet" href="{{.Repo.RootRelative}}syntax.css" /> | |
| 5 | 5 | {{end}} | |
| 6 | 6 | ||
| 7 | 7 | {{define "content"}} |
+1,
-1
| ... | ... | @@ -1,7 +1,7 @@ | |
| 1 | 1 | {{template "base" .}} | |
| 2 | 2 | {{define "title"}}{{.Item.Path}}@{{.RevData.Name}}{{end}} | |
| 3 | 3 | {{define "meta"}} | |
| 4 | - | <link rel="stylesheet" href="/syntax.css" /> | |
| 4 | + | <link rel="stylesheet" href="{{.Repo.RootRelative}}syntax.css" /> | |
| 5 | 5 | {{end}} | |
| 6 | 6 | ||
| 7 | 7 | {{define "content"}} |
+1,
-1
| ... | ... | @@ -2,7 +2,7 @@ | |
| 2 | 2 | ||
| 3 | 3 | {{define "title"}}{{.Repo.RepoName}}{{if .Repo.Desc}}- {{.Repo.Desc}}{{end}}{{end}} | |
| 4 | 4 | {{define "meta"}} | |
| 5 | - | <link rel="stylesheet" href="/syntax.css" /> | |
| 5 | + | <link rel="stylesheet" href="{{.Repo.RootRelative}}syntax.css" /> | |
| 6 | 6 | {{end}} | |
| 7 | 7 | ||
| 8 | 8 | {{define "content"}} |
M
main.go
+65,
-47
| ... | ... | @@ -14,6 +14,7 @@ import ( | |
| 14 | 14 | "sort" | |
| 15 | 15 | "strings" | |
| 16 | 16 | "sync" | |
| 17 | + | "time" | |
| 17 | 18 | "unicode/utf8" | |
| 18 | 19 | ||
| 19 | 20 | "github.com/alecthomas/chroma/v2" |
| ... | ... | @@ -52,6 +53,9 @@ type Config struct { | |
| 52 | 53 | HomeURL template.URL | |
| 53 | 54 | CloneURL template.URL | |
| 54 | 55 | ||
| 56 | + | // https://developer.mozilla.org/en-US/docs/Web/API/URL_API/Resolving_relative_references#root_relative | |
| 57 | + | RootRelative string | |
| 58 | + | ||
| 55 | 59 | // computed | |
| 56 | 60 | // cache for skipping commits, trees, etc. | |
| 57 | 61 | Cache map[string]bool |
| ... | ... | @@ -73,8 +77,9 @@ type RevInfo interface { | |
| 73 | 77 | ||
| 74 | 78 | // revision data | |
| 75 | 79 | type RevData struct { | |
| 76 | - | id string | |
| 77 | - | name string | |
| 80 | + | id string | |
| 81 | + | name string | |
| 82 | + | Config *Config | |
| 78 | 83 | } | |
| 79 | 84 | ||
| 80 | 85 | func (r *RevData) ID() string { |
| ... | ... | @@ -86,11 +91,11 @@ func (r *RevData) Name() string { | |
| 86 | 91 | } | |
| 87 | 92 | ||
| 88 | 93 | func (r *RevData) TreeURL() template.URL { | |
| 89 | - | return getTreeURL(r) | |
| 94 | + | return r.Config.getTreeURL(r) | |
| 90 | 95 | } | |
| 91 | 96 | ||
| 92 | 97 | func (r *RevData) LogURL() template.URL { | |
| 93 | - | return getLogsURL(r) | |
| 98 | + | return r.Config.getLogsURL(r) | |
| 94 | 99 | } | |
| 95 | 100 | ||
| 96 | 101 | type TagData struct { |
| ... | ... | @@ -427,7 +432,7 @@ func (c *Config) writeHTMLTreeFile(pageData *PageData, treeItem *TreeItem) strin | |
| 427 | 432 | Contents: template.HTML(contents), | |
| 428 | 433 | Item: treeItem, | |
| 429 | 434 | }, | |
| 430 | - | Subdir: getFileURL(pageData.RevData, d), | |
| 435 | + | Subdir: getFileDir(pageData.RevData, d), | |
| 431 | 436 | }) | |
| 432 | 437 | return readme | |
| 433 | 438 | } |
| ... | ... | @@ -494,8 +499,8 @@ func (c *Config) writeLogDiff(repo *git.Repository, pageData *PageData, commit * | |
| 494 | 499 | CommitID: getShortID(commitID), | |
| 495 | 500 | Diff: rnd, | |
| 496 | 501 | Parent: getShortID(commit.ParentID), | |
| 497 | - | CommitURL: getCommitURL(commitID), | |
| 498 | - | ParentURL: getCommitURL(commit.ParentID), | |
| 502 | + | CommitURL: c.getCommitURL(commitID), | |
| 503 | + | ParentURL: c.getCommitURL(commit.ParentID), | |
| 499 | 504 | } | |
| 500 | 505 | ||
| 501 | 506 | c.writeHtml(&WriteData{ |
| ... | ... | @@ -506,13 +511,13 @@ func (c *Config) writeLogDiff(repo *git.Repository, pageData *PageData, commit * | |
| 506 | 511 | }) | |
| 507 | 512 | } | |
| 508 | 513 | ||
| 509 | - | func getSummaryURL() template.URL { | |
| 510 | - | url := "/index.html" | |
| 514 | + | func (c *Config) getSummaryURL() template.URL { | |
| 515 | + | url := c.RootRelative + "index.html" | |
| 511 | 516 | return template.URL(url) | |
| 512 | 517 | } | |
| 513 | 518 | ||
| 514 | - | func getRefsURL() template.URL { | |
| 515 | - | url := "/refs.html" | |
| 519 | + | func (c *Config) getRefsURL() template.URL { | |
| 520 | + | url := c.RootRelative + "refs.html" | |
| 516 | 521 | return template.URL(url) | |
| 517 | 522 | } | |
| 518 | 523 |
| ... | ... | @@ -537,24 +542,32 @@ func getFileBaseDir(info RevInfo) string { | |
| 537 | 542 | return filepath.Join(getTreeBaseDir(info), "item") | |
| 538 | 543 | } | |
| 539 | 544 | ||
| 540 | - | func getFileURL(info RevInfo, fname string) string { | |
| 545 | + | func getFileDir(info RevInfo, fname string) string { | |
| 541 | 546 | return filepath.Join(getFileBaseDir(info), fname) | |
| 542 | 547 | } | |
| 543 | 548 | ||
| 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) | |
| 547 | 556 | return template.URL(url) | |
| 548 | 557 | } | |
| 549 | 558 | ||
| 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 { | |
| 551 | 565 | dir := getLogBaseDir(info) | |
| 552 | - | url := filepath.Join(dir, "index.html") | |
| 553 | - | return template.URL(url) | |
| 566 | + | return c.compileURL(dir, "index.html") | |
| 554 | 567 | } | |
| 555 | 568 | ||
| 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) | |
| 558 | 571 | return template.URL(url) | |
| 559 | 572 | } | |
| 560 | 573 |
| ... | ... | @@ -562,8 +575,8 @@ func (c *Config) getURLs() *SiteURLs { | |
| 562 | 575 | return &SiteURLs{ | |
| 563 | 576 | HomeURL: c.HomeURL, | |
| 564 | 577 | CloneURL: c.CloneURL, | |
| 565 | - | RefsURL: getRefsURL(), | |
| 566 | - | SummaryURL: getSummaryURL(), | |
| 578 | + | RefsURL: c.getRefsURL(), | |
| 579 | + | SummaryURL: c.getSummaryURL(), | |
| 567 | 580 | } | |
| 568 | 581 | } | |
| 569 | 582 |
| ... | ... | @@ -596,8 +609,9 @@ func (c *Config) writeRepo() *BranchOutput { | |
| 596 | 609 | } | |
| 597 | 610 | ||
| 598 | 611 | data := &RevData{ | |
| 599 | - | id: fullRevID, | |
| 600 | - | name: revName, | |
| 612 | + | id: fullRevID, | |
| 613 | + | name: revName, | |
| 614 | + | Config: c, | |
| 601 | 615 | } | |
| 602 | 616 | ||
| 603 | 617 | if first == nil { |
| ... | ... | @@ -673,8 +687,9 @@ func (c *Config) writeRepo() *BranchOutput { | |
| 673 | 687 | // use the first revision in our list to generate | |
| 674 | 688 | // the root summary, logs, and tree the user can click | |
| 675 | 689 | revData := &RevData{ | |
| 676 | - | id: first.ID(), | |
| 677 | - | name: first.Name(), | |
| 690 | + | id: first.ID(), | |
| 691 | + | name: first.Name(), | |
| 692 | + | Config: c, | |
| 678 | 693 | } | |
| 679 | 694 | ||
| 680 | 695 | data := &PageData{ |
| ... | ... | @@ -699,6 +714,7 @@ type TreeWalker struct { | |
| 699 | 714 | HideTreeLastCommit bool | |
| 700 | 715 | PageData *PageData | |
| 701 | 716 | Repo *git.Repository | |
| 717 | + | Config *Config | |
| 702 | 718 | } | |
| 703 | 719 | ||
| 704 | 720 | type Breadcrumb struct { |
| ... | ... | @@ -712,11 +728,9 @@ func (tw *TreeWalker) calcBreadcrumbs(curpath string) []*Breadcrumb { | |
| 712 | 728 | return []*Breadcrumb{} | |
| 713 | 729 | } | |
| 714 | 730 | 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", | |
| 720 | 734 | ) | |
| 721 | 735 | ||
| 722 | 736 | crumbs := make([]*Breadcrumb, len(parts)+1) |
| ... | ... | @@ -727,9 +741,11 @@ func (tw *TreeWalker) calcBreadcrumbs(curpath string) []*Breadcrumb { | |
| 727 | 741 | ||
| 728 | 742 | cur := "" | |
| 729 | 743 | for idx, d := range parts { | |
| 744 | + | crumb := filepath.Join(getFileBaseDir(tw.PageData.RevData), cur, d) | |
| 745 | + | crumbUrl := tw.Config.compileURL(crumb, "index.html") | |
| 730 | 746 | crumbs[idx+1] = &Breadcrumb{ | |
| 731 | 747 | Text: d, | |
| 732 | - | URL: template.URL(filepath.Join(getFileBaseDir(tw.PageData.RevData), cur, d, "index.html")), | |
| 748 | + | URL: crumbUrl, | |
| 733 | 749 | } | |
| 734 | 750 | if idx == len(parts)-1 { | |
| 735 | 751 | crumbs[idx+1].IsLast = true |
| ... | ... | @@ -775,7 +791,7 @@ func (tw *TreeWalker) NewTreeItem(entry *git.TreeEntry, curpath string, crumbs [ | |
| 775 | 791 | Name: entry.Name(), | |
| 776 | 792 | Path: fname, | |
| 777 | 793 | Entry: entry, | |
| 778 | - | URL: template.URL(getFileURL(tw.PageData.RevData, fname)), | |
| 794 | + | URL: tw.Config.getFileURL(tw.PageData.RevData, fname), | |
| 779 | 795 | Crumbs: crumbs, | |
| 780 | 796 | } | |
| 781 | 797 |
| ... | ... | @@ -794,29 +810,28 @@ func (tw *TreeWalker) NewTreeItem(entry *git.TreeEntry, curpath string, crumbs [ | |
| 794 | 810 | if len(lastCommits) > 0 { | |
| 795 | 811 | lc = lastCommits[0] | |
| 796 | 812 | } | |
| 797 | - | item.CommitURL = getCommitURL(lc.ID.String()) | |
| 813 | + | item.CommitURL = tw.Config.getCommitURL(lc.ID.String()) | |
| 798 | 814 | item.CommitID = getShortID(lc.ID.String()) | |
| 799 | 815 | item.Summary = lc.Summary() | |
| 800 | - | item.When = lc.Author.When.Format("02 Jan 06") | |
| 816 | + | item.When = lc.Author.When.Format(time.DateOnly) | |
| 801 | 817 | item.Author = lc.Author | |
| 802 | 818 | } | |
| 803 | 819 | ||
| 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)) | |
| 808 | 821 | if typ == git.ObjectTree { | |
| 809 | 822 | 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 | + | ), | |
| 814 | 829 | "index.html", | |
| 815 | 830 | ) | |
| 816 | 831 | } else if typ == git.ObjectBlob { | |
| 817 | 832 | item.Icon = FilenameToDevIcon(item.Name) | |
| 818 | 833 | } | |
| 819 | - | item.URL = template.URL(fpath) | |
| 834 | + | item.URL = fpath | |
| 820 | 835 | ||
| 821 | 836 | return item | |
| 822 | 837 | } |
| ... | ... | @@ -926,11 +941,11 @@ func (c *Config) writeRevision(repo *git.Repository, pageData *PageData, refs [] | |
| 926 | 941 | } | |
| 927 | 942 | logs = append(logs, &CommitData{ | |
| 928 | 943 | ParentID: parentID, | |
| 929 | - | URL: getCommitURL(commit.ID.String()), | |
| 944 | + | URL: c.getCommitURL(commit.ID.String()), | |
| 930 | 945 | ShortID: getShortID(commit.ID.String()), | |
| 931 | 946 | SummaryStr: commit.Summary(), | |
| 932 | 947 | AuthorStr: commit.Author.Name, | |
| 933 | - | WhenStr: commit.Author.When.Format("02 Jan 06"), | |
| 948 | + | WhenStr: commit.Author.When.Format(time.DateOnly), | |
| 934 | 949 | Commit: commit, | |
| 935 | 950 | Refs: tags, | |
| 936 | 951 | }) |
| ... | ... | @@ -954,6 +969,7 @@ func (c *Config) writeRevision(repo *git.Repository, pageData *PageData, refs [] | |
| 954 | 969 | entries := make(chan *TreeItem) | |
| 955 | 970 | subtrees := make(chan *TreeRoot) | |
| 956 | 971 | tw := &TreeWalker{ | |
| 972 | + | Config: c, | |
| 957 | 973 | PageData: pageData, | |
| 958 | 974 | Repo: repo, | |
| 959 | 975 | treeItem: entries, |
| ... | ... | @@ -1035,12 +1051,13 @@ func style(theme chroma.Style) string { | |
| 1035 | 1051 | func main() { | |
| 1036 | 1052 | var outdir = flag.String("out", "./public", "output directory") | |
| 1037 | 1053 | 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)") | |
| 1039 | 1055 | var themeFlag = flag.String("theme", "dracula", "theme to use for site") | |
| 1040 | 1056 | var labelFlag = flag.String("label", "", "pretty name for the subdir where we create the repo, default is last folder in --repo") | |
| 1041 | 1057 | var cloneFlag = flag.String("clone-url", "", "git clone URL") | |
| 1042 | 1058 | var homeFlag = flag.String("home-url", "", "URL for breadcumbs to get to list of repositories") | |
| 1043 | 1059 | var descFlag = flag.String("desc", "", "description for repo") | |
| 1060 | + | var rootRelativeFlag = flag.String("root-relative", "/", "html root relative") | |
| 1044 | 1061 | var maxCommitsFlag = flag.Int("max-commits", 0, "maximum number of commits to generate") | |
| 1045 | 1062 | var hideTreeLastCommitFlag = flag.Bool("hide-tree-last-commit", false, "dont calculate last commit for each file in the tree") | |
| 1046 | 1063 |