Adds readme, license and contributing parsing

This commit is contained in:
Miguel de la Cruz 2024-07-14 12:02:48 +02:00
parent 741709c8d5
commit 1d980ae947
2 changed files with 37 additions and 14 deletions

View file

@ -13,7 +13,7 @@ Project heavily inspired by the amazing
- [ ] Add a base url to avoid relative links if provided.
- [X] Generate the index html file for the `index` subcommand.
- [X] Generate the log html file for a repository.
- [ ] Detect and link README, LICENSE and CONTRIBUTING files.
- [X] Detect and link README, LICENSE and CONTRIBUTING files.
- [X] Generate the files html file and file structure.
- [ ] Generate the refs html file.
- [X] Add a proper CLI parsing and subcommands.

View file

@ -27,12 +27,12 @@ type RepoDir struct {
// ToDo: replace has* with the filename, as it can be bare or .md
type RepoInfo struct {
Name string
Description string
Url string
HasReadme bool
HasLicense bool
HasContributing bool
Name string
Description string
Url string
Readme string
License string
Contributing string
}
type CommitFile struct {
@ -69,6 +69,9 @@ type CommitInfo struct {
var embedTmpl embed.FS
var timeShortFormatStr = "2006-01-02 15:04"
var timeLongFormatStr = time.RFC1123
var readmeNames = []string{"README", "README.md", "README.txt"}
var licenseNames = []string{"LICENSE", "LICENSE.txt"}
var contributingNames = []string{"CONTRIBUTING", "CONTRIBUTING.txt"}
var funcMap = template.FuncMap{
"inc": func(i int) int {
return i + 1
@ -78,16 +81,16 @@ var funcMap = template.FuncMap{
menu += " | <a href=\"" + relpath + "files.html\">Files</a>"
menu += " | <a href=\"" + relpath + "refs.html\">Refs</a>"
if repoInfo.HasReadme {
menu += "| <a href=\"" + relpath + "file/README.html\">README</a>"
if repoInfo.Readme != "" {
menu += " | <a href=\"" + relpath + "file/" + repoInfo.Readme + ".html\">README</a>"
}
if repoInfo.HasLicense {
menu += "| <a href=\"" + relpath + "file/LICENSE.html\">LICENSE</a>"
if repoInfo.License != "" {
menu += " | <a href=\"" + relpath + "file/" + repoInfo.License + ".html\">LICENSE</a>"
}
if repoInfo.HasContributing {
menu += "| <a href=\"" + relpath + "file/CONTRIBUTING.html\">CONTRIBUTING</a>"
if repoInfo.Contributing != "" {
menu += " | <a href=\"" + relpath + "file/" + repoInfo.Contributing + ".html\">CONTRIBUTING</a>"
}
return template.HTML(menu)
@ -271,7 +274,27 @@ func generateRepo(path string, logLimit int) error {
return fmt.Errorf("cannot get commit for hash %s: %w", head.Hash(), err)
}
// ToDo: populate hasReadme, hasLicense and hasContributing
// populate Readme, License and Contributing
for _, name := range readmeNames {
if _, err := c.File(name); err == nil {
repoInfo.Readme = name
break
}
}
for _, name := range licenseNames {
if _, err := c.File(name); err == nil {
repoInfo.License = name
break
}
}
for _, name := range contributingNames {
if _, err := c.File(name); err == nil {
repoInfo.Contributing = name
break
}
}
tree, err := c.Tree()
if err != nil {