Adds and embeds a default style.css file

This commit is contained in:
Miguel de la Cruz 2024-07-03 20:55:41 +02:00
parent c251e1543d
commit 2a17689a9f
4 changed files with 27 additions and 7 deletions

View file

@ -7,12 +7,12 @@ A simple CLI tool to generate a static website from a
- [X] Recursively traverse the `recipes` directory. - [X] Recursively traverse the `recipes` directory.
- [X] Add a proper CLI. - [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 - [ ] Improve the steps rendering logic to avoid doing all of it
inside a template function. inside a template function.
- [X] Add image support for steps and the recipe. - [X] Add image support for steps and the recipe.
- [ ] Add an `index.html` file that lists all the recipes. - [ ] 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 - [ ] Add command to copy the embedded templates to an external
directory. directory.
- [ ] Allow to read the templates from an external directory. - [ ] Allow to read the templates from an external directory.

View file

@ -130,6 +130,10 @@ func executeTemplateToFile(tmplName string, data any, filename string) error {
return nil return nil
} }
func getRelpath(name string) string {
return strings.Repeat("../", strings.Count(name, "/"))
}
func copyFile(srcpath, dstpath string) error { func copyFile(srcpath, dstpath string) error {
src, err := os.Open(srcpath) src, err := os.Open(srcpath)
if err != nil { if err != nil {
@ -137,6 +141,10 @@ func copyFile(srcpath, dstpath string) error {
} }
defer src.Close() defer src.Close()
return copyToFile(src, dstpath)
}
func copyToFile(src io.Reader, dstpath string) error {
dst, err := os.Create(dstpath) dst, err := os.Create(dstpath)
if err != nil { if err != nil {
return fmt.Errorf("cannot open dst file %q: %w", dstpath, err) return fmt.Errorf("cannot open dst file %q: %w", dstpath, err)
@ -144,7 +152,7 @@ func copyFile(srcpath, dstpath string) error {
defer dst.Close() defer dst.Close()
if _, err := io.Copy(dst, src); err != nil { 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() return dst.Sync()
@ -216,7 +224,7 @@ func (g *generateCmd) Run() error {
slog.Debug("Parsed file", "path", path, "steps", len(recipe.Steps)) slog.Debug("Parsed file", "path", path, "steps", len(recipe.Steps))
recipeDistPath := filepath.Join(g.Output, filepath.Dir(relpath), fmt.Sprintf("%s.html", recipeName)) 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) slog.Debug("Executing template", "recipeName", recipeName, "recipeWebPath", recipeDistPath)
if err := executeTemplateToFile("recipe", data, recipeDistPath); err != nil { if err := executeTemplateToFile("recipe", data, recipeDistPath); err != nil {
return fmt.Errorf("cannot execute template \"recipe\" to file %q: %w", recipeDistPath, err) return fmt.Errorf("cannot execute template \"recipe\" to file %q: %w", recipeDistPath, err)
@ -224,10 +232,18 @@ func (g *generateCmd) Run() error {
return nil return nil
}) })
if walkErr != nil { if walkErr != nil {
return fmt.Errorf("error while walking directory %q: %w", g.Path, walkErr) 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 return nil
} }

View file

@ -4,8 +4,8 @@
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" /> <meta name="viewport" content="width=device-width, initial-scale=1" />
<title>{{.name}}</title> <title>{{.name}}</title>
<link rel="icon" type="image/png" href="favicon.png" /> <link rel="icon" type="image/png" href="{{.relpath}}favicon.png" />
<link rel="stylesheet" type="text/css" href="style.css" /> <link rel="stylesheet" type="text/css" href="{{.relpath}}style.css" />
</head> </head>
<body> <body>
<h1>{{.name}}</h1> <h1>{{.name}}</h1>

4
templates/style.css Normal file
View file

@ -0,0 +1,4 @@
body {
background-color: lightgrey;
font-family: monospace;
}