Adding the wiki route and better styling

This commit is contained in:
ngn 2024-01-06 23:20:58 +03:00
parent 6477240dd4
commit 7f61a0cd58
15 changed files with 299 additions and 161 deletions

88
lib/content.go Normal file
View File

@ -0,0 +1,88 @@
package lib
import (
"encoding/json"
"errors"
"html/template"
"os"
"path"
"strings"
)
const CONTENT_PATH string = "content"
type Content struct{
Dir string
Name string
Title string `json:"title"`
ID string `json:"id"`
Md string
HTML template.HTML
Date string `json:"date"`
Author string `json:"author"`
}
func ListContent(dir string) ([]Content, error) {
var res []Content
dirpath := path.Join(CONTENT_PATH, dir)
entries, err := os.ReadDir(dirpath)
if err != nil {
return nil, errors.New("Cannot readdir: "+dirpath)
}
for _, e := range entries {
if(!strings.HasSuffix(e.Name(), ".json")) {
continue
}
var con Content
jsonfile := path.Join(dirpath, e.Name())
jsoncon, err := os.ReadFile(jsonfile)
if err != nil {
return nil, errors.New("Cannot get JSON: "+jsonfile)
}
err = json.Unmarshal(jsoncon, &con)
if err != nil {
return nil, errors.New("Cannot parse JSON: "+jsonfile)
}
con.Dir = dir
con.Name = strings.Split(e.Name(), ".")[0]
res = append(res, con)
}
return res, nil
}
func GetContent(dir string, name string) (Content, error) {
var res Content
jsonfile := path.Join(CONTENT_PATH, dir, name+".json")
mdfile := path.Join(CONTENT_PATH, dir, name+".md")
res.Dir = dir
res.Name = name
jsoncontent, err := os.ReadFile(jsonfile)
if err != nil {
return res, errors.New("Cannot read JSON file: "+jsonfile)
}
rawcontent, 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)
}
res.Md = string(rawcontent)
res.HTML = template.HTML(ParseMarkdown(res.Md))
return res, nil
}

39
lib/util.go Normal file
View File

@ -0,0 +1,39 @@
package lib
import (
"time"
"github.com/gofiber/fiber/v2"
"github.com/russross/blackfriday/v2"
)
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,
})
}
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 ParseMarkdown(md string) string {
ext := blackfriday.FencedCode
ext |= blackfriday.BackslashLineBreak
ext |= blackfriday.Strikethrough
ext |= blackfriday.Tables
return string(blackfriday.Run([]byte(md), blackfriday.WithExtensions(ext)))
}

10
main.go
View File

@ -1,6 +1,7 @@
package main package main
import ( import (
"git.matterlinux.xyz/Matterlinux/website/lib"
"git.matterlinux.xyz/Matterlinux/website/routes" "git.matterlinux.xyz/Matterlinux/website/routes"
"github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/log" "github.com/gofiber/fiber/v2/log"
@ -13,13 +14,18 @@ func main(){
Views: engine, Views: engine,
}) })
app.Static("/", "./public") app.Static("/", "./public")
app.Static("/images", "./content/images") app.Static("/assets", "./content/assets")
app.Get("/", routes.IndexRoute) app.Get("/", routes.IndexRoute)
app.Get("/news", routes.NewsRoute) app.Get("/news", routes.NewsRoute)
app.Get("/news/:id", routes.PostRoute) app.Get("/news/:id", routes.PostRoute)
app.Get("/wiki", routes.WikiMainRoute)
app.Get("/wiki/:id", routes.WikiRoute)
app.Get("*", func(c *fiber.Ctx) error { app.Get("*", func(c *fiber.Ctx) error {
return routes.RenderError(c, 404) return lib.RenderError(c, 404)
}) })
log.Fatal(app.Listen(":9878")) log.Fatal(app.Listen(":9878"))

9
public/font.css Normal file
View File

@ -0,0 +1,9 @@
@font-face {
font-family: Ubuntu;
src: url("fonts/ubuntu.ttf");
}
@font-face {
font-family: JetBrainsMono;
src: url("fonts/jetbrains.ttf");
}

View File

