diff --git a/go.mod b/go.mod index 9fac591..8a7bb82 100644 --- a/go.mod +++ b/go.mod @@ -3,11 +3,11 @@ module git.phc.dm.unipi.it/phc/website go 1.13 require ( - github.com/alecthomas/chroma v0.9.4 // indirect + github.com/alecthomas/chroma v0.9.4 github.com/go-chi/chi/v5 v5.0.7 github.com/joho/godotenv v1.4.0 github.com/litao91/goldmark-mathjax v0.0.0-20210217064022-a43cf739a50f github.com/yuin/goldmark v1.4.4 - github.com/yuin/goldmark-highlighting v0.0.0-20210516132338-9216f9c5aa01 // indirect + github.com/yuin/goldmark-highlighting v0.0.0-20210516132338-9216f9c5aa01 gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b ) diff --git a/go.sum b/go.sum index bea734a..50a8bc9 100644 --- a/go.sum +++ b/go.sum @@ -1,18 +1,15 @@ github.com/alecthomas/assert v0.0.0-20170929043011-405dbfeb8e38/go.mod h1:r7bzyVFMNntcxPZXK3/+KdruV1H5KSlyVY0gc+NgInI= -github.com/alecthomas/chroma v0.7.2-0.20200305040604-4f3623dce67a h1:3v1NrYWWqp2S72e4HLgxKt83B3l0lnORDholH/ihoMM= github.com/alecthomas/chroma v0.7.2-0.20200305040604-4f3623dce67a/go.mod h1:fv5SzZPFJbwp2NXJWpFIX7DZS4HgV1K4ew4Pc2OZD9s= github.com/alecthomas/chroma v0.9.4 h1:YL7sOAE3p8HS96T9km7RgvmsZIctqbK1qJ0b7hzed44= github.com/alecthomas/chroma v0.9.4/go.mod h1:jtJATyUxlIORhUOFNA9NZDWGAQ8wpxQQqNSB4rjA/1s= github.com/alecthomas/colour v0.0.0-20160524082231-60882d9e2721/go.mod h1:QO9JBoKquHd+jz9nshCh40fOfO+JzsoXy8qTHF68zU0= github.com/alecthomas/kong v0.2.1-0.20190708041108-0548c6b1afae/go.mod h1:+inYUSluD+p4L8KdviBSgzcqEjUQOfC5fQDRFuc36lI= github.com/alecthomas/repr v0.0.0-20180818092828-117648cd9897/go.mod h1:xTS7Pm1pD1mvyM075QCDSRqH6qRLXylzS24ZTpRiSzQ= -github.com/danwakefield/fnmatch v0.0.0-20160403171240-cbb64ac3d964 h1:y5HC9v93H5EPKqaS1UYVg1uYah5Xf51mBfIoWehClUQ= github.com/danwakefield/fnmatch v0.0.0-20160403171240-cbb64ac3d964/go.mod h1:Xd9hchkHSWYkEqJwUGisez3G1QY8Ryz0sdWrLPMGjLk= -github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/dlclark/regexp2 v1.1.6/go.mod h1:2pZnwuY/m+8K6iRw6wQdMtk+rH5tNGR1i55kozfMjCc= -github.com/dlclark/regexp2 v1.2.0 h1:8sAhBGEM0dRWogWqWyQeIJnxjWO6oIjl8FKqREDsGfk= github.com/dlclark/regexp2 v1.2.0/go.mod h1:2pZnwuY/m+8K6iRw6wQdMtk+rH5tNGR1i55kozfMjCc= github.com/dlclark/regexp2 v1.4.0 h1:F1rxgk7p4uKjwIQxBs9oAXe5CqrXlCduYEJvrF4u93E= github.com/dlclark/regexp2 v1.4.0/go.mod h1:2pZnwuY/m+8K6iRw6wQdMtk+rH5tNGR1i55kozfMjCc= @@ -32,7 +29,6 @@ github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAm github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= -github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0= github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= diff --git a/main.go b/main.go index e3abc1a..fa1685f 100644 --- a/main.go +++ b/main.go @@ -29,9 +29,10 @@ func main() { // Templates & Renderer renderer := templates.NewRenderer( "./views/", - templates.File("./views/base.html"), - templates.Pattern("./views/partials/*.html"), + "./views/base.html", + "./views/partials/*.html", ) + newsArticlesRegistry := articles.NewRegistry("./news") // Routes diff --git a/templates/templates.go b/templates/templates.go index 9a22d88..e0d5738 100644 --- a/templates/templates.go +++ b/templates/templates.go @@ -10,88 +10,72 @@ import ( "git.phc.dm.unipi.it/phc/website/util" ) -type LoadTemplate interface { - Load(t *template.Template) (*template.Template, error) -} - -type File string - -func (file File) Load(t *template.Template) (*template.Template, error) { - return t.ParseFiles(string(file)) -} - -type Pattern string - -func (pattern Pattern) Load(t *template.Template) (*template.Template, error) { - return t.ParseGlob(string(pattern)) -} - type CachedTemplate struct { - Loaders []LoadTemplate - template *template.Template + LoadPatterns []string + cachedTmpl *template.Template } -func NewCacheTemplate(loaders ...LoadTemplate) *CachedTemplate { +func NewCacheTemplate(loadPatterns ...string) *CachedTemplate { cachedTemplate := &CachedTemplate{ - Loaders: loaders, - template: nil, + LoadPatterns: loadPatterns, + cachedTmpl: nil, } template.Must(cachedTemplate.Load(template.New(""))) return cachedTemplate } func (ct *CachedTemplate) Load(t *template.Template) (*template.Template, error) { - if ct.template != nil { - return ct.template, nil + if ct.cachedTmpl != nil { + return ct.cachedTmpl, nil } - ct.template = t + ct.cachedTmpl = t - for _, loader := range ct.Loaders { + for _, pattern := range ct.LoadPatterns { var err error - ct.template, err = loader.Load(ct.template) + ct.cachedTmpl, err = ct.cachedTmpl.ParseGlob(pattern) if err != nil { return nil, err } } - return ct.template, nil + return ct.cachedTmpl, nil } func (ct *CachedTemplate) Reload() { - ct.template = nil + ct.cachedTmpl = nil template.Must(ct.Load(nil)) } func (ct *CachedTemplate) Template() *template.Template { - return ct.template + return ct.cachedTmpl } // Renderer holds cached templates for rendering type Renderer struct { - rootPath string - baseLoaders []LoadTemplate - routes map[string]*CachedTemplate + rootPath string + loadPatterns []string + templateCache map[string]*CachedTemplate } // NewRenderer constructs a template renderer with a base file -func NewRenderer(rootPath string, loaders ...LoadTemplate) *Renderer { +func NewRenderer(rootPath string, loadPatterns ...string) *Renderer { return &Renderer{ rootPath, - loaders, + loadPatterns, map[string]*CachedTemplate{}, } } func (r *Renderer) Load(name string) *CachedTemplate { - cachedTemplate, present := r.routes[name] + cachedTemplate, present := r.templateCache[name] if !present { - loaders := []LoadTemplate{} - loaders = append(loaders, r.baseLoaders...) - loaders = append(loaders, File(path.Join(r.rootPath, name))) + loaders := []string{} + loaders = append(loaders, r.loadPatterns...) + loaders = append(loaders, path.Join(r.rootPath, name)) cachedTemplate = NewCacheTemplate(loaders...) - r.routes[name] = cachedTemplate + r.templateCache[name] = cachedTemplate } return cachedTemplate diff --git a/views/home.html b/views/home.html index 55b413a..001271e 100644 --- a/views/home.html +++ b/views/home.html @@ -28,7 +28,7 @@
yyyy-mm-dd
- Lorem ipsum dolor sit amet consectetur adipisicing elit. Quis nemo aperiam, voluptas quam alias esse sed natus tempore suscipit fugiat sit delectus exercitationem numquam ipsum assumenda recusandae consequatur... + much doge, ipsum dolor sit amet consectetur adipisicing elit. Quis nemo aperiam, voluptas quam alias esse sed natus tempore suscipit fugiat sit delectus exercitationem numquam ipsum assumenda recusandae consequatur...