about summary refs log tree commit diff stats
path: root/internal
diff options
context:
space:
mode:
authorAlan Pearce2024-06-29 16:48:40 +0200
committerAlan Pearce2024-06-29 16:48:40 +0200
commitb4095d108a2646bcf9e7fff64788b10d9bce8da3 (patch)
treef45554857a3128922b665ab0fb04150380c0d44e /internal
parente8221541404a8d2ca0758d48ba5fe9c85f9067ac (diff)
downloadwebsite-b4095d108a2646bcf9e7fff64788b10d9bce8da3.tar.lz
website-b4095d108a2646bcf9e7fff64788b10d9bce8da3.tar.zst
website-b4095d108a2646bcf9e7fff64788b10d9bce8da3.zip
build outside of working directory
Diffstat (limited to 'internal')
-rw-r--r--internal/builder/builder.go30
-rw-r--r--internal/builder/template.go15
-rw-r--r--internal/config/config.go8
-rw-r--r--internal/server/server.go7
4 files changed, 36 insertions, 24 deletions
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
 	}
diff --git a/internal/config/config.go b/internal/config/config.go
index b429333..ca4782d 100644
--- a/internal/config/config.go
+++ b/internal/config/config.go
@@ -3,6 +3,7 @@ package config
 import (
 	"io/fs"
 	"net/url"
+	"path/filepath"
 	"website/internal/log"
 
 	"github.com/BurntSushi/toml"
@@ -49,10 +50,11 @@ type Config struct {
 	Menus map[string][]MenuItem
 }
 
-func GetConfig() (*Config, error) {
+func GetConfig(dir string) (*Config, error) {
 	config := Config{}
-	log.Debug("reading config.toml")
-	_, err := toml.DecodeFile("config.toml", &config)
+	filename := filepath.Join(dir, "config.toml")
+	log.Debug("reading config", "filename", filename)
+	_, err := toml.DecodeFile(filename, &config)
 	if err != nil {
 		switch t := err.(type) {
 		case *fs.PathError:
diff --git a/internal/server/server.go b/internal/server/server.go
index 3c43812..c7b5659 100644
--- a/internal/server/server.go
+++ b/internal/server/server.go
@@ -99,7 +99,6 @@ func rebuild(builderConfig *builder.IOConfig, config *cfg.Config) error {
 
 func New(runtimeConfig *Config) (*Server, error) {
 	builderConfig := &builder.IOConfig{
-		Source:      "content",
 		Destination: runtimeConfig.Root,
 		Development: runtimeConfig.Development,
 	}
@@ -114,19 +113,19 @@ func New(runtimeConfig *Config) (*Server, error) {
 		if err != nil {
 			return nil, err
 		}
-		err = os.Chdir(vcsConfig.LocalPath)
+		err = os.Chdir(runtimeConfig.Root)
 		if err != nil {
 			return nil, err
 		}
 
-		builderConfig.Source = filepath.Join(vcsConfig.LocalPath, "content")
+		builderConfig.Source = vcsConfig.LocalPath
 
 		publicDir := filepath.Join(runtimeConfig.Root, "public")
 		builderConfig.Destination = publicDir
 		runtimeConfig.Root = publicDir
 	}
 
-	config, err := cfg.GetConfig()
+	config, err := cfg.GetConfig(builderConfig.Source)
 	if err != nil {
 		return nil, errors.WithMessage(err, "error parsing configuration file")
 	}