go-cv/template.go

55 lines
775 B
Go
Raw Permalink Normal View History

2021-02-24 02:10:44 +00:00
package cv
import (
"html/template"
"log"
"net/url"
"strings"
)
func (g *generator) setupTemplate() error {
tpl := template.New("cv.gohtml")
tpl.Funcs(template.FuncMap{
"prettyUrl": prettyUrl,
"notEmpty": notEmpty,
})
tpl, err := tpl.ParseFiles("templates/cv.gohtml")
if err != nil {
return err
}
g.tpl = tpl
return nil
}
func notEmpty(v interface{}) bool {
switch t := v.(type) {
case string:
return len(t) > 0
case []string:
return len(t) > 0
}
return true
}
func prettyUrl(u string) string {
v, err := url.Parse(u)
if err != nil {
log.Println("Error:", err)
return u
}
if strings.HasPrefix(v.Host, "www.") {
v.Host = strings.TrimPrefix(v.Host, "www.")
}
v.Path = strings.TrimRight(v.Path, "/")
return v.Host + v.Path
}