Switch to one file per mod and added additional options for go parts

This commit is contained in:
Leon Mika 2026-02-08 10:36:37 +11:00
parent 30f42db6a6
commit 1d52b0784c
2 changed files with 46 additions and 13 deletions

View file

@ -16,12 +16,39 @@ type goFilePart struct {
Body string
}
func (g goFilePart) FirstMeta(name string) string {
for _, meta := range g.Meta {
if meta.Name == name {
return meta.Value
}
}
return ""
}
type goFileDocs struct {
Parts []goFilePart
}
type allGoFileDocs struct {
Files []goFileDocs
func (g goFileDocs) PartsWithMeta(name string) []goFilePart {
var parts []goFilePart
for _, part := range g.Parts {
if part.FirstMeta(name) != "" {
parts = append(parts, part)
}
}
return parts
}
func (g goFileDocs) FirstPartWithMeta(name string) goFilePart {
parts := g.PartsWithMeta(name)
if len(parts) > 0 {
return parts[0]
}
return goFilePart{}
}
func (g goFileDocs) HasDocs() bool {
return len(g.Parts) > 0
}
func parseGoFileDocs(r io.Reader) (goFileDocs, error) {

View file

@ -41,8 +41,6 @@ func (pb PathBuilder) File(file string) Option {
func (pb PathBuilder) GoFiles(dir, templateFile string) Option {
return Option{
configSitemap: func(sm *siteMap) error {
var allDocs allGoFileDocs
tmplBytes, err := os.ReadFile(templateFile)
if err != nil {
return err
@ -57,6 +55,11 @@ func (pb PathBuilder) GoFiles(dir, templateFile string) Option {
return err
}
if !info.IsDir() && info.Mode().IsRegular() {
relPath, err := filepath.Rel(dir, strings.TrimSuffix(path, filepath.Ext(path)))
if err != nil {
return nil
}
absName, err := filepath.Abs(path)
if err != nil {
return err
@ -72,20 +75,23 @@ func (pb PathBuilder) GoFiles(dir, templateFile string) Option {
if err != nil {
return err
}
allDocs.Files = append(allDocs.Files, goFileDoc)
if !goFileDoc.HasDocs() {
return nil
}
sm.Pages = append(sm.Pages, sitePage{
Path: relPath,
Source: mdTemplateSource{
Template: tmpl,
Data: goFileDoc,
},
})
}
return nil
}); err != nil {
return err
}
sm.Pages = append(sm.Pages, sitePage{
Path: pb.path,
Source: mdTemplateSource{
Template: tmpl,
Data: allDocs,
},
})
return nil
},
}