2023-05-23 04:08:58 +04:00
|
|
|
package web
|
|
|
|
|
|
|
|
import (
|
|
|
|
"embed"
|
2023-09-24 20:00:17 +04:00
|
|
|
"log"
|
2023-05-23 04:08:58 +04:00
|
|
|
"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
|
2023-09-24 19:05:22 +04:00
|
|
|
var articles map[string]*article.Article = make(map[string]*article.Article)
|
2023-05-23 04:08:58 +04:00
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2023-09-24 20:43:36 +04:00
|
|
|
var Metadata []ArticleMetadata
|
2023-05-23 04:08:58 +04:00
|
|
|
|
2023-09-24 20:00:17 +04:00
|
|
|
func GetArticle(name string) *article.Article {
|
2023-05-23 04:08:58 +04:00
|
|
|
if artcl, ok := articles[name]; ok {
|
2023-09-24 20:00:17 +04:00
|
|
|
return artcl
|
2023-09-23 04:23:33 +04:00
|
|
|
}
|
2023-09-24 20:00:17 +04:00
|
|
|
return nil
|
2023-05-23 04:08:58 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
2023-09-24 20:43:36 +04:00
|
|
|
|
|
|
|
//// Load articles
|
|
|
|
|
2023-05-23 04:08:58 +04:00
|
|
|
entries, _ := articlesFS.ReadDir("articles")
|
|
|
|
|
|
|
|
for _, entry := range entries {
|
|
|
|
if strings.HasSuffix(entry.Name(), articleFileExtension) {
|
2023-09-24 20:00:17 +04:00
|
|
|
name := strings.TrimSuffix(entry.Name(), articleFileExtension)
|
|
|
|
|
2023-09-24 20:43:36 +04:00
|
|
|
meta, err := articlesFS.ReadFile("articles/" + name + articleMetaExtension)
|
2023-09-24 20:00:17 +04:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalln("an article \"", name, "\" cannot be read:", err)
|
|
|
|
}
|
|
|
|
|
2023-09-24 20:43:36 +04:00
|
|
|
md, err := articlesFS.ReadFile("articles/" + entry.Name())
|
2023-09-24 20:00:17 +04:00
|
|
|
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)
|
|
|
|
}
|
2023-05-23 04:08:58 +04:00
|
|
|
}
|
|
|
|
}
|
2023-09-24 20:43:36 +04:00
|
|
|
|
|
|
|
//// 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{}
|
2023-05-23 04:08:58 +04:00
|
|
|
}
|