85 lines
1.8 KiB
Go
85 lines
1.8 KiB
Go
package web
|
|
|
|
import (
|
|
"embed"
|
|
"log"
|
|
"sort"
|
|
"strings"
|
|
"time"
|
|
|
|
"git.arav.su/Arav/dwelling-home/pkg/article"
|
|
"github.com/gomarkdown/markdown"
|
|
)
|
|
|
|
const (
|
|
articleFileExtension = ".md"
|
|
articleMetaExtension = ".meta"
|
|
)
|
|
|
|
//go:embed articles
|
|
var articlesFS embed.FS
|
|
var articles map[string]*article.Article = make(map[string]*article.Article)
|
|
|
|
// ArticleMetadata holds date, title and URL of an article used in articles list
|
|
// on the Stuff page.
|
|
type ArticleMetadata struct {
|
|
ID int64
|
|
Date time.Time
|
|
Title string
|
|
URL string
|
|
}
|
|
|
|
var Metadata []ArticleMetadata
|
|
|
|
func GetArticle(name string) *article.Article {
|
|
if artcl, ok := articles[name]; ok {
|
|
return artcl
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func init() {
|
|
|
|
//// Load articles
|
|
|
|
entries, _ := articlesFS.ReadDir("articles")
|
|
|
|
for _, entry := range entries {
|
|
if strings.HasSuffix(entry.Name(), articleFileExtension) {
|
|
name := strings.TrimSuffix(entry.Name(), articleFileExtension)
|
|
|
|
meta, err := articlesFS.ReadFile("articles/" + name + articleMetaExtension)
|
|
if err != nil {
|
|
log.Fatalln("an article \"", name, "\" cannot be read:", err)
|
|
}
|
|
|
|
md, err := articlesFS.ReadFile("articles/" + entry.Name())
|
|
if err != nil {
|
|
log.Fatalln("a metadata for an article \"", name, "\" cannot be read:", err)
|
|
}
|
|
|
|
articles[name], err = article.New(markdown.ToHTML(md, nil, nil), meta)
|
|
if err != nil {
|
|
log.Fatalln("an article \"", name, "\" cannot be parsed:", err)
|
|
}
|
|
}
|
|
}
|
|
|
|
//// Fill and sort metadata slice
|
|
|
|
for urlid, article := range articles {
|
|
Metadata = append(Metadata,
|
|
ArticleMetadata{
|
|
ID: article.ID,
|
|
Date: article.Date,
|
|
Title: article.Title,
|
|
URL: "stuff/article/" + urlid})
|
|
}
|
|
|
|
sort.Slice(Metadata, func(i, j int) bool {
|
|
return Metadata[i].ID > Metadata[j].ID
|
|
})
|
|
|
|
articlesFS = embed.FS{}
|
|
}
|