1
0
dwelling-home/web/web.go

62 lines
1.5 KiB
Go

package web
import (
"embed"
"io/fs"
"net/http"
"git.arav.su/Arav/dwelling-home/pkg/article"
"github.com/gomarkdown/markdown"
)
// To install a Jade compiler: go install github.com/Joker/jade/cmd/jade@latest
//
//go:generate $GOPATH/bin/jade -basedir ./templates -pkg=web -writer index.pug stuff.pug mindflow.pug
//go:generate $GOPATH/bin/jade -basedir ./templates -pkg=web -writer about.pug guestbook.pug rss.pug
//go:generate $GOPATH/bin/jade -basedir ./templates -pkg=web -writer guestbook_admin.pug mindflow_admin.pug
//go:generate $GOPATH/bin/jade -basedir ./templates -pkg=web -writer article.pug
//go:generate $GOPATH/bin/jade -basedir ./templates -pkg=web -writer error403.pug error404.pug error500.pug
//go:embed assets
var assetsDir embed.FS
func Assets() http.FileSystem {
f, _ := fs.Sub(assetsDir, "assets")
return http.FS(f)
}
func AssetsGetFile(path string) ([]byte, error) {
return assetsDir.ReadFile("assets/" + path)
}
//go:embed articles
var articlesFS embed.FS
var articles map[string]article.Article = make(map[string]article.Article)
func GetArticle(name string) (*article.Article, error) {
if artcl, ok := articles[name]; ok {
return &artcl, nil
} else {
meta, err := articlesFS.ReadFile("articles/" + name + ".meta")
if err != nil {
return nil, err
}
md, err := articlesFS.ReadFile("articles/" + name + ".md")
if err != nil {
return nil, err
}
artcl := article.Article{
Body: markdown.ToHTML(md, nil, nil)}
artcl.ParseMetadata(meta)
articles[name] = artcl
return &artcl, nil
}
}