Skip to content

Commit

Permalink
feat(render): allow renaming of individual files in front matter
Browse files Browse the repository at this point in the history
  • Loading branch information
Depado committed Sep 19, 2023
1 parent 301d343 commit bfa182a
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 180 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -440,5 +440,4 @@ to copy the file and not render it.

- Per-file config:
- [ ] Allow more complex conditional rendering
- [ ] Ability to rename files once rendered
- [ ] Unable to access sub variables in condition
66 changes: 0 additions & 66 deletions conf/command.go

This file was deleted.

82 changes: 0 additions & 82 deletions conf/command_test.go

This file was deleted.

17 changes: 13 additions & 4 deletions conf/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,15 @@ type File struct {
NewPath string
Info os.FileInfo
Renderers []*ConfigFile
Metadata *Config
Metadata *FileMetadata
Ctx InputCtx
}

type FileMetadata struct {
Config `yaml:",inline"`
Rename string `yaml:"rename"`
}

// templateFuncMaps adds just simple strings filter to pretty print string
var templateFuncMaps template.FuncMap = template.FuncMap{
"title": func(str string) string {
Expand Down Expand Up @@ -89,11 +94,12 @@ func (f *File) ParseFrontMatter() error {
}

// Parse stuff to configuration
var r Config
if err = yaml.Unmarshal([]byte(in), &r); err != nil {
var fm FileMetadata
if err = yaml.Unmarshal([]byte(in), &fm); err != nil {
return err
}
f.Metadata = &r
f.Metadata = &fm

if f.Metadata.Variables != nil && len(*f.Metadata.Variables) > 0 {
utils.OkPrintln("Variables for single file", color.YellowString(f.Path))
f.Metadata.Variables.FillPrompt("", f.Ctx)
Expand Down Expand Up @@ -225,6 +231,9 @@ func (f *File) Render() error {
if f.Metadata.Variables != nil {
f.Metadata.Variables.AddToCtx("", ctx)
}
if f.Metadata.Rename != "" {
f.NewPath = filepath.Join(filepath.Dir(f.NewPath), f.Metadata.Rename)
}
}
if ignore {
utils.OkPrintln("Ignored ", color.GreenString(f.NewPath))
Expand Down
30 changes: 3 additions & 27 deletions conf/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,15 @@ import (
"strings"

yaml "gopkg.in/yaml.v2"

"github.com/Depado/quokka/utils"
)

// Root is a ConfigFile with extra information. It should be located at the root
// of the template
type Root struct {
ConfigFile `yaml:",inline"`
Name string `yaml:"name"`
Version string `yaml:"version"`
Description string `yaml:"description"`
After []Command `yaml:"after"`
Name string `yaml:"name"`
Version string `yaml:"version"`
Description string `yaml:"description"`
}

// Parse will parse the yaml file and store its result in the root config
Expand All @@ -32,27 +29,6 @@ func (r *Root) Parse() error {
return yaml.Unmarshal(out, r)
}

// ExecuteCommands will execute the commands in the newly rendered directory
func (r Root) ExecuteCommands(dir string) {
var err error

if err = os.Chdir(dir); err != nil {
utils.FatalPrintln("Couldn't change directory:", err)
}

for _, cmd := range r.After {
if cmd.Cmd != "" {
if cmd.If != "" && r.Variables != nil {
if v := r.Variables.FindNamed(cmd.If); v != nil && v.True() {
cmd.Run()
}
} else {
cmd.Run()
}
}
}
}

// NewPath adds the path where the file should be rendered according to the root
func (r Root) NewPath(f *File, new string) {
f.NewPath = filepath.ToSlash(strings.Replace(f.Path, r.File.Dir, new, 1))
Expand Down

0 comments on commit bfa182a

Please sign in to comment.