From b4095d108a2646bcf9e7fff64788b10d9bce8da3 Mon Sep 17 00:00:00 2001 From: Alan Pearce Date: Sat, 29 Jun 2024 16:48:40 +0200 Subject: build outside of working directory --- internal/builder/builder.go | 30 ++++++++++++++++++++---------- internal/builder/template.go | 15 ++++++++------- 2 files changed, 28 insertions(+), 17 deletions(-) (limited to 'internal/builder') diff --git a/internal/builder/builder.go b/internal/builder/builder.go index 256d422..a4e44c6 100644 --- a/internal/builder/builder.go +++ b/internal/builder/builder.go @@ -6,6 +6,7 @@ import ( "io" "os" "path" + "path/filepath" "slices" "time" @@ -86,13 +87,21 @@ func writerToFile(writer io.WriterTo, pathParts ...string) error { return nil } -func build(outDir string, config *config.Config) (*Result, error) { +func joinSourcePath(src string) func(string) string { + return func(rel string) string { + return filepath.Join(src, rel) + } +} + +func build(ioConfig *IOConfig, config *config.Config) (*Result, error) { + outDir := ioConfig.Destination + joinSource := joinSourcePath(ioConfig.Source) log.Debug("output", "dir", outDir) r := &Result{ Hashes: make([]string, 0), } - err := copyRecursive("static", outDir) + err := copyRecursive(joinSource("static"), outDir) if err != nil { return nil, errors.WithMessage(err, "could not copy static files") } @@ -101,7 +110,7 @@ func build(outDir string, config *config.Config) (*Result, error) { return nil, errors.WithMessage(err, "could not create post output directory") } log.Debug("reading posts") - posts, tags, err := content.ReadPosts("content", "post", outDir) + posts, tags, err := content.ReadPosts(joinSource("content"), "post", outDir) if err != nil { return nil, err } @@ -162,6 +171,7 @@ func build(outDir string, config *config.Config) (*Result, error) { log.Debug("rendering tags feed", "tag", tag) feed, err := renderFeed( + ioConfig.Source, fmt.Sprintf("%s - %s", config.Title, tag), config, matchingPosts, @@ -182,7 +192,7 @@ func build(outDir string, config *config.Config) (*Result, error) { sitemap.AddPath("/post/", lastMod) log.Debug("rendering feed") - feed, err := renderFeed(config.Title, config, posts, "feed") + feed, err := renderFeed(ioConfig.Source, config.Title, config, posts, "feed") if err != nil { return nil, errors.WithMessage(err, "could not render feed") } @@ -191,7 +201,7 @@ func build(outDir string, config *config.Config) (*Result, error) { } log.Debug("rendering feed styles") - feedStyles, err := renderFeedStyles() + feedStyles, err := renderFeedStyles(ioConfig.Source) if err != nil { return nil, errors.WithMessage(err, "could not render feed styles") } @@ -209,7 +219,7 @@ func build(outDir string, config *config.Config) (*Result, error) { r.Hashes = append(r.Hashes, h) log.Debug("rendering homepage") - _, text, err := content.GetPost(path.Join("content", "index.md")) + _, text, err := content.GetPost(joinSource(filepath.Join("content", "index.md"))) if err != nil { return nil, err } @@ -233,7 +243,7 @@ func build(outDir string, config *config.Config) (*Result, error) { } log.Debug("rendering robots.txt") - rob, err := renderRobotsTXT(config) + rob, err := renderRobotsTXT(ioConfig.Source, config) if err != nil { return nil, err } @@ -247,7 +257,7 @@ func build(outDir string, config *config.Config) (*Result, error) { func BuildSite(ioConfig *IOConfig, cfg *config.Config) (*Result, error) { if cfg == nil { var err error - cfg, err = config.GetConfig() + cfg, err = config.GetConfig(ioConfig.Source) if err != nil { return nil, errors.WithMessage(err, "could not get config") } @@ -256,7 +266,7 @@ func BuildSite(ioConfig *IOConfig, cfg *config.Config) (*Result, error) { compressFiles = !ioConfig.Development templates.Setup() - loadCSS() + loadCSS(ioConfig.Source) - return build(ioConfig.Destination, cfg) + return build(ioConfig, cfg) } diff --git a/internal/builder/template.go b/internal/builder/template.go index d46844d..0be3214 100644 --- a/internal/builder/template.go +++ b/internal/builder/template.go @@ -27,8 +27,8 @@ var ( } ) -func loadCSS() { - bytes, err := os.ReadFile("templates/style.css") +func loadCSS(source string) { + bytes, err := os.ReadFile(filepath.Join(source, "templates/style.css")) if err != nil { panic(err) } @@ -66,9 +66,9 @@ func (q *QueryDocument) Find(selector string) *QuerySelection { return &QuerySelection{q.Document.Find(selector)} } -func renderRobotsTXT(config *config.Config) (io.Reader, error) { +func renderRobotsTXT(source string, config *config.Config) (io.Reader, error) { r, w := io.Pipe() - tpl, err := template.ParseFiles("templates/robots.tmpl") + tpl, err := template.ParseFiles(filepath.Join(source, "templates/robots.tmpl")) if err != nil { return nil, err } @@ -86,12 +86,13 @@ func renderRobotsTXT(config *config.Config) (io.Reader, error) { } func renderFeed( + source string, title string, config *config.Config, posts []content.Post, specific string, ) (io.Reader, error) { - reader, err := loadTemplate("templates/feed.xml") + reader, err := loadTemplate(filepath.Join(source, "templates/feed.xml")) if err != nil { return nil, err } @@ -153,8 +154,8 @@ func renderFeed( return strings.NewReader(doc.OutputXML(true)), nil } -func renderFeedStyles() (*strings.Reader, error) { - tpl, err := template.ParseFiles("templates/feed-styles.xsl") +func renderFeedStyles(source string) (*strings.Reader, error) { + tpl, err := template.ParseFiles(filepath.Join(source, "templates/feed-styles.xsl")) if err != nil { return nil, err } -- cgit 1.4.1