mirror of
https://github.com/facundoolano/jorge.git
synced 2024-12-26 21:58:51 +01:00
cleanup directory vars
This commit is contained in:
parent
97b6bc5111
commit
e821866d5d
2 changed files with 22 additions and 32 deletions
|
@ -12,7 +12,7 @@ import (
|
|||
|
||||
const SRC_DIR = "src"
|
||||
const TARGET_DIR = "target"
|
||||
const LAYOUT_DIR = "layouts"
|
||||
const LAYOUTS_DIR = "layouts"
|
||||
const FILE_RW_MODE = 0777
|
||||
|
||||
func Init() error {
|
||||
|
@ -25,24 +25,13 @@ func Init() error {
|
|||
}
|
||||
|
||||
// Read the files in src/ render them and copy the result to target/
|
||||
// FIXME pass src and target by arg
|
||||
// TODO add root dir override support
|
||||
func Build() error {
|
||||
_, err := os.ReadDir(SRC_DIR)
|
||||
if os.IsNotExist(err) {
|
||||
return fmt.Errorf("missing %s directory", SRC_DIR)
|
||||
} else if err != nil {
|
||||
return fmt.Errorf("couldn't read %s", SRC_DIR)
|
||||
}
|
||||
|
||||
// TODO add dir override support
|
||||
site, err := site.Load(".")
|
||||
site, err := site.Load(SRC_DIR, LAYOUTS_DIR)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return writeTarget(site)
|
||||
}
|
||||
|
||||
func writeTarget(site *site.Site) error {
|
||||
// clear previous target contents
|
||||
os.RemoveAll(TARGET_DIR)
|
||||
os.Mkdir(TARGET_DIR, FILE_RW_MODE)
|
||||
|
@ -56,7 +45,6 @@ func writeTarget(site *site.Site) error {
|
|||
os.MkdirAll(targetPath, FILE_RW_MODE)
|
||||
} else {
|
||||
|
||||
// FIXME replace with method
|
||||
if templ, ok := site.TemplateIndex[path]; ok {
|
||||
// if a template was found at source, render it
|
||||
content, err := site.Render(templ)
|
||||
|
@ -66,11 +54,11 @@ func writeTarget(site *site.Site) error {
|
|||
|
||||
// write the file contents over to target at the same location
|
||||
targetPath = strings.TrimSuffix(targetPath, filepath.Ext(targetPath)) + templ.Ext()
|
||||
fmt.Println("writing ", targetPath)
|
||||
fmt.Println("writing", targetPath)
|
||||
return os.WriteFile(targetPath, []byte(content), FILE_RW_MODE)
|
||||
} else {
|
||||
// if a non template was found, copy file as is
|
||||
fmt.Println("writing ", targetPath)
|
||||
fmt.Println("writing", targetPath)
|
||||
return copyFile(path, targetPath)
|
||||
}
|
||||
}
|
||||
|
|
32
site/site.go
32
site/site.go
|
@ -21,32 +21,27 @@ type Site struct {
|
|||
TemplateIndex map[string]*templates.Template
|
||||
}
|
||||
|
||||
func Load(rootDir string) (*Site, error) {
|
||||
func Load(srcDir string, layoutsDir string) (*Site, error) {
|
||||
// TODO load config from config.yml
|
||||
site := Site{
|
||||
layouts: make(map[string]templates.Template),
|
||||
TemplateIndex: make(map[string]*templates.Template),
|
||||
config: make(map[string]string),
|
||||
}
|
||||
|
||||
// TODO merge with contents of local config.yml file
|
||||
site.config = map[string]string{
|
||||
"src_dir": filepath.Join(rootDir, "src"),
|
||||
"target_dir": filepath.Join(rootDir, "target"),
|
||||
"layouts_dir": filepath.Join(rootDir, "layouts"),
|
||||
}
|
||||
|
||||
if err := site.loadLayouts(); err != nil {
|
||||
if err := site.loadLayouts(layoutsDir); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if err := site.loadTemplates(); err != nil {
|
||||
if err := site.loadTemplates(srcDir); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &site, nil
|
||||
}
|
||||
|
||||
func (site *Site) loadLayouts() error {
|
||||
files, err := os.ReadDir(site.config["layouts_dir"])
|
||||
func (site *Site) loadLayouts(layoutsDir string) error {
|
||||
files, err := os.ReadDir(layoutsDir)
|
||||
|
||||
if os.IsNotExist(err) {
|
||||
return nil
|
||||
|
@ -57,7 +52,7 @@ func (site *Site) loadLayouts() error {
|
|||
for _, entry := range files {
|
||||
if !entry.IsDir() {
|
||||
filename := entry.Name()
|
||||
path := filepath.Join(site.config["layouts_dir"], filename)
|
||||
path := filepath.Join(layoutsDir, filename)
|
||||
templ, err := templates.Parse(path)
|
||||
if err != nil {
|
||||
return err
|
||||
|
@ -71,8 +66,15 @@ func (site *Site) loadLayouts() error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func (site *Site) loadTemplates() error {
|
||||
return filepath.WalkDir(site.config["src_dir"], func(path string, entry fs.DirEntry, err error) error {
|
||||
func (site *Site) loadTemplates(srcDir string) error {
|
||||
_, err := os.ReadDir(srcDir)
|
||||
if os.IsNotExist(err) {
|
||||
return fmt.Errorf("missing %s directory", srcDir)
|
||||
} else if err != nil {
|
||||
return fmt.Errorf("couldn't read %s", srcDir)
|
||||
}
|
||||
|
||||
return filepath.WalkDir(srcDir, func(path string, entry fs.DirEntry, err error) error {
|
||||
if !entry.IsDir() {
|
||||
templ, err := templates.Parse(path)
|
||||
// if sometime fails or this is not a template, skip
|
||||
|
|
Loading…
Reference in a new issue