all repos — vite @ 4c0dbd41900270f85e454f3e26c7e86fd4a7a663

a fast (this time, actually) and minimal static site generator

Support parsing templates recursively

Templates can now be specified as a relative path under 'templates'. For
example, a template at 'templates/foo/bar.html' can simply be referenced
as 'foo/bar.html' for loading.
Anirudh Oppiliappan x@icyphox.sh
Tue, 22 Feb 2022 10:44:39 +0530
commit

4c0dbd41900270f85e454f3e26c7e86fd4a7a663

parent

fbcc6c93a3aa3282d34c7c90a223c9535e8d2b94

3 files changed, 92 insertions(+), 15 deletions(-)

jump to
M markdown/markdown.gomarkdown/markdown.go

@@ -2,9 +2,10 @@ package markdown

import ( "os" - "path/filepath" - "text/template" + gotmpl "text/template" "time" + + "git.icyphox.sh/vite/markdown/template" bfc "github.com/Depado/bfchroma" bf "github.com/russross/blackfriday/v2"

@@ -51,13 +52,14 @@ if metaTemplate == "" {

metaTemplate = "text.html" } - t, err := template.New("").Funcs(template.FuncMap{ + tmpl := template.NewTmpl() + tmpl.SetFuncs(gotmpl.FuncMap{ "parsedate": func(s string) time.Time { date, _ := time.Parse("2006-01-02", s) return date }, - }).ParseGlob(filepath.Join(tmplDir, "*.html")) - if err != nil { + }) + if err := tmpl.Load(tmplDir); err != nil { return err }

@@ -66,7 +68,7 @@ if err != nil {

return err } - if err = t.ExecuteTemplate(w, metaTemplate, data); err != nil { + if err = tmpl.ExecuteTemplate(w, metaTemplate, data); err != nil { return err } return nil
A markdown/template/template.go

@@ -0,0 +1,59 @@

+package template + +import ( + "os" + "path/filepath" + "strings" + "text/template" +) + +type Tmpl struct { + *template.Template +} + +func NewTmpl() *Tmpl { + tmpl := Tmpl{} + tmpl.Template = template.New("") + return &tmpl +} + +func (t *Tmpl) SetFuncs(funcMap template.FuncMap) { + t.Template = t.Template.Funcs(funcMap) +} + +func (t *Tmpl) Load(dir string) (err error) { + if dir, err = filepath.Abs(dir); err != nil { + return err + } + + root := t.Template + + if err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) (_ error) { + if err != nil { + return err + } + + if filepath.Ext(path) != ".html" { + return + } + + var rel string + if rel, err = filepath.Rel(dir, path); err != nil { + return err + } + + rel = strings.Join(strings.Split(rel, string(os.PathSeparator)), "/") + newTmpl := root.New(rel) + + var b []byte + if b, err = os.ReadFile(path); err != nil { + return err + } + + _, err = newTmpl.Parse(string(b)) + return err + }); err != nil { + return err + } + return nil +}
M readmereadme

@@ -5,7 +5,7 @@ A fast (this time, actually) and minimal static site generator.

INSTALLING - go get git.icyphox.sh/vite + go install git.icyphox.sh/vite@latest USAGE

@@ -41,15 +41,31 @@

TEMPLATING Non-index templates have access to the below objects: -· Cfg: object of ConfigYaml -· Meta: map[string]string of the page's frontmatter metadata -· Body: Contains the HTML +• Cfg: object of ConfigYaml +• Meta: map[string]string of the page's frontmatter metadata +• Body: Contains the HTML Index templates have access to everything above, and a Posts object, which is a slice containing HTML and Meta. This is useful for iterating through to generate an index page. Example: https://git.icyphox.sh/site/tree/templates/index.html +Templates are written as standard Go templates (ref: +https://godocs.io/text/template), and can be loaded recursively. +Consider the below template structure: + + templates/ + |-- blog.html + |-- index.html + |-- project/ + |-- index.html + `-- project.html + +The templates under 'project/' are referenced as 'project/index.html'. +This deserves mention because Go templates don't recurse into +subdirectories by default (template.ParseGlob uses filepath.Glob, and +doesn't support deep-matching, i.e. '**'). + More templating examples can be found at: https://git.icyphox.sh/site/tree/templates

@@ -63,11 +79,11 @@

FILE TREE . - ├── build/ - ├── config.yaml - ├── pages/ - ├── static/ - └── templates/ + |-- build/ + |-- config.yaml + |-- pages/ + |-- static/ + |-- templates/ The entire 'static/' directory gets copied over to 'build/', and can be used to reference static assets -- css, images, etc. 'pages/' supports