package progdoc import ( "bytes" "log" "os" "path/filepath" "strings" "text/template" ) func Path(path string) PathBuilder { return PathBuilder{path: path} } func (pb PathBuilder) File(file string) Option { return Option{ configSitemap: func(sm *siteMap) error { _, err := os.Stat(file) if err != nil { return err } // TODO: support things other than markdown src, err := inferSourceFromFilename(file) if err != nil { return err } log.Printf("Page '%s' -> %s", pb.path, file) sm.Pages = append(sm.Pages, sitePage{ Path: pb.path, Source: stdLayoutSource{MainSource: src}, }) return nil }, } } func (pb PathBuilder) GoFiles(dir, templateFile string) Option { return Option{ configSitemap: func(sm *siteMap) error { tmplBytes, err := os.ReadFile(templateFile) if err != nil { return err } tmpl, err := template.New("").Parse(string(tmplBytes)) if err != nil { return err } if err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error { if err != nil { return err } if !info.IsDir() && info.Mode().IsRegular() { relPath, err := filepath.Rel(dir, strings.TrimSuffix(path, filepath.Ext(path))) if err != nil { return nil } targetPath := filepath.Join(pb.path, relPath) absName, err := filepath.Abs(path) if err != nil { return err } else if !strings.HasSuffix(absName, ".go") { return nil } goBts, err := os.ReadFile(path) if err != nil { return err } goFileDoc, err := parseGoFileDocs(bytes.NewReader(goBts)) if err != nil { return err } if !goFileDoc.HasDocs() { return nil } sm.Pages = append(sm.Pages, sitePage{ Path: targetPath, Source: stdLayoutSource{ MainSource: mdTemplateSource{ Template: tmpl, Data: goFileDoc, }, }, }) } return nil }); err != nil { return err } return nil }, } } func (pb PathBuilder) Dir(dir string) Option { return Option{ configSitemap: func(sm *siteMap) error { return filepath.Walk(dir, func(path string, info os.FileInfo, err error) error { if err != nil { return err } if !info.IsDir() && info.Mode().IsRegular() { src, err := inferSourceFromFilename(path) if err != nil { return nil } relPath, err := filepath.Rel(dir, strings.TrimSuffix(path, filepath.Ext(path))) if err != nil { return nil } targetPath := filepath.Join(pb.path, relPath) log.Printf("Page '%s' -> %s", targetPath, path) sm.Pages = append(sm.Pages, sitePage{ Path: targetPath, Source: stdLayoutSource{MainSource: src}, }) } return nil }) }, } }