Something went wrong. Try again.
A static site generator for git repositories that integrates with git-bug to track issues. This is it.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211package pgit
import ( "fmt" "html/template" "os" "path/filepath" "sort" "strings" "time"
git "github.com/gogs/git-module")
type TreeRoot struct { Path string Items []*TreeItem Crumbs []*Breadcrumb}
type TreeWalker struct { treeItem chan *TreeItem tree chan *TreeRoot HideTreeLastCommit bool PageData *PageData Repo *git.Repository Config *Config}
type Breadcrumb struct { Text string URL template.URL IsLast bool}
func (tw *TreeWalker) CalcBreadcrumbs(curpath string) []*Breadcrumb { if curpath == "" { return []*Breadcrumb{} } parts := strings.Split(curpath, string(os.PathSeparator)) rootURL := tw.Config.CompileURL( GetTreeBaseDir(*tw.PageData.RevData), "index.html", )
crumbs := make([]*Breadcrumb, len(parts)+1) crumbs[0] = &Breadcrumb{ URL: rootURL, Text: tw.PageData.Repo.RepoName, }
cur := "" for idx, d := range parts { crumb := filepath.Join(GetFileBaseDir(*tw.PageData.RevData), cur, d) crumbUrl := tw.Config.CompileURL(crumb, "index.html") crumbs[idx+1] = &Breadcrumb{ Text: d, URL: crumbUrl, } if idx == len(parts)-1 { crumbs[idx+1].IsLast = true } cur = filepath.Join(cur, d) }
return crumbs}
func FilenameToDevIcon(filename string) string { ext := filepath.Ext(filename) extMapper := map[string]string{ ".html": "html5", ".go": "go", ".py": "python", ".css": "css3", ".js": "javascript", ".md": "markdown", ".ts": "typescript", ".tsx": "react", ".jsx": "react", }
nameMapper := map[string]string{ "Makefile": "cmake", "Dockerfile": "docker", }
icon := extMapper[ext] if icon == "" { icon = nameMapper[filename] }
return fmt.Sprintf("devicon-%s-original", icon)}
func (tw *TreeWalker) NewTreeItem(entry *git.TreeEntry, curpath string, crumbs []*Breadcrumb) *TreeItem { typ := entry.Type() fname := filepath.Join(curpath, entry.Name()) item := &TreeItem{ Size: ToPretty(entry.Size()), Name: entry.Name(), Path: fname, Entry: entry, URL: tw.Config.GetFileURL(*tw.PageData.RevData, fname), Crumbs: crumbs, Author: &git.Signature{ Name: "unknown", }, }
if !tw.HideTreeLastCommit { id := (*tw.PageData.RevData).ID() lastCommits, err := tw.Repo.RevList([]string{id}, git.RevListOptions{ Path: item.Path, CommandOptions: git.CommandOptions{Args: []string{"-1"}}, }) Bail(err)
if len(lastCommits) > 0 { lc := lastCommits[0] item.CommitURL = tw.Config.GetCommitURL(lc.ID.String()) item.CommitID = GetShortID(lc.ID.String()) item.Summary = lc.Summary() item.When = lc.Author.When.Format(time.DateOnly) item.WhenISO = lc.Author.When.UTC().Format(time.RFC3339) item.WhenDisplay = FormatDateForDisplay(lc.Author.When) item.Author = lc.Author } }
fpath := tw.Config.GetFileURL(*tw.PageData.RevData, fmt.Sprintf("%s.html", fname)) switch typ { case git.ObjectTree: item.IsDir = true fpath = tw.Config.CompileURL( filepath.Join( GetFileBaseDir(*tw.PageData.RevData), curpath, entry.Name(), ), "index.html", ) case git.ObjectBlob: item.Icon = FilenameToDevIcon(item.Name) } item.URL = fpath
return item}
func (tw *TreeWalker) Walk(tree *git.Tree, curpath string) { entries, err := tree.Entries() Bail(err)
crumbs := tw.CalcBreadcrumbs(curpath) treeEntries := []*TreeItem{} for _, entry := range entries { typ := entry.Type() item := tw.NewTreeItem(entry, curpath, crumbs)
switch typ { case git.ObjectTree: item.IsDir = true re, _ := tree.Subtree(entry.Name()) tw.Walk(re, item.Path) treeEntries = append(treeEntries, item) tw.treeItem <- item case git.ObjectBlob: treeEntries = append(treeEntries, item) tw.treeItem <- item } }
sort.Slice(treeEntries, func(i, j int) bool { nameI := treeEntries[i].Name nameJ := treeEntries[j].Name if treeEntries[i].IsDir && treeEntries[j].IsDir { return nameI < nameJ }
if treeEntries[i].IsDir && !treeEntries[j].IsDir { return true }
if !treeEntries[i].IsDir && treeEntries[j].IsDir { return false }
return nameI < nameJ })
fpath := filepath.Join( GetFileBaseDir(*tw.PageData.RevData), curpath, ) if curpath == "" { fpath = GetTreeBaseDir(*tw.PageData.RevData) }
tw.tree <- &TreeRoot{ Path: fpath, Items: treeEntries, Crumbs: crumbs, }
if curpath == "" { close(tw.tree) close(tw.treeItem) }}