Adding the wiki route and better styling
This commit is contained in:
@ -1,18 +1,19 @@
|
||||
package routes
|
||||
|
||||
import (
|
||||
"git.matterlinux.xyz/Matterlinux/website/lib"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/gofiber/fiber/v2/log"
|
||||
)
|
||||
|
||||
func IndexRoute(c *fiber.Ctx) error{
|
||||
content, err := GetContent("content", "index")
|
||||
con, err := lib.GetContent("", "index")
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
return RenderError(c, 500)
|
||||
log.Error("GetContent -> ", err)
|
||||
return lib.RenderError(c, 500)
|
||||
}
|
||||
|
||||
return c.Render("index", fiber.Map{
|
||||
"readme": content,
|
||||
"readme": con,
|
||||
})
|
||||
}
|
||||
|
@ -1,102 +1,69 @@
|
||||
package routes
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"os"
|
||||
"path"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"git.matterlinux.xyz/Matterlinux/website/lib"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/gofiber/fiber/v2/log"
|
||||
)
|
||||
|
||||
var news_path = path.Join("content", "news")
|
||||
|
||||
func NewsRoute(c *fiber.Ctx) error {
|
||||
var res []Content
|
||||
entries, err := os.ReadDir(news_path)
|
||||
contents, err := lib.ListContent("news")
|
||||
if err != nil {
|
||||
log.Errorf("Cannot news dir: %s", err)
|
||||
return RenderError(c, 500)
|
||||
log.Errorf("ListContent -> %s", err.Error())
|
||||
return lib.RenderError(c, 500)
|
||||
}
|
||||
|
||||
for _, e := range entries {
|
||||
if(!strings.HasSuffix(e.Name(), ".json")) {
|
||||
continue
|
||||
sort.Slice(contents, func(i, j int) bool {
|
||||
time1, err := lib.TimeFromString(contents[i].Date)
|
||||
if err != nil {
|
||||
log.Errorf("Bad date while sorting: %s", err.Error())
|
||||
return false
|
||||
}
|
||||
|
||||
var con Content
|
||||
jsonc, err := os.ReadFile(path.Join(news_path, e.Name()))
|
||||
if err != nil {
|
||||
log.Errorf("Cannot news JSON for %s: %s", e.Name(), err)
|
||||
return RenderError(c, 500)
|
||||
time2, err := lib.TimeFromString(contents[j].Date)
|
||||
if err != nil {
|
||||
log.Errorf("Bad date while sorting: %s", err.Error())
|
||||
return false
|
||||
}
|
||||
err = json.Unmarshal(jsonc, &con)
|
||||
if err != nil {
|
||||
log.Errorf("Cannot parse news JSON for %s: %s", e.Name(), err)
|
||||
return RenderError(c, 500)
|
||||
}
|
||||
|
||||
con.Time, err = TimeFromString(con.Date)
|
||||
if err != nil {
|
||||
log.Errorf("Cannot parse time for '%s': %s", con.Name, err)
|
||||
}
|
||||
res = append(res, con)
|
||||
|
||||
}
|
||||
|
||||
sort.Slice(res, func(i, j int) bool {
|
||||
return res[i].Time.After(res[j].Time)
|
||||
|
||||
return time1.After(time2)
|
||||
})
|
||||
|
||||
return c.Render("news", fiber.Map{
|
||||
"news": res,
|
||||
"news": contents,
|
||||
})
|
||||
}
|
||||
|
||||
func PostRoute(c *fiber.Ctx) error {
|
||||
postid := c.Params("id")
|
||||
if len(postid) == 0 {
|
||||
return RenderError(c, 404)
|
||||
return lib.RenderError(c, 404)
|
||||
}
|
||||
|
||||
var res Content
|
||||
entries, err := os.ReadDir(news_path)
|
||||
contents, err := lib.ListContent("news")
|
||||
if err != nil {
|
||||
log.Errorf("Cannot news dir: %s", err)
|
||||
return RenderError(c, 500)
|
||||
log.Errorf("ListContent -> %s", err.Error())
|
||||
return lib.RenderError(c, 500)
|
||||
}
|
||||
|
||||
for _, e := range entries {
|
||||
if(!strings.HasSuffix(e.Name(), ".json")) {
|
||||
for _, con := range contents {
|
||||
if(con.ID != postid) {
|
||||
continue
|
||||
}
|
||||
|
||||
jsonc, err := os.ReadFile(path.Join(news_path, e.Name()))
|
||||
con, err = lib.GetContent(con.Dir, con.Name)
|
||||
if err != nil {
|
||||
log.Errorf("Cannot news JSON: %s", err)
|
||||
return RenderError(c, 500)
|
||||
}
|
||||
|
||||
err = json.Unmarshal(jsonc, &res)
|
||||
if err != nil {
|
||||
log.Errorf("Cannot parse news JSON: %s", err)
|
||||
return RenderError(c, 500)
|
||||
log.Errorf("GetContent -> %s", err.Error())
|
||||
return lib.RenderError(c, 500)
|
||||
}
|
||||
|
||||
if(res.ID == postid) {
|
||||
res, err = GetContent(news_path, strings.Split(e.Name(), ".json")[0])
|
||||
if err != nil {
|
||||
log.Errorf("Cannot get content: %s", err)
|
||||
return RenderError(c, 500)
|
||||
}
|
||||
|
||||
return c.Render("post", fiber.Map{
|
||||
"post": res,
|
||||
})
|
||||
}
|
||||
return c.Render("post", fiber.Map{
|
||||
"title": "News",
|
||||
"post": con,
|
||||
})
|
||||
}
|
||||
|
||||
return RenderError(c, 404)
|
||||
return lib.RenderError(c, 404)
|
||||
}
|
||||
|
@ -1,73 +0,0 @@
|
||||
package routes
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"html/template"
|
||||
"os"
|
||||
"path"
|
||||
"time"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/russross/blackfriday/v2"
|
||||
)
|
||||
|
||||
type Content struct {
|
||||
ID string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Date string `json:"date"`
|
||||
Author string `json:"author"`
|
||||
Content template.HTML `json:"content"`
|
||||
Time time.Time
|
||||
}
|
||||
|
||||
func TimeFromString(date string) (time.Time, error) {
|
||||
res, err := time.Parse("02/01/06", date)
|
||||
if err == nil {
|
||||
return res, nil
|
||||
}
|
||||
|
||||
return time.Now(), nil
|
||||
}
|
||||
|
||||
func GetContent(pth string, name string) (Content, error) {
|
||||
var res Content
|
||||
jsonfile := path.Join(pth, name+".json")
|
||||
mdfile := path.Join(pth, name+".md")
|
||||
|
||||
jsoncontent, err := os.ReadFile(jsonfile)
|
||||
if err != nil {
|
||||
return res, errors.New("Cannot read JSON file: "+jsonfile)
|
||||
}
|
||||
|
||||
mdcontent, err := os.ReadFile(mdfile)
|
||||
if err != nil {
|
||||
return res, errors.New("Cannot read markdown file: "+mdfile)
|
||||
}
|
||||
|
||||
if(json.Unmarshal(jsoncontent, &res)!= nil) {
|
||||
return res, errors.New("Cannot parse JSON: "+jsonfile)
|
||||
}
|
||||
|
||||
ext := blackfriday.FencedCode
|
||||
ext |= blackfriday.BackslashLineBreak
|
||||
ext |= blackfriday.Strikethrough
|
||||
|
||||
md := string(blackfriday.Run(mdcontent, blackfriday.WithExtensions(ext)))
|
||||
res.Content = template.HTML(md)
|
||||
return res, nil
|
||||
}
|
||||
|
||||
func RenderError(c *fiber.Ctx, code int) error{
|
||||
var msg string = "Server Error"
|
||||
c.Status(code)
|
||||
|
||||
switch code {
|
||||
case 404:
|
||||
msg = "Not Found"
|
||||
}
|
||||
|
||||
return c.Render("error", fiber.Map{
|
||||
"msg": msg,
|
||||
})
|
||||
}
|
53
routes/wiki.go
Normal file
53
routes/wiki.go
Normal file
@ -0,0 +1,53 @@
|
||||
package routes
|
||||
|
||||
import (
|
||||
"git.matterlinux.xyz/Matterlinux/website/lib"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/gofiber/fiber/v2/log"
|
||||
)
|
||||
|
||||
func WikiMainRoute(c *fiber.Ctx) error {
|
||||
con, err := lib.GetContent("wiki", "main")
|
||||
if err != nil {
|
||||
log.Error("GetContent -> ", err)
|
||||
return lib.RenderError(c, 500)
|
||||
}
|
||||
|
||||
return c.Render("post", fiber.Map{
|
||||
"title": "Wiki",
|
||||
"post": con,
|
||||
})
|
||||
}
|
||||
|
||||
func WikiRoute(c *fiber.Ctx) error{
|
||||
docid := c.Params("id")
|
||||
if len(docid) == 0 {
|
||||
return lib.RenderError(c, 404)
|
||||
}
|
||||
|
||||
contents, err := lib.ListContent("wiki")
|
||||
if err != nil {
|
||||
log.Errorf("ListContent -> %s", err.Error())
|
||||
return lib.RenderError(c, 500)
|
||||
}
|
||||
|
||||
for _, con := range contents {
|
||||
if(con.ID != docid) {
|
||||
continue
|
||||
}
|
||||
|
||||
con, err = lib.GetContent(con.Dir, con.Name)
|
||||
if err != nil {
|
||||
log.Errorf("GetContent -> %s", err.Error())
|
||||
return lib.RenderError(c, 500)
|
||||
}
|
||||
|
||||
con.Title = "Wiki: "+con.Title
|
||||
return c.Render("post", fiber.Map{
|
||||
"title": "Wiki",
|
||||
"post": con,
|
||||
})
|
||||
}
|
||||
|
||||
return lib.RenderError(c, 404)
|
||||
}
|
Reference in New Issue
Block a user