feat: range over files for paths
All checks were successful
/ deploy (push) Successful in 46s

This commit is contained in:
ange 2024-12-14 09:19:13 +00:00
parent e9fc659a5a
commit 186491250e
Signed by: ange
GPG Key ID: 9E0C4157BB7BEB1D
3 changed files with 13 additions and 15 deletions

View File

@ -9,21 +9,23 @@ import (
var TMPL map[string][]byte var TMPL map[string][]byte
// TODO not use a global
// TODO use range instead of if else in header.html
func generateTmpl() { func generateTmpl() {
files, _ := filepath.Glob("html/*.html") files, _ := filepath.Glob("html/*.html")
re := regexp.MustCompile("html(/.+).html") re := regexp.MustCompile("html/(.+).html")
names := make([]string, len(files))
for i, f := range files {
names[i] = re.FindStringSubmatch(f)[1]
}
TMPL = make(map[string][]byte, len(files)) TMPL = make(map[string][]byte, len(files))
for _, f := range files { for i, f := range files {
b := new(bytes.Buffer) b := new(bytes.Buffer)
name := re.FindStringSubmatch(f)[1]
t, _ := template.ParseFiles(f) t, _ := template.ParseFiles(f)
t.ParseGlob("tmpl/*.tmpl") t.ParseGlob("tmpl/*.tmpl")
t.Execute(b, map[string]any{ t.Execute(b, map[string]any{
"name": name, "name": names[i],
"names": names,
}) })
TMPL[name] = b.Bytes() TMPL["/" + names[i]] = b.Bytes()
} }
} }

View File

@ -5,8 +5,7 @@ import (
) )
func index(w http.ResponseWriter, r *http.Request) { func index(w http.ResponseWriter, r *http.Request) {
r.URL.Path = "/contact" http.Redirect(w, r, "/contact", http.StatusMovedPermanently)
html(w, r)
} }
func style(w http.ResponseWriter, r *http.Request) { func style(w http.ResponseWriter, r *http.Request) {

View File

@ -4,12 +4,9 @@
<span class="right" style="font-size: .4em;">Website heavily inspired from <a href="//suckless.org/" target="_blank" rel="noreferrer noopener">suckless.org</a></span> <span class="right" style="font-size: .4em;">Website heavily inspired from <a href="//suckless.org/" target="_blank" rel="noreferrer noopener">suckless.org</a></span>
</div> </div>
<div id="menu"> <div id="menu">
{{if eq .name "/contact"}} {{$name := .name}}
<a href="/"><b>contact</b></a> {{range .names}}
<a href="/resume">resume</a> <a href="/{{.}}">{{if eq $name .}}<b>{{.}}</b>{{else}}{{.}}{{end}}</a>
{{else if eq .name "/resume"}}
<a href="/">contact</a>
<a href="/resume"><b>resume</b></a>
{{end}} {{end}}
<span class="right"> <span class="right">
<a href="//git.gmoker.com/yw5n" target="_blank" rel="noreferrer noopener">source</a> <a href="//git.gmoker.com/yw5n" target="_blank" rel="noreferrer noopener">source</a>