From 1d52b0784c9cd3e5c6654d7cceedfbec4a2bf777 Mon Sep 17 00:00:00 2001 From: Leon Mika Date: Sun, 8 Feb 2026 10:36:37 +1100 Subject: [PATCH] Switch to one file per mod and added additional options for go parts --- gofilereader.go | 31 +++++++++++++++++++++++++++++-- pathbuilder.go | 28 +++++++++++++++++----------- 2 files changed, 46 insertions(+), 13 deletions(-) diff --git a/gofilereader.go b/gofilereader.go index 8fa668c..8b3b0f9 100644 --- a/gofilereader.go +++ b/gofilereader.go @@ -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) { diff --git a/pathbuilder.go b/pathbuilder.go index f86792b..cbbfdad 100644 --- a/pathbuilder.go +++ b/pathbuilder.go @@ -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 }, }