fork of anirudh.fi/vite that uses chroma for hl
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

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.

+92 -15
+8 -6
markdown/markdown.go
··· 2 2 3 3 import ( 4 4 "os" 5 - "path/filepath" 6 - "text/template" 5 + gotmpl "text/template" 7 6 "time" 7 + 8 + "git.icyphox.sh/vite/markdown/template" 8 9 9 10 bfc "github.com/Depado/bfchroma" 10 11 bf "github.com/russross/blackfriday/v2" ··· 51 52 metaTemplate = "text.html" 52 53 } 53 54 54 - t, err := template.New("").Funcs(template.FuncMap{ 55 + tmpl := template.NewTmpl() 56 + tmpl.SetFuncs(gotmpl.FuncMap{ 55 57 "parsedate": func(s string) time.Time { 56 58 date, _ := time.Parse("2006-01-02", s) 57 59 return date 58 60 }, 59 - }).ParseGlob(filepath.Join(tmplDir, "*.html")) 60 - if err != nil { 61 + }) 62 + if err := tmpl.Load(tmplDir); err != nil { 61 63 return err 62 64 } 63 65 ··· 66 68 return err 67 69 } 68 70 69 - if err = t.ExecuteTemplate(w, metaTemplate, data); err != nil { 71 + if err = tmpl.ExecuteTemplate(w, metaTemplate, data); err != nil { 70 72 return err 71 73 } 72 74 return nil
+59
markdown/template/template.go
··· 1 + package template 2 + 3 + import ( 4 + "os" 5 + "path/filepath" 6 + "strings" 7 + "text/template" 8 + ) 9 + 10 + type Tmpl struct { 11 + *template.Template 12 + } 13 + 14 + func NewTmpl() *Tmpl { 15 + tmpl := Tmpl{} 16 + tmpl.Template = template.New("") 17 + return &tmpl 18 + } 19 + 20 + func (t *Tmpl) SetFuncs(funcMap template.FuncMap) { 21 + t.Template = t.Template.Funcs(funcMap) 22 + } 23 + 24 + func (t *Tmpl) Load(dir string) (err error) { 25 + if dir, err = filepath.Abs(dir); err != nil { 26 + return err 27 + } 28 + 29 + root := t.Template 30 + 31 + if err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) (_ error) { 32 + if err != nil { 33 + return err 34 + } 35 + 36 + if filepath.Ext(path) != ".html" { 37 + return 38 + } 39 + 40 + var rel string 41 + if rel, err = filepath.Rel(dir, path); err != nil { 42 + return err 43 + } 44 + 45 + rel = strings.Join(strings.Split(rel, string(os.PathSeparator)), "/") 46 + newTmpl := root.New(rel) 47 + 48 + var b []byte 49 + if b, err = os.ReadFile(path); err != nil { 50 + return err 51 + } 52 + 53 + _, err = newTmpl.Parse(string(b)) 54 + return err 55 + }); err != nil { 56 + return err 57 + } 58 + return nil 59 + }
+25 -9
readme
··· 5 5 6 6 INSTALLING 7 7 8 - go get git.icyphox.sh/vite 8 + go install git.icyphox.sh/vite@latest 9 9 10 10 11 11 USAGE ··· 41 41 TEMPLATING 42 42 43 43 Non-index templates have access to the below objects: 44 - · Cfg: object of ConfigYaml 45 - · Meta: map[string]string of the page's frontmatter metadata 46 - · Body: Contains the HTML 44 + • Cfg: object of ConfigYaml 45 + • Meta: map[string]string of the page's frontmatter metadata 46 + • Body: Contains the HTML 47 47 48 48 Index templates have access to everything above, and a Posts object, 49 49 which is a slice containing HTML and Meta. This is useful for iterating 50 50 through to generate an index page. 51 51 Example: https://git.icyphox.sh/site/tree/templates/index.html 52 52 53 + Templates are written as standard Go templates (ref: 54 + https://godocs.io/text/template), and can be loaded recursively. 55 + Consider the below template structure: 56 + 57 + templates/ 58 + |-- blog.html 59 + |-- index.html 60 + |-- project/ 61 + |-- index.html 62 + `-- project.html 63 + 64 + The templates under 'project/' are referenced as 'project/index.html'. 65 + This deserves mention because Go templates don't recurse into 66 + subdirectories by default (template.ParseGlob uses filepath.Glob, and 67 + doesn't support deep-matching, i.e. '**'). 68 + 53 69 More templating examples can be found at: 54 70 https://git.icyphox.sh/site/tree/templates 55 71 ··· 63 79 FILE TREE 64 80 65 81 . 66 - ├── build/ 67 - ├── config.yaml 68 - ├── pages/ 69 - ├── static/ 70 - └── templates/ 82 + |-- build/ 83 + |-- config.yaml 84 + |-- pages/ 85 + |-- static/ 86 + |-- templates/ 71 87 72 88 The entire 'static/' directory gets copied over to 'build/', and can be 73 89 used to reference static assets -- css, images, etc. 'pages/' supports