update: general formatting and cleanup

This commit is contained in:
ngn
2024-08-14 00:32:48 +03:00
parent 25f30b51f0
commit 87a9236e01
15 changed files with 402 additions and 344 deletions

View File

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

View File

@ -12,44 +12,44 @@ import (
// python3 -c "import string; print(string.ascii_letters+string.digits+\" /,_-?!'\\\"\")"
var valid string = `abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 /,_-?!'"`
func IsStringValid(s string) bool{
for _, c := range s {
if !strings.Contains(valid, string(c)){
fmt.Printf("%c\n", c)
return false
}
}
return true
func IsStringValid(s string) bool {
for _, c := range s {
if !strings.Contains(valid, string(c)) {
fmt.Printf("%c\n", c)
return false
}
}
return true
}
func RenderError(c *fiber.Ctx, code int) error{
var msg string = "Server Error"
c.Status(code)
func RenderError(c *fiber.Ctx, code int) error {
var msg string = "Server Error"
c.Status(code)
switch code {
case 404:
msg = "Not Found"
}
switch code {
case 404:
msg = "Not Found"
}
return c.Render("error", fiber.Map{
"msg": msg,
})
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
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
ext |= blackfriday.NoEmptyLineBeforeBlock
ext := blackfriday.FencedCode
ext |= blackfriday.BackslashLineBreak
ext |= blackfriday.Strikethrough
ext |= blackfriday.Tables
ext |= blackfriday.NoEmptyLineBeforeBlock
return string(blackfriday.Run([]byte(md), blackfriday.WithExtensions(ext)))
return string(blackfriday.Run([]byte(md), blackfriday.WithExtensions(ext)))
}