@ -22,6 +22,12 @@
font-size: 20px; font-size: 20px;
color: var(--bright-second); color: var(--bright-second);
line-height: 28px; line-height: 28px;
letter-spacing: .01em;
}
.md strong {
font-weight: 900;
color: var(--bright-main);
} }
.md a { .md a {
@ -43,5 +49,54 @@
.md code { .md code {
font-family: JetBrainsMono; font-family: JetBrainsMono;
background: var(--dark-third); background: var(--dark-third);
color: var(--bright-main);
border-radius: 5px; border-radius: 5px;
font-size: 19px;
}
.md pre {
margin-top: 0;
margin-bottom: 0;
font-family: JetBrainsMono;
font-size: 12px;
word-wrap: normal;
margin-bottom: 16px;
margin-top: 16px;
padding: 16px;
overflow: auto;
line-height: 1.45;
color: var(--bright-main);
background-color: var(--dark-third);
border-radius: 6px;
}
.md table {
border-spacing: 0;
border-collapse: collapse;
display: block;
width: max-content;
max-width: 100%;
overflow: auto;
margin: 16px 0px 16px 0px;
}
.md table th{
font-weight: 900;
font-size: 20px;
}
.md table th,
.md table td{
font-size: 20px;
padding: 6px 13px;
border: 1px solid var(--bright-second);
}
.markdown-body table tr {
background-color: var(--dark-second);
border-top: 1px solid var(--bright-second);
}
.md a:hover {
color: var(--bright-main);
} }

View File

@ -1,17 +1,9 @@
@font-face { @import url("/font.css");
font-family: Ubuntu;
src: url("fonts/ubuntu.ttf");
}
@font-face {
font-family: JetBrainsMono;
src: url("fonts/jetbrains.ttf");
}
:root { :root {
--dark-main: black; --dark-main: black;
--dark-second: #070707; --dark-second: #070707;
--dark-third: #101010; --dark-third: #151515;
--bright-main: white; --bright-main: white;
--bright-second: #DFDFDF; --bright-second: #DFDFDF;
} }

View File

@ -1,18 +1,19 @@
package routes package routes
import ( import (
"git.matterlinux.xyz/Matterlinux/website/lib"
"github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/log" "github.com/gofiber/fiber/v2/log"
) )
func IndexRoute(c *fiber.Ctx) error{ func IndexRoute(c *fiber.Ctx) error{
content, err := GetContent("content", "index") con, err := lib.GetContent("", "index")
if err != nil { if err != nil {
log.Error(err) log.Error("GetContent -> ", err)
return RenderError(c, 500) return lib.RenderError(c, 500)
} }
return c.Render("index", fiber.Map{ return c.Render("index", fiber.Map{
"readme": content, "readme": con,
}) })
} }

View File

@ -1,102 +1,69 @@
package routes package routes
import ( import (
"encoding/json"
"os"
"path"
"sort" "sort"
"strings"
"git.matterlinux.xyz/Matterlinux/website/lib"
"github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/log" "github.com/gofiber/fiber/v2/log"
) )
var news_path = path.Join("content", "news")
func NewsRoute(c *fiber.Ctx) error { func NewsRoute(c *fiber.Ctx) error {
var res []Content contents, err := lib.ListContent("news")
entries, err := os.ReadDir(news_path)
if err != nil { if err != nil {
log.Errorf("Cannot news dir: %s", err) log.Errorf("ListContent -> %s", err.Error())
return RenderError(c, 500) return lib.RenderError(c, 500)
} }
for _, e := range entries { sort.Slice(contents, func(i, j int) bool {
if(!strings.HasSuffix(e.Name(), ".json")) { time1, err := lib.TimeFromString(contents[i].Date)
continue if err != nil {
log.Errorf("Bad date while sorting: %s", err.Error())
return false
} }
var con Content time2, err := lib.TimeFromString(contents[j].Date)
jsonc, err := os.ReadFile(path.Join(news_path, e.Name())) if err != nil {
if err != nil { log.Errorf("Bad date while sorting: %s", err.Error())
log.Errorf("Cannot news JSON for %s: %s", e.Name(), err) return false
return RenderError(c, 500)
} }
err = json.Unmarshal(jsonc, &con)
if err != nil { return time1.After(time2)
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 c.Render("news", fiber.Map{ return c.Render("news", fiber.Map{
"news": res, "news": contents,
}) })
} }
func PostRoute(c *fiber.Ctx) error { func PostRoute(c *fiber.Ctx) error {
postid := c.Params("id") postid := c.Params("id")
if len(postid) == 0 { if len(postid) == 0 {
return RenderError(c, 404) return lib.RenderError(c, 404)
} }
var res Content contents, err := lib.ListContent("news")
entries, err := os.ReadDir(news_path)
if err != nil { if err != nil {
log.Errorf("Cannot news dir: %s", err) log.Errorf("ListContent -> %s", err.Error())
return RenderError(c, 500) return lib.RenderError(c, 500)
} }
for _, e := range entries { for _, con := range contents {
if(!strings.HasSuffix(e.Name(), ".json")) { if(con.ID != postid) {
continue continue
} }
jsonc, err := os.ReadFile(path.Join(news_path, e.Name())) con, err = lib.GetContent(con.Dir, con.Name)
if err != nil { if err != nil {
log.Errorf("Cannot news JSON: %s", err) log.Errorf("GetContent -> %s", err.Error())
return RenderError(c, 500) return lib.RenderError(c, 500)
}
err = json.Unmarshal(jsonc, &res)
if err != nil {
log.Errorf("Cannot parse news JSON: %s", err)
return RenderError(c, 500)
} }
if(res.ID == postid) { return c.Render("post", fiber.Map{
res, err = GetContent(news_path, strings.Split(e.Name(), ".json")[0]) "title": "News",
if err != nil { "post": con,
log.Errorf("Cannot get content: %s", err) })
return RenderError(c, 500)
}
return c.Render("post", fiber.Map{
"post": res,
})
}
} }
return RenderError(c, 404) return lib.RenderError(c, 404)
} }

View File

@ -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
View 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)
}

View File

@ -1,7 +1,7 @@
<!DOCTYPE html> <!DOCTYPE html>
<html lang="en"> <html lang="en">
<head> <head>
<title>Matter Linux</title> <title>MatterLinux | Error</title>
<meta charset="UTF-8"> <meta charset="UTF-8">
<meta name="viewport" content="width=1200"> <meta name="viewport" content="width=1200">
<link href="/style.css" rel="stylesheet"> <link href="/style.css" rel="stylesheet">

View File

@ -1,7 +1,7 @@
<!DOCTYPE html> <!DOCTYPE html>
<html lang="en"> <html lang="en">
<head> <head>
<title>Matter Linux</title> <title>MatterLinux</title>
<meta charset="UTF-8"> <meta charset="UTF-8">
<meta name="viewport" content="width=1200"> <meta name="viewport" content="width=1200">
<link href="/style.css" rel="stylesheet"> <link href="/style.css" rel="stylesheet">
@ -10,7 +10,7 @@
<body> <body>
{{template "parts/bar" .}} {{template "parts/bar" .}}
<div class="readme md"> <div class="readme md">
{{.readme.Content}} {{.readme.HTML}}
</div> </div>
</body> </body>
</html> </html>

View File

@ -1,7 +1,7 @@
<!DOCTYPE html> <!DOCTYPE html>
<html lang="en"> <html lang="en">
<head> <head>
<title>Matter Linux</title> <title>MatterLinux | News</title>
<meta charset="UTF-8"> <meta charset="UTF-8">
<meta name="viewport" content="width=1200"> <meta name="viewport" content="width=1200">
<link href="/style.css" rel="stylesheet"> <link href="/style.css" rel="stylesheet">
@ -12,7 +12,7 @@
{{range $new := .news}} {{range $new := .news}}
<a href="/news/{{$new.ID}}" class="new-small"> <a href="/news/{{$new.ID}}" class="new-small">
<h1>{{$new.Date}}</h1> <h1>{{$new.Date}}</h1>
<h1>{{$new.Name}}</h1> <h1>{{$new.Title}}</h1>
</a> </a>
{{end}} {{end}}
</div> </div>

View File

@ -3,7 +3,8 @@
<div class="links"> <div class="links">
<a href="/">Home</a> <a href="/">Home</a>
<a href="/news">News</a> <a href="/news">News</a>
<a href="https://wiki.matterlinux.xyz">Wiki</a> <a href="/wiki">Wiki</a>
<a href="https://git.matterlinux.xyz">Source</a> <a href="https://git.matterlinux.xyz">Source</a>
<a href="https://pkgs.matterlinux.xyz">Packages</a>
</div> </div>
</div> </div>

View File

@ -1,7 +1,7 @@
<!DOCTYPE html> <!DOCTYPE html>
<html lang="en"> <html lang="en">
<head> <head>
<title>Matter Linux</title> <title>MatterLinux | {{.title}}</title>
<meta charset="UTF-8"> <meta charset="UTF-8">
<meta name="viewport" content="width=1200"> <meta name="viewport" content="width=1200">
<link href="/style.css" rel="stylesheet"> <link href="/style.css" rel="stylesheet">
@ -10,10 +10,10 @@
<body> <body>
{{template "parts/bar" .}} {{template "parts/bar" .}}
<div class="post"> <div class="post">
<h1>{{.post.Name}}</h1> <h1>{{.post.Title}}</h1>
<p>{{.post.Date}} | by {{.post.Author}}</p> <p>{{.post.Date}} | by {{.post.Author}}</p>
<div class="md"> <div class="md">
{{.post.Content}} {{.post.HTML}}
</div> </div>
</div> </div>
</body> </body>