diff --git a/README.md b/README.md index d701af4..37d14ed 100644 --- a/README.md +++ b/README.md @@ -7,12 +7,12 @@ A simple CLI tool to generate a static website from a - [X] Recursively traverse the `recipes` directory. - [X] Add a proper CLI. -- [ ] Add a default CSS file. +- [X] Add a default CSS file. - [ ] Improve the steps rendering logic to avoid doing all of it inside a template function. - [X] Add image support for steps and the recipe. - [ ] Add an `index.html` file that lists all the recipes. -- [ ] Embed the default `style.css` file. +- [X] Embed the default `style.css` file. - [ ] Add command to copy the embedded templates to an external directory. - [ ] Allow to read the templates from an external directory. diff --git a/sandwitchboard.go b/sandwitchboard.go index e4714a2..14d1125 100644 --- a/sandwitchboard.go +++ b/sandwitchboard.go @@ -130,6 +130,10 @@ func executeTemplateToFile(tmplName string, data any, filename string) error { return nil } +func getRelpath(name string) string { + return strings.Repeat("../", strings.Count(name, "/")) +} + func copyFile(srcpath, dstpath string) error { src, err := os.Open(srcpath) if err != nil { @@ -137,6 +141,10 @@ func copyFile(srcpath, dstpath string) error { } defer src.Close() + return copyToFile(src, dstpath) +} + +func copyToFile(src io.Reader, dstpath string) error { dst, err := os.Create(dstpath) if err != nil { return fmt.Errorf("cannot open dst file %q: %w", dstpath, err) @@ -144,7 +152,7 @@ func copyFile(srcpath, dstpath string) error { defer dst.Close() if _, err := io.Copy(dst, src); err != nil { - return fmt.Errorf("cannot copy file from %q to %q: %w", srcpath, dstpath, err) + return fmt.Errorf("cannot copy to file %q: %w", dstpath, err) } return dst.Sync() @@ -216,7 +224,7 @@ func (g *generateCmd) Run() error { slog.Debug("Parsed file", "path", path, "steps", len(recipe.Steps)) recipeDistPath := filepath.Join(g.Output, filepath.Dir(relpath), fmt.Sprintf("%s.html", recipeName)) - data := map[string]any{"name": recipeName, "recipe": recipe, "path": path} + data := map[string]any{"name": recipeName, "recipe": recipe, "path": path, "relpath": getRelpath(relpath)} slog.Debug("Executing template", "recipeName", recipeName, "recipeWebPath", recipeDistPath) if err := executeTemplateToFile("recipe", data, recipeDistPath); err != nil { return fmt.Errorf("cannot execute template \"recipe\" to file %q: %w", recipeDistPath, err) @@ -224,10 +232,18 @@ func (g *generateCmd) Run() error { return nil }) - if walkErr != nil { return fmt.Errorf("error while walking directory %q: %w", g.Path, walkErr) } + styleFile, err := embedTmpl.Open(filepath.Join("templates", "style.css")) + if err != nil { + return fmt.Errorf("cannot read style.css embed file: %w", err) + } + + if err := copyToFile(styleFile, filepath.Join(g.Output, "style.css")); err != nil { + return fmt.Errorf("cannot copy style.css: %w", err) + } + return nil } diff --git a/templates/recipe.html.tmpl b/templates/recipe.html.tmpl index 9532c30..d830a2b 100644 --- a/templates/recipe.html.tmpl +++ b/templates/recipe.html.tmpl @@ -4,8 +4,8 @@