diff --git a/cmd/doc/main.go b/cmd/doc/main.go index 6a15591..dfe681b 100644 --- a/cmd/doc/main.go +++ b/cmd/doc/main.go @@ -9,8 +9,8 @@ import ( func main() { if err := progdoc.Site( progdoc.Meta(progdoc.SiteMeta{Title: "Prog Doc"}), - progdoc.SourceFile("/", "README.md"), - progdoc.SourceDir("/", "_site"), + progdoc.Path("/").File("README.md"), + progdoc.Path("/").Dir("_site"), progdoc.StaticFile("/go.mod", "go.mod"), ).Generate(); err != nil { log.Fatal(err) diff --git a/go.mod b/go.mod index 77fa040..50fe220 100644 --- a/go.mod +++ b/go.mod @@ -13,6 +13,7 @@ require ( github.com/asaskevich/govalidator v0.0.0-20200907205600-7a23bdc65eef // indirect github.com/cenkalti/backoff v2.2.1+incompatible // indirect github.com/cenkalti/backoff/v4 v4.0.2 // indirect + github.com/davecgh/go-spew v1.1.1 // indirect github.com/dgrijalva/jwt-go v3.2.0+incompatible // indirect github.com/go-openapi/analysis v0.19.16 // indirect github.com/go-openapi/errors v0.19.9 // indirect @@ -31,8 +32,10 @@ require ( github.com/netlify/netlify-go v0.1.11 // indirect github.com/netlify/open-api/v2 v2.46.0 // indirect github.com/pkg/errors v0.9.1 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect github.com/rsc/goversion v1.2.0 // indirect github.com/sirupsen/logrus v1.9.4 // indirect + github.com/stretchr/testify v1.11.1 // indirect github.com/yuin/goldmark v1.7.16 // indirect go.mongodb.org/mongo-driver v1.4.4 // indirect golang.org/x/net v0.7.0 // indirect @@ -40,4 +43,5 @@ require ( golang.org/x/sys v0.13.0 // indirect golang.org/x/text v0.7.0 // indirect gopkg.in/yaml.v2 v2.4.0 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index eb4bc9f..0087231 100644 --- a/go.sum +++ b/go.sum @@ -69,6 +69,7 @@ github.com/coreos/go-systemd v0.0.0-20190321100706-95778dfbb74e/go.mod h1:F5haX7 github.com/coreos/pkg v0.0.0-20180928190104-399ea9e2e55f/go.mod h1:E3G3o1h8I7cfcXa63jLwjI0eiQQMgzzUDFVpN/nH/eA= github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumCAMpl/TFQ4/5kLM= github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ= @@ -330,6 +331,7 @@ github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINE github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/posener/complete v1.1.1/go.mod h1:em0nMJCgc9GFtwrmVmEMR/ZL6WyhyjMBndrE9hABlRI= github.com/pquerna/cachecontrol v0.0.0-20180517163645-1555304b9b35/go.mod h1:prYjPmNq4d1NPVmpShWobRqXY3q7Vp+80DqgxxUrUIA= @@ -385,6 +387,8 @@ github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/ github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= +github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U= +github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U= github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw= github.com/tidwall/pretty v1.0.0/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk= github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U= @@ -616,6 +620,7 @@ gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.0-20200605160147-a5ece683394c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= honnef.co/go/tools v0.0.0-20190106161140-3f1c8253044a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= diff --git a/mdsource.go b/mdsource.go index 7dc937a..8712c43 100644 --- a/mdsource.go +++ b/mdsource.go @@ -1,6 +1,8 @@ package progdoc import ( + "bytes" + "html/template" "io" "os" @@ -32,3 +34,27 @@ func (m mdSource) HTML(w io.Writer, srcCtx *SourceCtx) error { return md.Convert(srcBytes, w) } + +type mdTemplateSource struct { + Template *template.Template + Data interface{} +} + +func (m mdTemplateSource) HTML(w io.Writer, srcCtx *SourceCtx) error { + var tmplOut bytes.Buffer + if err := m.Template.Execute(&tmplOut, m.Data); err != nil { + return err + } + + md := goldmark.New( + goldmark.WithExtensions(extension.GFM), + goldmark.WithParserOptions( + parser.WithAutoHeadingID(), + ), + goldmark.WithRendererOptions( + html.WithUnsafe(), + ), + ) + + return md.Convert(tmplOut.Bytes(), w) +} diff --git a/siteopts.go b/siteopts.go index 4b5b82c..581ba35 100644 --- a/siteopts.go +++ b/siteopts.go @@ -21,29 +21,8 @@ func Meta(meta SiteMeta) Option { } } -func SourceFile(path, 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", path, file) - sm.Pages = append(sm.Pages, sitePage{ - Path: path, - Source: stdLayoutSource{MainSource: src}, - }) - - return nil - }, - } +type PathBuilder struct { + path string } func StaticFile(path, file string) Option { @@ -72,37 +51,6 @@ func StaticFile(path, file string) Option { } } -func SourceDir(basePath, 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(basePath, relPath) - - log.Printf("Page '%s' -> %s", targetPath, path) - sm.Pages = append(sm.Pages, sitePage{ - Path: relPath, - Source: stdLayoutSource{MainSource: src}, - }) - } - return nil - }) - }, - } -} - func inferSourceFromFilename(fname string) (pageSource, error) { absName, err := filepath.Abs(fname) if err != nil {