diff --git a/web/web.go b/web/web.go index c47d195..811512c 100644 --- a/web/web.go +++ b/web/web.go @@ -4,6 +4,9 @@ import ( "embed" "io/fs" "net/http" + + "git.arav.top/Arav/dwelling-home/pkg/article" + "github.com/gomarkdown/markdown" ) // To install a Jade compiler: go install github.com/Joker/jade/cmd/jade@latest @@ -11,11 +14,8 @@ import ( //go:generate $GOPATH/bin/jade -basedir ./templates -pkg=web -stdbuf -stdlib -writer index.pug stuff.pug mindflow.pug about.pug // guestbook.pug //go:generate $GOPATH/bin/jade -basedir ./templates -pkg=web -stdbuf -stdlib -writer rss.pug -//go:generate $GOPATH/bin/jade -basedir ./templates -pkg=web -stdbuf -stdlib -writer articles/hardening_mikrotik.pug -//go:generate $GOPATH/bin/jade -basedir ./templates -pkg=web -stdbuf -stdlib -writer articles/nginx_recipes_and_tips.pug -//go:generate $GOPATH/bin/jade -basedir ./templates -pkg=web -stdbuf -stdlib -writer articles/rpi_root_on_external_drive.pug -//go:generate $GOPATH/bin/jade -basedir ./templates -pkg=web -stdbuf -stdlib -writer articles/setting_up_a_mail_server.pug -//go:generate $GOPATH/bin/jade -basedir ./templates -pkg=web -stdbuf -stdlib -writer articles/setting_up_a_tor_proxy_relay_hiddenserv.pug + +//go:generate $GOPATH/bin/jade -basedir ./templates -pkg=web -stdbuf -stdlib -writer article.pug //go:embed assets var assetsDir embed.FS @@ -28,3 +28,32 @@ func Assets() http.FileSystem { 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 + } +}