mirror of
https://github.com/facundoolano/jorge.git
synced 2024-12-25 21:58:28 +01:00
Expose list of static_files as template variables (#48)
* add slug to template metadata * add static files variable * fix (hopefully) index.html case in relpath * fix test
This commit is contained in:
parent
ab3bc633d2
commit
c252507af9
1 changed files with 40 additions and 22 deletions
38
site/site.go
38
site/site.go
|
@ -27,6 +27,7 @@ type site struct {
|
|||
layouts map[string]markup.Template
|
||||
posts []map[string]interface{}
|
||||
pages []map[string]interface{}
|
||||
static_files []map[string]interface{}
|
||||
tags map[string][]map[string]interface{}
|
||||
data map[string]interface{}
|
||||
|
||||
|
@ -143,19 +144,37 @@ func (site *site) loadTemplates() error {
|
|||
err := filepath.WalkDir(site.config.SrcDir, func(path string, entry fs.DirEntry, err error) error {
|
||||
if !entry.IsDir() {
|
||||
templ, err := markup.Parse(site.templateEngine, path)
|
||||
// if something fails or this is not a template, skip
|
||||
if err != nil || templ == nil {
|
||||
// if something fails skip
|
||||
if err != nil {
|
||||
return checkFileError(err)
|
||||
}
|
||||
|
||||
// set site related (?) metadata. Not sure if this should go elsewhere
|
||||
relPath, _ := filepath.Rel(site.config.SrcDir, path)
|
||||
baseName := strings.TrimSuffix(filepath.Base(relPath), filepath.Ext(relPath))
|
||||
|
||||
// if it's a static file, treat separately
|
||||
if templ == nil {
|
||||
// using the same variable names as jekyll
|
||||
metadata := map[string]interface{}{
|
||||
"path": relPath,
|
||||
"name": filepath.Base(relPath),
|
||||
"basename": baseName,
|
||||
"extname": filepath.Ext(relPath),
|
||||
}
|
||||
site.static_files = append(site.static_files, metadata)
|
||||
return nil
|
||||
}
|
||||
|
||||
srcPath, _ := filepath.Rel(site.config.RootDir, path)
|
||||
relPath = strings.TrimSuffix(relPath, filepath.Ext(relPath)) + templ.TargetExt()
|
||||
targetPath := strings.TrimSuffix(relPath, filepath.Ext(relPath)) + templ.TargetExt()
|
||||
if templ.TargetExt() == ".html" && baseName != "index" {
|
||||
targetPath = filepath.Join(strings.TrimSuffix(relPath, filepath.Ext(relPath)), "index.html")
|
||||
}
|
||||
templ.Metadata["src_path"] = srcPath
|
||||
templ.Metadata["path"] = relPath
|
||||
templ.Metadata["url"] = "/" + strings.TrimSuffix(strings.TrimSuffix(relPath, "index.html"), ".html")
|
||||
templ.Metadata["path"] = targetPath
|
||||
templ.Metadata["url"] = "/" + strings.TrimSuffix(strings.TrimSuffix(targetPath, "/index.html"), ".html")
|
||||
templ.Metadata["dir"] = "/" + filepath.Dir(relPath)
|
||||
templ.Metadata["slug"] = filepath.Base(templ.Metadata["url"].(string))
|
||||
|
||||
// if drafts are disabled, exclude from posts, page and tags indexes, but not from site.templates
|
||||
// we want to explicitly exclude the template from the target, rather than treating it as a non template file
|
||||
|
@ -175,14 +194,11 @@ func (site *site) loadTemplates() error {
|
|||
}
|
||||
}
|
||||
|
||||
} else {
|
||||
} else if baseName != "index" {
|
||||
// the index pages should be skipped from the page directory
|
||||
filename := strings.TrimSuffix(entry.Name(), filepath.Ext(entry.Name()))
|
||||
if filename != "index" {
|
||||
site.pages = append(site.pages, templ.Metadata)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
site.templates[path] = templ
|
||||
}
|
||||
|
@ -203,6 +219,7 @@ func (site *site) loadTemplates() error {
|
|||
}
|
||||
return strings.Compare(a["path"].(string), b["path"].(string))
|
||||
}
|
||||
slices.SortFunc(site.static_files, CompareTemplates)
|
||||
slices.SortFunc(site.posts, CompareTemplates)
|
||||
slices.SortFunc(site.pages, CompareTemplates)
|
||||
for _, posts := range site.tags {
|
||||
|
@ -363,6 +380,7 @@ func (site *site) render(templ *markup.Template) ([]byte, error) {
|
|||
"posts": site.posts,
|
||||
"tags": site.tags,
|
||||
"pages": site.pages,
|
||||
"static_files": site.static_files,
|
||||
"data": site.data,
|
||||
},
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue